blob: 22c834bcf3126057e81a3638d0695f82b28582f9 [file] [log] [blame]
Greg Aker90715732011-04-21 14:36:26 -05001<?php
2
3require_once(BASEPATH.'helpers/text_helper.php');
4
Greg Akerb4d93db2011-04-21 14:42:33 -05005class Text_helper_test extends CI_TestCase
Greg Aker90715732011-04-21 14:36:26 -05006{
7 private $_long_string;
8
9 public function setUp()
10 {
11 $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
16 public function testWordLimiter()
17 {
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
25 public function testCharacterLimiter()
26 {
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
35 public function testAsciiToEntities()
36 {
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
50 public function testEntitiesToAscii()
51 {
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 }
62
63 // ------------------------------------------------------------------------
64
65 public function testCensoredWords()
66 {
67 $censored = array('boob', 'nerd', 'ass', 'fart');
68
69 $strs = array(
70 'Ted bobbled the ball' => 'Ted bobbled the ball',
71 'Jake is a nerdo' => 'Jake is a nerdo',
72 'The borg will assimilate you' => 'The borg will assimilate you',
73 'Did Mary Fart?' => 'Did Mary $*#?',
74 'Jake is really a boob' => 'Jake is really a $*#'
75 );
76
77
78 foreach ($strs as $str => $expect)
79 {
80 $this->assertEquals($expect, word_censor($str, $censored, '$*#'));
81 }
82
83 // test censored words being sent as a string
84 $this->assertEquals('test', word_censor('test', 'test'));
85 }
86
87 // ------------------------------------------------------------------------
88
89 public function testHighlightCode()
90 {
91 $code = '<?php var_dump($this); ?>';
92 $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>";
93
94 $this->assertEquals($expect, highlight_code($code));
95 }
96
97 // ------------------------------------------------------------------------
98
99 public function testHighlightPhrase()
100 {
101 $strs = array(
102 'this is a phrase' => '<strong>this is</strong> a phrase',
103 'this is another' => '<strong>this is</strong> another',
104 'Gimme a test, Sally' => 'Gimme a test, Sally',
105 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>',
106 '' => ''
107 );
108
109 foreach ($strs as $str => $expect)
110 {
111 $this->assertEquals($expect, highlight_phrase($str, 'this is'));
112 }
113 }
114
115 // ------------------------------------------------------------------------
116
117 public function testEllipsizing()
118 {
119 $strs = array(
120 '0' => array(
121 'this is my string' => '&hellip; my string',
122 "here's another one" => '&hellip;nother one',
123 'this one is just a bit longer' => '&hellip;bit longer',
124 'short' => 'short'
125 ),
126 '.5' => array(
127 'this is my string' => 'this &hellip;tring',
128 "here's another one" => "here'&hellip;r one",
129 'this one is just a bit longer' => 'this &hellip;onger',
130 'short' => 'short'
131 ),
132 '1' => array(
133 'this is my string' => 'this is my&hellip;',
134 "here's another one" => "here's ano&hellip;",
135 'this one is just a bit longer' => 'this one i&hellip;',
136 'short' => 'short'
137 ),
138 );
139
140 foreach ($strs as $pos => $s)
141 {
142 foreach ($s as $str => $expect)
143 {
144 $this->assertEquals($expect, ellipsize($str, 10, $pos));
145 }
146 }
147 }
148
149 // ------------------------------------------------------------------------
150
151}