blob: 7a7dc0a124b0060cc3ed4ed66a25f4d3aa20e41e [file] [log] [blame]
Greg Aker90715732011-04-21 14:36:26 -05001<?php
2
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07003class Text_helper_test extends CI_TestCase {
Greg Aker90715732011-04-21 14:36:26 -05004
Greg Aker90715732011-04-21 14:36:26 -05005 private $_long_string;
Andrey Andreev99b782d2012-06-09 22:24:46 +03006
Eric Barnes68286a42011-04-21 22:00:33 -04007 public function set_up()
Greg Aker90715732011-04-21 14:36:26 -05008 {
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07009 $this->helper('text');
Andrey Andreev99b782d2012-06-09 22:24:46 +030010
Greg Aker90715732011-04-21 14:36:26 -050011 $this->_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.';
12 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030013
Greg Aker90715732011-04-21 14:36:26 -050014 // ------------------------------------------------------------------------
Andrey Andreev99b782d2012-06-09 22:24:46 +030015
Eric Barnes68286a42011-04-21 22:00:33 -040016 public function test_word_limiter()
Greg Aker90715732011-04-21 14:36:26 -050017 {
18 $this->assertEquals('Once upon a time,&#8230;', word_limiter($this->_long_string, 4));
19 $this->assertEquals('Once upon a time,&hellip;', word_limiter($this->_long_string, 4, '&hellip;'));
20 $this->assertEquals('', word_limiter('', 4));
21 }
22
Andrey Andreev99b782d2012-06-09 22:24:46 +030023 // ------------------------------------------------------------------------
24
Eric Barnes68286a42011-04-21 22:00:33 -040025 public function test_character_limiter()
Greg Aker90715732011-04-21 14:36:26 -050026 {
27 $this->assertEquals('Once upon a time, a&#8230;', character_limiter($this->_long_string, 20));
28 $this->assertEquals('Once upon a time, a&hellip;', character_limiter($this->_long_string, 20, '&hellip;'));
29 $this->assertEquals('Short', character_limiter('Short', 20));
30 $this->assertEquals('Short', character_limiter('Short', 5));
31 }
32
Andrey Andreev99b782d2012-06-09 22:24:46 +030033 // ------------------------------------------------------------------------
34
Eric Barnes68286a42011-04-21 22:00:33 -040035 public function test_ascii_to_entities()
Greg Aker90715732011-04-21 14:36:26 -050036 {
37 $strs = array(
38 '“‘ “test”' => '&#8220;&#8216; &#8220;test&#8221;',
39 '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;'
40 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030041
Greg Aker90715732011-04-21 14:36:26 -050042 foreach ($strs as $str => $expect)
43 {
44 $this->assertEquals($expect, ascii_to_entities($str));
45 }
46 }
47
Andrey Andreev99b782d2012-06-09 22:24:46 +030048 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -050049
Eric Barnes68286a42011-04-21 22:00:33 -040050 public function test_entities_to_ascii()
Greg Aker90715732011-04-21 14:36:26 -050051 {
52 $strs = array(
53 '&#8220;&#8216; &#8220;test&#8221;' => '“‘ “test”',
54 '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;' => '†¥¨ˆøåß∂ƒ©˙∆˚¬'
55 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030056
Greg Aker90715732011-04-21 14:36:26 -050057 foreach ($strs as $str => $expect)
58 {
59 $this->assertEquals($expect, entities_to_ascii($str));
Andrey Andreev99b782d2012-06-09 22:24:46 +030060 }
Greg Aker90715732011-04-21 14:36:26 -050061 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030062
63 // ------------------------------------------------------------------------
64
65 public function test_convert_accented_characters()
Eric Barnes68286a42011-04-21 22:00:33 -040066 {
dchill427ecc5cd2012-10-12 16:25:51 -040067 $this->ci_vfs_clone('application/config/foreign_chars.php');
Eric Barnes68286a42011-04-21 22:00:33 -040068 $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ'));
69 $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü'));
70 }
Greg Aker90715732011-04-21 14:36:26 -050071
Andrey Andreev99b782d2012-06-09 22:24:46 +030072 // ------------------------------------------------------------------------
73
Eric Barnes68286a42011-04-21 22:00:33 -040074 public function test_censored_words()
Greg Aker90715732011-04-21 14:36:26 -050075 {
76 $censored = array('boob', 'nerd', 'ass', 'fart');
Andrey Andreev99b782d2012-06-09 22:24:46 +030077
Greg Aker90715732011-04-21 14:36:26 -050078 $strs = array(
79 'Ted bobbled the ball' => 'Ted bobbled the ball',
80 'Jake is a nerdo' => 'Jake is a nerdo',
81 'The borg will assimilate you' => 'The borg will assimilate you',
82 'Did Mary Fart?' => 'Did Mary $*#?',
83 'Jake is really a boob' => 'Jake is really a $*#'
84 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030085
Greg Aker90715732011-04-21 14:36:26 -050086 foreach ($strs as $str => $expect)
87 {
88 $this->assertEquals($expect, word_censor($str, $censored, '$*#'));
89 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030090
Greg Aker90715732011-04-21 14:36:26 -050091 // test censored words being sent as a string
92 $this->assertEquals('test', word_censor('test', 'test'));
93 }
94
Andrey Andreev99b782d2012-06-09 22:24:46 +030095 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -050096
Eric Barnes68286a42011-04-21 22:00:33 -040097 public function test_highlight_code()
Greg Aker90715732011-04-21 14:36:26 -050098 {
Greg Aker90715732011-04-21 14:36:26 -050099 $expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">&lt;?php&nbsp;var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">);&nbsp;</span><span style=\"color: #0000BB\">?&gt;&nbsp;</span>\n</span>\n</code>";
100
Andrey Andreev99b782d2012-06-09 22:24:46 +0300101 $this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>'));
Greg Aker90715732011-04-21 14:36:26 -0500102 }
103
Andrey Andreev99b782d2012-06-09 22:24:46 +0300104 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500105
Eric Barnes68286a42011-04-21 22:00:33 -0400106 public function test_highlight_phrase()
Greg Aker90715732011-04-21 14:36:26 -0500107 {
108 $strs = array(
Andrey Andreev0bb32d32014-01-07 16:30:42 +0200109 'this is a phrase' => '<mark>this is</mark> a phrase',
110 'this is another' => '<mark>this is</mark> another',
111 'Gimme a test, Sally' => 'Gimme a test, Sally',
112 'Or tell me what this is' => 'Or tell me what <mark>this is</mark>',
113 '' => ''
Greg Aker90715732011-04-21 14:36:26 -0500114 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300115
Greg Aker90715732011-04-21 14:36:26 -0500116 foreach ($strs as $str => $expect)
117 {
118 $this->assertEquals($expect, highlight_phrase($str, 'this is'));
119 }
Andrey Andreev0bb32d32014-01-07 16:30:42 +0200120
121 $this->assertEquals('<strong>this is</strong> a strong test', highlight_phrase('this is a strong test', 'this is', '<strong>', '</strong>'));
Greg Aker90715732011-04-21 14:36:26 -0500122 }
123
Andrey Andreev99b782d2012-06-09 22:24:46 +0300124 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500125
Joffrey Jaffeux5ace4402012-06-06 14:52:15 +0200126 public function test_ellipsize()
Greg Aker90715732011-04-21 14:36:26 -0500127 {
128 $strs = array(
129 '0' => array(
130 'this is my string' => '&hellip; my string',
131 "here's another one" => '&hellip;nother one',
132 'this one is just a bit longer' => '&hellip;bit longer',
133 'short' => 'short'
134 ),
135 '.5' => array(
136 'this is my string' => 'this &hellip;tring',
137 "here's another one" => "here'&hellip;r one",
138 'this one is just a bit longer' => 'this &hellip;onger',
139 'short' => 'short'
140 ),
141 '1' => array(
142 'this is my string' => 'this is my&hellip;',
143 "here's another one" => "here's ano&hellip;",
144 'this one is just a bit longer' => 'this one i&hellip;',
145 'short' => 'short'
146 ),
147 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300148
Greg Aker90715732011-04-21 14:36:26 -0500149 foreach ($strs as $pos => $s)
150 {
151 foreach ($s as $str => $expect)
152 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300153 $this->assertEquals($expect, ellipsize($str, 10, $pos));
Greg Aker90715732011-04-21 14:36:26 -0500154 }
155 }
156 }
157
Andrey Andreev99b782d2012-06-09 22:24:46 +0300158 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500159
Joffrey Jaffeux0ef92f62012-06-06 15:12:37 +0200160 public function test_word_wrap()
161 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300162 $string = 'Here is a simple string of text that will help us demonstrate this function.';
163 $this->assertEquals(substr_count(word_wrap($string, 25), "\n"), 4);
Joffrey Jaffeux4221b982012-06-06 15:38:35 +0200164 }
165
Andrey Andreev99b782d2012-06-09 22:24:46 +0300166 // ------------------------------------------------------------------------
Joffrey Jaffeux4221b982012-06-06 15:38:35 +0200167
168 public function test_default_word_wrap_charlim()
169 {
Joffrey Jaffeux385452c2012-06-06 15:43:18 +0200170 $string = "Here is a longer string of text that will help us demonstrate the default charlim of this function.";
Andrey Andreev99b782d2012-06-09 22:24:46 +0300171 $this->assertEquals(strpos(word_wrap($string), "\n"), 73);
Joffrey Jaffeux0ef92f62012-06-06 15:12:37 +0200172 }
173
Greg Aker90715732011-04-21 14:36:26 -0500174}