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 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 67 | $this->ci_vfs_clone('application/config/foreign_chars.php'); |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 68 | $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); |
| 69 | $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); |
| 70 | } |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 71 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 72 | // ------------------------------------------------------------------------ |
| 73 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 74 | public function test_censored_words() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 75 | { |
| 76 | $censored = array('boob', 'nerd', 'ass', 'fart'); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 77 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 78 | $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 Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 85 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 86 | foreach ($strs as $str => $expect) |
| 87 | { |
| 88 | $this->assertEquals($expect, word_censor($str, $censored, '$*#')); |
| 89 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 90 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 91 | // test censored words being sent as a string |
| 92 | $this->assertEquals('test', word_censor('test', 'test')); |
| 93 | } |
| 94 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 95 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 96 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 97 | public function test_highlight_code() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 98 | { |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 99 | $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>"; |
| 100 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 101 | $this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>')); |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 104 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 105 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 106 | public function test_highlight_phrase() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 107 | { |
| 108 | $strs = array( |
Andrey Andreev | 0bb32d3 | 2014-01-07 16:30:42 +0200 | [diff] [blame] | 109 | '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 Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 114 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 115 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 116 | foreach ($strs as $str => $expect) |
| 117 | { |
| 118 | $this->assertEquals($expect, highlight_phrase($str, 'this is')); |
| 119 | } |
Andrey Andreev | 0bb32d3 | 2014-01-07 16:30:42 +0200 | [diff] [blame] | 120 | |
| 121 | $this->assertEquals('<strong>this is</strong> a strong test', highlight_phrase('this is a strong test', 'this is', '<strong>', '</strong>')); |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 124 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 125 | |
Joffrey Jaffeux | 5ace440 | 2012-06-06 14:52:15 +0200 | [diff] [blame] | 126 | public function test_ellipsize() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 127 | { |
| 128 | $strs = array( |
| 129 | '0' => array( |
| 130 | 'this is my string' => '… my string', |
| 131 | "here's another one" => '…nother one', |
| 132 | 'this one is just a bit longer' => '…bit longer', |
| 133 | 'short' => 'short' |
| 134 | ), |
| 135 | '.5' => array( |
| 136 | 'this is my string' => 'this …tring', |
| 137 | "here's another one" => "here'…r one", |
| 138 | 'this one is just a bit longer' => 'this …onger', |
| 139 | 'short' => 'short' |
| 140 | ), |
| 141 | '1' => array( |
| 142 | 'this is my string' => 'this is my…', |
| 143 | "here's another one" => "here's ano…", |
| 144 | 'this one is just a bit longer' => 'this one i…', |
| 145 | 'short' => 'short' |
| 146 | ), |
| 147 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 148 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 149 | foreach ($strs as $pos => $s) |
| 150 | { |
| 151 | foreach ($s as $str => $expect) |
| 152 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 153 | $this->assertEquals($expect, ellipsize($str, 10, $pos)); |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 158 | // ------------------------------------------------------------------------ |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 159 | |
Joffrey Jaffeux | 0ef92f6 | 2012-06-06 15:12:37 +0200 | [diff] [blame] | 160 | public function test_word_wrap() |
| 161 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 162 | $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 Jaffeux | 4221b98 | 2012-06-06 15:38:35 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 166 | // ------------------------------------------------------------------------ |
Joffrey Jaffeux | 4221b98 | 2012-06-06 15:38:35 +0200 | [diff] [blame] | 167 | |
| 168 | public function test_default_word_wrap_charlim() |
| 169 | { |
Joffrey Jaffeux | 385452c | 2012-06-06 15:43:18 +0200 | [diff] [blame] | 170 | $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] | 171 | $this->assertEquals(strpos(word_wrap($string), "\n"), 73); |
Joffrey Jaffeux | 0ef92f6 | 2012-06-06 15:12:37 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 174 | } |