blob: f131469cbc7856f128b7987a93675bc750c280ea [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 {
67 $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ'));
68 $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü'));
69 }
Greg Aker90715732011-04-21 14:36:26 -050070
Andrey Andreev99b782d2012-06-09 22:24:46 +030071 // ------------------------------------------------------------------------
72
Eric Barnes68286a42011-04-21 22:00:33 -040073 public function test_censored_words()
Greg Aker90715732011-04-21 14:36:26 -050074 {
75 $censored = array('boob', 'nerd', 'ass', 'fart');
Andrey Andreev99b782d2012-06-09 22:24:46 +030076
Greg Aker90715732011-04-21 14:36:26 -050077 $strs = array(
78 'Ted bobbled the ball' => 'Ted bobbled the ball',
79 'Jake is a nerdo' => 'Jake is a nerdo',
80 'The borg will assimilate you' => 'The borg will assimilate you',
81 'Did Mary Fart?' => 'Did Mary $*#?',
82 'Jake is really a boob' => 'Jake is really a $*#'
83 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030084
Greg Aker90715732011-04-21 14:36:26 -050085 foreach ($strs as $str => $expect)
86 {
87 $this->assertEquals($expect, word_censor($str, $censored, '$*#'));
88 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030089
Greg Aker90715732011-04-21 14:36:26 -050090 // test censored words being sent as a string
91 $this->assertEquals('test', word_censor('test', 'test'));
92 }
93
Andrey Andreev99b782d2012-06-09 22:24:46 +030094 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -050095
Eric Barnes68286a42011-04-21 22:00:33 -040096 public function test_highlight_code()
Greg Aker90715732011-04-21 14:36:26 -050097 {
Greg Aker90715732011-04-21 14:36:26 -050098 $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>";
99
Andrey Andreev99b782d2012-06-09 22:24:46 +0300100 $this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>'));
Greg Aker90715732011-04-21 14:36:26 -0500101 }
102
Andrey Andreev99b782d2012-06-09 22:24:46 +0300103 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500104
Eric Barnes68286a42011-04-21 22:00:33 -0400105 public function test_highlight_phrase()
Greg Aker90715732011-04-21 14:36:26 -0500106 {
107 $strs = array(
108 'this is a phrase' => '<strong>this is</strong> a phrase',
109 'this is another' => '<strong>this is</strong> another',
110 'Gimme a test, Sally' => 'Gimme a test, Sally',
111 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>',
112 '' => ''
113 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300114
Greg Aker90715732011-04-21 14:36:26 -0500115 foreach ($strs as $str => $expect)
116 {
117 $this->assertEquals($expect, highlight_phrase($str, 'this is'));
118 }
119 }
120
Andrey Andreev99b782d2012-06-09 22:24:46 +0300121 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500122
Joffrey Jaffeux5ace4402012-06-06 14:52:15 +0200123 public function test_ellipsize()
Greg Aker90715732011-04-21 14:36:26 -0500124 {
125 $strs = array(
126 '0' => array(
127 'this is my string' => '&hellip; my string',
128 "here's another one" => '&hellip;nother one',
129 'this one is just a bit longer' => '&hellip;bit longer',
130 'short' => 'short'
131 ),
132 '.5' => array(
133 'this is my string' => 'this &hellip;tring',
134 "here's another one" => "here'&hellip;r one",
135 'this one is just a bit longer' => 'this &hellip;onger',
136 'short' => 'short'
137 ),
138 '1' => array(
139 'this is my string' => 'this is my&hellip;',
140 "here's another one" => "here's ano&hellip;",
141 'this one is just a bit longer' => 'this one i&hellip;',
142 'short' => 'short'
143 ),
144 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300145
Greg Aker90715732011-04-21 14:36:26 -0500146 foreach ($strs as $pos => $s)
147 {
148 foreach ($s as $str => $expect)
149 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300150 $this->assertEquals($expect, ellipsize($str, 10, $pos));
Greg Aker90715732011-04-21 14:36:26 -0500151 }
152 }
153 }
154
Andrey Andreev99b782d2012-06-09 22:24:46 +0300155 // ------------------------------------------------------------------------
Greg Aker90715732011-04-21 14:36:26 -0500156
Joffrey Jaffeux0ef92f62012-06-06 15:12:37 +0200157 public function test_word_wrap()
158 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300159 $string = 'Here is a simple string of text that will help us demonstrate this function.';
160 $this->assertEquals(substr_count(word_wrap($string, 25), "\n"), 4);
Joffrey Jaffeux4221b982012-06-06 15:38:35 +0200161 }
162
Andrey Andreev99b782d2012-06-09 22:24:46 +0300163 // ------------------------------------------------------------------------
Joffrey Jaffeux4221b982012-06-06 15:38:35 +0200164
165 public function test_default_word_wrap_charlim()
166 {
Joffrey Jaffeux385452c2012-06-06 15:43:18 +0200167 $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 +0300168 $this->assertEquals(strpos(word_wrap($string), "\n"), 73);
Joffrey Jaffeux0ef92f62012-06-06 15:12:37 +0200169 }
170
Greg Aker90715732011-04-21 14:36:26 -0500171}