blob: 584066b0c545fd434a9d11ca149516872db6a905 [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;
6
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');
10
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 }
13
14 // ------------------------------------------------------------------------
15
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
23 // ------------------------------------------------------------------------
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
33 // ------------------------------------------------------------------------
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 );
41
42 foreach ($strs as $str => $expect)
43 {
44 $this->assertEquals($expect, ascii_to_entities($str));
45 }
46 }
47
48 // ------------------------------------------------------------------------
49
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 );
56
57 foreach ($strs as $str => $expect)
58 {
59 $this->assertEquals($expect, entities_to_ascii($str));
60 }
61 }
Eric Barnes68286a42011-04-21 22:00:33 -040062
63 // ------------------------------------------------------------------------
64
65 function test_convert_accented_characters()
66 {
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
71 // ------------------------------------------------------------------------
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');
76
77 $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 );
84
85
86 foreach ($strs as $str => $expect)
87 {
88 $this->assertEquals($expect, word_censor($str, $censored, '$*#'));
89 }
90
91 // test censored words being sent as a string
92 $this->assertEquals('test', word_censor('test', 'test'));
93 }
94
95 // ------------------------------------------------------------------------
96
Eric Barnes68286a42011-04-21 22:00:33 -040097 public function test_highlight_code()
Greg Aker90715732011-04-21 14:36:26 -050098 {
99 $code = '<?php var_dump($this); ?>';
100 $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>";
101
102 $this->assertEquals($expect, highlight_code($code));
103 }
104
105 // ------------------------------------------------------------------------
106
Eric Barnes68286a42011-04-21 22:00:33 -0400107 public function test_highlight_phrase()
Greg Aker90715732011-04-21 14:36:26 -0500108 {
109 $strs = array(
110 'this is a phrase' => '<strong>this is</strong> a phrase',
111 'this is another' => '<strong>this is</strong> another',
112 'Gimme a test, Sally' => 'Gimme a test, Sally',
113 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>',
114 '' => ''
115 );
116
117 foreach ($strs as $str => $expect)
118 {
119 $this->assertEquals($expect, highlight_phrase($str, 'this is'));
120 }
121 }
122
123 // ------------------------------------------------------------------------
124
Eric Barnes68286a42011-04-21 22:00:33 -0400125 public function test_ellipsizing()
Greg Aker90715732011-04-21 14:36:26 -0500126 {
127 $strs = array(
128 '0' => array(
129 'this is my string' => '&hellip; my string',
130 "here's another one" => '&hellip;nother one',
131 'this one is just a bit longer' => '&hellip;bit longer',
132 'short' => 'short'
133 ),
134 '.5' => array(
135 'this is my string' => 'this &hellip;tring',
136 "here's another one" => "here'&hellip;r one",
137 'this one is just a bit longer' => 'this &hellip;onger',
138 'short' => 'short'
139 ),
140 '1' => array(
141 'this is my string' => 'this is my&hellip;',
142 "here's another one" => "here's ano&hellip;",
143 'this one is just a bit longer' => 'this one i&hellip;',
144 'short' => 'short'
145 ),
146 );
147
148 foreach ($strs as $pos => $s)
149 {
150 foreach ($s as $str => $expect)
151 {
152 $this->assertEquals($expect, ellipsize($str, 10, $pos));
153 }
154 }
155 }
156
157 // ------------------------------------------------------------------------
158
159}