Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 3 | class Text_helper_test extends CI_TestCase { |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 4 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 5 | private $_long_string; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 6 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 7 | public function set_up() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 8 | { |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 9 | $this->helper('text'); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 10 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 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 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 13 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 14 | // ------------------------------------------------------------------------ |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 15 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 16 | public function test_word_limiter() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 17 | { |
| 18 | $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4)); |
| 19 | $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4, '…')); |
| 20 | $this->assertEquals('', word_limiter('', 4)); |
| 21 | } |
| 22 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 23 | // ------------------------------------------------------------------------ |
| 24 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 25 | public function test_character_limiter() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 26 | { |
| 27 | $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20)); |
| 28 | $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20, '…')); |
| 29 | $this->assertEquals('Short', character_limiter('Short', 20)); |
| 30 | $this->assertEquals('Short', character_limiter('Short', 5)); |
| 31 | } |
| 32 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 33 | // ------------------------------------------------------------------------ |
| 34 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 35 | public function test_ascii_to_entities() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 36 | { |
| 37 | $strs = array( |
| 38 | '“‘ “test”' => '“‘ “test”', |
| 39 | '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' |
| 40 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 41 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 42 | foreach ($strs as $str => $expect) |
| 43 | { |
| 44 | $this->assertEquals($expect, ascii_to_entities($str)); |
| 45 | } |
| 46 | } |
| 47 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 48 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 49 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 50 | public function test_entities_to_ascii() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 51 | { |
| 52 | $strs = array( |
| 53 | '“‘ “test”' => '“‘ “test”', |
| 54 | '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' |
| 55 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 56 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 57 | foreach ($strs as $str => $expect) |
| 58 | { |
| 59 | $this->assertEquals($expect, entities_to_ascii($str)); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 60 | } |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 61 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 62 | |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | public function test_convert_accented_characters() |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 66 | { |
| 67 | $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); |
| 68 | $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); |
| 69 | } |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 70 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 71 | // ------------------------------------------------------------------------ |
| 72 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 73 | public function test_censored_words() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 74 | { |
| 75 | $censored = array('boob', 'nerd', 'ass', 'fart'); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 76 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 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 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 84 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 85 | foreach ($strs as $str => $expect) |
| 86 | { |
| 87 | $this->assertEquals($expect, word_censor($str, $censored, '$*#')); |
| 88 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 89 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 90 | // test censored words being sent as a string |
| 91 | $this->assertEquals('test', word_censor('test', 'test')); |
| 92 | } |
| 93 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 94 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 95 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 96 | public function test_highlight_code() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 97 | { |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 98 | $expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\"><?php var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">); </span><span style=\"color: #0000BB\">?> </span>\n</span>\n</code>"; |
| 99 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 100 | $this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>')); |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 103 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 104 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 105 | public function test_highlight_phrase() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 106 | { |
| 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 Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 114 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 115 | foreach ($strs as $str => $expect) |
| 116 | { |
| 117 | $this->assertEquals($expect, highlight_phrase($str, 'this is')); |
| 118 | } |
| 119 | } |
| 120 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 121 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 122 | |
Joffrey Jaffeux | 5ace440 | 2012-06-06 14:52:15 +0200 | [diff] [blame] | 123 | public function test_ellipsize() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 124 | { |
| 125 | $strs = array( |
| 126 | '0' => array( |
| 127 | 'this is my string' => '… my string', |
| 128 | "here's another one" => '…nother one', |
| 129 | 'this one is just a bit longer' => '…bit longer', |
| 130 | 'short' => 'short' |
| 131 | ), |
| 132 | '.5' => array( |
| 133 | 'this is my string' => 'this …tring', |
| 134 | "here's another one" => "here'…r one", |
| 135 | 'this one is just a bit longer' => 'this …onger', |
| 136 | 'short' => 'short' |
| 137 | ), |
| 138 | '1' => array( |
| 139 | 'this is my string' => 'this is my…', |
| 140 | "here's another one" => "here's ano…", |
| 141 | 'this one is just a bit longer' => 'this one i…', |
| 142 | 'short' => 'short' |
| 143 | ), |
| 144 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 145 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 146 | foreach ($strs as $pos => $s) |
| 147 | { |
| 148 | foreach ($s as $str => $expect) |
| 149 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 150 | $this->assertEquals($expect, ellipsize($str, 10, $pos)); |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 155 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 156 | |
Joffrey Jaffeux | 0ef92f6 | 2012-06-06 15:12:37 +0200 | [diff] [blame] | 157 | public function test_word_wrap() |
| 158 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 159 | $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 Jaffeux | 4221b98 | 2012-06-06 15:38:35 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 163 | // ------------------------------------------------------------------------ |
Joffrey Jaffeux | 4221b98 | 2012-06-06 15:38:35 +0200 | [diff] [blame] | 164 | |
| 165 | public function test_default_word_wrap_charlim() |
| 166 | { |
Joffrey Jaffeux | 385452c | 2012-06-06 15:43:18 +0200 | [diff] [blame] | 167 | $string = "Here is a longer string of text that will help us demonstrate the default charlim of this function."; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 168 | $this->assertEquals(strpos(word_wrap($string), "\n"), 73); |
Joffrey Jaffeux | 0ef92f6 | 2012-06-06 15:12:37 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 171 | } |