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