blob: 250aefb24692c3bc64c45f6e987168067f9ccae6 [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 {
7 $obj = new StdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->type = new Mock_Libraries_Typography();
Greg Aker2cadade2011-04-21 15:00:49 -05009
10 $this->ci_instance($obj);
11
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 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',
36 );
37
38 foreach ($strs as $str => $expected)
39 {
40 $this->assertEquals($expected, $this->type->format_characters($str));
41 }
42 }
43
44 // --------------------------------------------------------------------
45
Eric Barnes68286a42011-04-21 22:00:33 -040046 public function test_nl2br_except_pre()
Greg Aker2cadade2011-04-21 15:00:49 -050047 {
48 $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
88 $this->assertEquals($expected,
89 $this->type->nl2br_except_pre($str));
90 }
91
92 // --------------------------------------------------------------------
93
Eric Barnes68286a42011-04-21 22:00:33 -040094 public function test_auto_typography()
Greg Aker2cadade2011-04-21 15:00:49 -050095 {
96 $this->_blank_string();
97 $this->_standardize_new_lines();
98 $this->_reduce_linebreaks();
99 $this->_remove_comments();
100 $this->_protect_pre();
101 $this->_no_opening_block();
102 $this->_protect_braced_quotes();
103 }
104
105 // --------------------------------------------------------------------
106
107 private function _blank_string()
108 {
109 // Test blank string
110 $this->assertEquals('', $this->type->auto_typography(''));
111 }
112
113 // --------------------------------------------------------------------
114
115 private function _standardize_new_lines()
116 {
117 $strs = array(
118 "My string\rhas return characters" => "<p>My string<br />\nhas return characters</p>",
119 'This one does not!' => '<p>This one does not!</p>'
120 );
121
122 foreach ($strs as $str => $expect)
123 {
124 $this->assertEquals($expect, $this->type->auto_typography($str));
125 }
126 }
127
128 // --------------------------------------------------------------------
129
130 private function _reduce_linebreaks()
131 {
132 $str = "This has way too many linebreaks.\n\n\n\nSee?";
133 $expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>";
134
135 $this->assertEquals($expect, $this->type->auto_typography($str, TRUE));
136 }
137
138 // --------------------------------------------------------------------
139
140 private function _remove_comments()
141 {
142 $str = '<!-- I can haz comments? --> But no!';
143 $expect = '<p><!-- I can haz comments? -->&nbsp; But no!</p>';
144
145 $this->assertEquals($expect, $this->type->auto_typography($str));
146 }
147
148 // --------------------------------------------------------------------
149
150 private function _protect_pre()
151 {
152 $str = '<p>My Sentence</p><pre>var_dump($this);</pre>';
153 $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>';
154
155 $this->assertEquals($expect, $this->type->auto_typography($str));
156 }
157
158 // --------------------------------------------------------------------
159
160 private function _no_opening_block()
161 {
162 $str = 'My Sentence<pre>var_dump($this);</pre>';
163 $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>';
164
165 $this->assertEquals($expect, $this->type->auto_typography($str));
166 }
167
168 // --------------------------------------------------------------------
169
170 public function _protect_braced_quotes()
171 {
172 $this->type->protect_braced_quotes = TRUE;
173
174 $str = 'Test {parse="foobar"}';
175 $expect = '<p>Test {parse="foobar"}</p>';
176
177 $this->assertEquals($expect, $this->type->auto_typography($str));
178
179 $this->type->protect_braced_quotes = FALSE;
180
181 $str = 'Test {parse="foobar"}';
182 $expect = '<p>Test {parse=&#8220;foobar&#8221;}</p>';
183
184 $this->assertEquals($expect, $this->type->auto_typography($str));
185
186
187 }
188}