Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 3 | class Typography_test extends CI_TestCase { |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 4 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 5 | public function set_up() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 6 | { |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 7 | $obj = new stdClass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 8 | $obj->type = new Mock_Libraries_Typography(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 9 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 10 | $this->ci_instance($obj); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 11 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 12 | $this->type = $obj->type; |
| 13 | } |
| 14 | |
| 15 | // -------------------------------------------------------------------- |
| 16 | |
| 17 | /** |
| 18 | * Tests the format_characters() function. |
| 19 | * |
| 20 | * this can and should grow. |
| 21 | */ |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 22 | public function test_format_characters() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 23 | { |
| 24 | $strs = array( |
| 25 | '"double quotes"' => '“double quotes”', |
| 26 | '"testing" in "theory" that is' => '“testing” in “theory” that is', |
| 27 | "Here's what I'm" => 'Here’s what I’m', |
| 28 | '&' => '&', |
| 29 | '&' => '&', |
| 30 | ' ' => ' ', |
| 31 | '--' => '—', |
| 32 | 'foo...' => 'foo…', |
| 33 | 'foo..' => 'foo..', |
| 34 | 'foo...bar.' => 'foo…bar.', |
| 35 | 'test. new' => 'test. new', |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 36 | ); |
| 37 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 38 | foreach ($strs as $str => $expected) |
| 39 | { |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 40 | $this->assertEquals($expected, $this->type->format_characters($str)); |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | // -------------------------------------------------------------------- |
| 45 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 46 | public function test_nl2br_except_pre() |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 47 | { |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 48 | $str = <<<EOH |
| 49 | Hello, I'm a happy string with some new lines. |
| 50 | |
| 51 | I like to skip. |
| 52 | |
| 53 | Jump |
| 54 | |
| 55 | and sing. |
| 56 | |
| 57 | <pre> |
| 58 | I am inside a pre tag. Please don't mess with me. |
| 59 | |
| 60 | k? |
| 61 | </pre> |
| 62 | |
| 63 | That's my story and I'm sticking to it. |
| 64 | |
| 65 | The End. |
| 66 | EOH; |
| 67 | |
| 68 | $expected = <<<EOH |
| 69 | Hello, I'm a happy string with some new lines. <br /> |
| 70 | <br /> |
| 71 | I like to skip.<br /> |
| 72 | <br /> |
| 73 | Jump<br /> |
| 74 | <br /> |
| 75 | and sing.<br /> |
| 76 | <br /> |
| 77 | <pre> |
| 78 | I am inside a pre tag. Please don't mess with me. |
| 79 | |
| 80 | k? |
| 81 | </pre><br /> |
| 82 | <br /> |
| 83 | That's my story and I'm sticking to it.<br /> |
| 84 | <br /> |
| 85 | The End. |
| 86 | EOH; |
| 87 | |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 88 | $this->assertEquals($expected, $this->type->nl2br_except_pre($str)); |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 92 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 93 | public function test_auto_typography() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 94 | { |
| 95 | $this->_blank_string(); |
| 96 | $this->_standardize_new_lines(); |
| 97 | $this->_reduce_linebreaks(); |
| 98 | $this->_remove_comments(); |
| 99 | $this->_protect_pre(); |
| 100 | $this->_no_opening_block(); |
| 101 | $this->_protect_braced_quotes(); |
| 102 | } |
| 103 | |
| 104 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 105 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 106 | private function _blank_string() |
| 107 | { |
| 108 | // Test blank string |
| 109 | $this->assertEquals('', $this->type->auto_typography('')); |
| 110 | } |
| 111 | |
| 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | private function _standardize_new_lines() |
| 115 | { |
| 116 | $strs = array( |
| 117 | "My string\rhas return characters" => "<p>My string<br />\nhas return characters</p>", |
| 118 | 'This one does not!' => '<p>This one does not!</p>' |
| 119 | ); |
| 120 | |
| 121 | foreach ($strs as $str => $expect) |
| 122 | { |
| 123 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // -------------------------------------------------------------------- |
| 128 | |
| 129 | private function _reduce_linebreaks() |
| 130 | { |
| 131 | $str = "This has way too many linebreaks.\n\n\n\nSee?"; |
| 132 | $expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>"; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 133 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 134 | $this->assertEquals($expect, $this->type->auto_typography($str, TRUE)); |
| 135 | } |
| 136 | |
| 137 | // -------------------------------------------------------------------- |
| 138 | |
| 139 | private function _remove_comments() |
| 140 | { |
| 141 | $str = '<!-- I can haz comments? --> But no!'; |
| 142 | $expect = '<p><!-- I can haz comments? --> But no!</p>'; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 143 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 144 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
| 148 | |
| 149 | private function _protect_pre() |
| 150 | { |
| 151 | $str = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
| 152 | $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 153 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 154 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | private function _no_opening_block() |
| 160 | { |
| 161 | $str = 'My Sentence<pre>var_dump($this);</pre>'; |
| 162 | $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 163 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 164 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 165 | } |
| 166 | |
| 167 | // -------------------------------------------------------------------- |
| 168 | |
| 169 | public function _protect_braced_quotes() |
| 170 | { |
| 171 | $this->type->protect_braced_quotes = TRUE; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 172 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 173 | $str = 'Test {parse="foobar"}'; |
| 174 | $expect = '<p>Test {parse="foobar"}</p>'; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 175 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 176 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 177 | |
| 178 | $this->type->protect_braced_quotes = FALSE; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 179 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 180 | $str = 'Test {parse="foobar"}'; |
| 181 | $expect = '<p>Test {parse=“foobar”}</p>'; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 182 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 183 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 184 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame^] | 185 | |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 186 | } |