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