blob: a0533bae08c00e9f8deb4645d6c6e836e04a65b4 [file] [log] [blame]
Greg Aker2cadade2011-04-21 15:00:49 -05001<?php
2
3require BASEPATH.'libraries/Typography.php';
4
5class Typography_test extends CI_TestCase
6{
7
Eric Barnes68286a42011-04-21 22:00:33 -04008 public function set_up()
Greg Aker2cadade2011-04-21 15:00:49 -05009 {
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 Barnes68286a42011-04-21 22:00:33 -040025 public function test_format_characters()
Greg Aker2cadade2011-04-21 15:00:49 -050026 {
27 $strs = array(
28 '"double quotes"' => '&#8220;double quotes&#8221;',
29 '"testing" in "theory" that is' => '&#8220;testing&#8221; in &#8220;theory&#8221; that is',
30 "Here's what I'm" => 'Here&#8217;s what I&#8217;m',
31 '&' => '&amp;',
32 '&amp;' => '&amp;',
33 '&nbsp;' => '&nbsp;',
34 '--' => '&#8212;',
35 'foo...' => 'foo&#8230;',
36 'foo..' => 'foo..',
37 'foo...bar.' => 'foo&#8230;bar.',
38 'test. new' => 'test.&nbsp; 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 Barnes68286a42011-04-21 22:00:33 -040049 public function test_nl2br_except_pre()
Greg Aker2cadade2011-04-21 15:00:49 -050050 {
51 $str = <<<EOH
52Hello, I'm a happy string with some new lines.
53
54I like to skip.
55
56Jump
57
58and sing.
59
60<pre>
61I am inside a pre tag. Please don't mess with me.
62
63k?
64</pre>
65
66That's my story and I'm sticking to it.
67
68The End.
69EOH;
70
71 $expected = <<<EOH
72Hello, I'm a happy string with some new lines. <br />
73<br />
74I like to skip.<br />
75<br />
76Jump<br />
77<br />
78and sing.<br />
79<br />
80<pre>
81I am inside a pre tag. Please don't mess with me.
82
83k?
84</pre><br />
85<br />
86That's my story and I'm sticking to it.<br />
87<br />
88The End.
89EOH;
90
91 $this->assertEquals($expected,
92 $this->type->nl2br_except_pre($str));
93 }
94
95 // --------------------------------------------------------------------
96
Eric Barnes68286a42011-04-21 22:00:33 -040097 public function test_auto_typography()
Greg Aker2cadade2011-04-21 15:00:49 -050098 {
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? -->&nbsp; 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=&#8220;foobar&#8221;}</p>';
186
187 $this->assertEquals($expect, $this->type->auto_typography($str));
188
189
190 }
191}