blob: eb6dacb7352c68fa1e7d23218cde97705cc514e9 [file] [log] [blame]
Greg Aker2cadade2011-04-21 15:00:49 -05001<?php
2
Taufan Adityaac5373a2012-03-28 16:03:38 +07003class Typography_test extends CI_TestCase {
Greg Aker2cadade2011-04-21 15:00:49 -05004
Eric Barnes68286a42011-04-21 22:00:33 -04005 public function set_up()
Greg Aker2cadade2011-04-21 15:00:49 -05006 {
Andrey Andreevc1862882012-06-09 23:16:58 +03007 $obj = new stdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->type = new Mock_Libraries_Typography();
Andrey Andreevc1862882012-06-09 23:16:58 +03009
Greg Aker2cadade2011-04-21 15:00:49 -050010 $this->ci_instance($obj);
Andrey Andreevc1862882012-06-09 23:16:58 +030011
Greg Aker2cadade2011-04-21 15:00:49 -050012 $this->type = $obj->type;
13 }
14
15 // --------------------------------------------------------------------
16
17 /**
18 * Tests the format_characters() function.
19 *
20 * this can and should grow.
21 */
Eric Barnes68286a42011-04-21 22:00:33 -040022 public function test_format_characters()
Greg Aker2cadade2011-04-21 15:00:49 -050023 {
24 $strs = array(
25 '"double quotes"' => '&#8220;double quotes&#8221;',
26 '"testing" in "theory" that is' => '&#8220;testing&#8221; in &#8220;theory&#8221; that is',
27 "Here's what I'm" => 'Here&#8217;s what I&#8217;m',
28 '&' => '&amp;',
29 '&amp;' => '&amp;',
30 '&nbsp;' => '&nbsp;',
31 '--' => '&#8212;',
32 'foo...' => 'foo&#8230;',
33 'foo..' => 'foo..',
34 'foo...bar.' => 'foo&#8230;bar.',
35 'test. new' => 'test.&nbsp; new',
Andrey Andreevc1862882012-06-09 23:16:58 +030036 );
37
Greg Aker2cadade2011-04-21 15:00:49 -050038 foreach ($strs as $str => $expected)
39 {
Andrey Andreevc1862882012-06-09 23:16:58 +030040 $this->assertEquals($expected, $this->type->format_characters($str));
Greg Aker2cadade2011-04-21 15:00:49 -050041 }
42 }
43
44 // --------------------------------------------------------------------
45
Eric Barnes68286a42011-04-21 22:00:33 -040046 public function test_nl2br_except_pre()
Andrey Andreevc1862882012-06-09 23:16:58 +030047 {
Greg Aker2cadade2011-04-21 15:00:49 -050048 $str = <<<EOH
49Hello, I'm a happy string with some new lines.
50
51I like to skip.
52
53Jump
54
55and sing.
56
57<pre>
58I am inside a pre tag. Please don't mess with me.
59
60k?
61</pre>
62
63That's my story and I'm sticking to it.
64
65The End.
66EOH;
67
68 $expected = <<<EOH
69Hello, I'm a happy string with some new lines. <br />
70<br />
71I like to skip.<br />
72<br />
73Jump<br />
74<br />
75and sing.<br />
76<br />
77<pre>
78I am inside a pre tag. Please don't mess with me.
79
80k?
81</pre><br />
82<br />
83That's my story and I'm sticking to it.<br />
84<br />
85The End.
86EOH;
87
Andrey Andreevc1862882012-06-09 23:16:58 +030088 $this->assertEquals($expected, $this->type->nl2br_except_pre($str));
Greg Aker2cadade2011-04-21 15:00:49 -050089 }
90
91 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030092
Eric Barnes68286a42011-04-21 22:00:33 -040093 public function test_auto_typography()
Greg Aker2cadade2011-04-21 15:00:49 -050094 {
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 Andreevc1862882012-06-09 23:16:58 +0300105
Greg Aker2cadade2011-04-21 15:00:49 -0500106 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 Andreevc1862882012-06-09 23:16:58 +0300133
Greg Aker2cadade2011-04-21 15:00:49 -0500134 $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? -->&nbsp; But no!</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300143
Greg Aker2cadade2011-04-21 15:00:49 -0500144 $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 Andreevc1862882012-06-09 23:16:58 +0300153
Greg Aker2cadade2011-04-21 15:00:49 -0500154 $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 Andreevc1862882012-06-09 23:16:58 +0300163
Greg Aker2cadade2011-04-21 15:00:49 -0500164 $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 Andreevc1862882012-06-09 23:16:58 +0300172
Greg Aker2cadade2011-04-21 15:00:49 -0500173 $str = 'Test {parse="foobar"}';
174 $expect = '<p>Test {parse="foobar"}</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300175
Greg Aker2cadade2011-04-21 15:00:49 -0500176 $this->assertEquals($expect, $this->type->auto_typography($str));
177
178 $this->type->protect_braced_quotes = FALSE;
Andrey Andreevc1862882012-06-09 23:16:58 +0300179
Greg Aker2cadade2011-04-21 15:00:49 -0500180 $str = 'Test {parse="foobar"}';
181 $expect = '<p>Test {parse=&#8220;foobar&#8221;}</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300182
Greg Aker2cadade2011-04-21 15:00:49 -0500183 $this->assertEquals($expect, $this->type->auto_typography($str));
Greg Aker2cadade2011-04-21 15:00:49 -0500184 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300185
Greg Aker2cadade2011-04-21 15:00:49 -0500186}