blob: e6ee7b05c458767715c00fbb3726a026e1497324 [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 Andreev10e7a322014-02-20 16:42:16 +02007 $this->type = new CI_Typography();
dchill427ecc5cd2012-10-12 16:25:51 -04008 $this->ci_instance('type', $this->type);
Greg Aker2cadade2011-04-21 15:00:49 -05009 }
10
11 // --------------------------------------------------------------------
12
13 /**
14 * Tests the format_characters() function.
15 *
16 * this can and should grow.
17 */
Eric Barnes68286a42011-04-21 22:00:33 -040018 public function test_format_characters()
Greg Aker2cadade2011-04-21 15:00:49 -050019 {
20 $strs = array(
21 '"double quotes"' => '&#8220;double quotes&#8221;',
22 '"testing" in "theory" that is' => '&#8220;testing&#8221; in &#8220;theory&#8221; that is',
23 "Here's what I'm" => 'Here&#8217;s what I&#8217;m',
24 '&' => '&amp;',
25 '&amp;' => '&amp;',
26 '&nbsp;' => '&nbsp;',
27 '--' => '&#8212;',
28 'foo...' => 'foo&#8230;',
29 'foo..' => 'foo..',
30 'foo...bar.' => 'foo&#8230;bar.',
31 'test. new' => 'test.&nbsp; new',
Andrey Andreevc1862882012-06-09 23:16:58 +030032 );
33
Greg Aker2cadade2011-04-21 15:00:49 -050034 foreach ($strs as $str => $expected)
35 {
Andrey Andreevc1862882012-06-09 23:16:58 +030036 $this->assertEquals($expected, $this->type->format_characters($str));
Greg Aker2cadade2011-04-21 15:00:49 -050037 }
38 }
39
40 // --------------------------------------------------------------------
41
Eric Barnes68286a42011-04-21 22:00:33 -040042 public function test_nl2br_except_pre()
Andrey Andreevc1862882012-06-09 23:16:58 +030043 {
Greg Aker2cadade2011-04-21 15:00:49 -050044 $str = <<<EOH
45Hello, I'm a happy string with some new lines.
46
47I like to skip.
48
49Jump
50
51and sing.
52
53<pre>
54I am inside a pre tag. Please don't mess with me.
55
56k?
57</pre>
58
59That's my story and I'm sticking to it.
60
61The End.
62EOH;
63
64 $expected = <<<EOH
65Hello, I'm a happy string with some new lines. <br />
66<br />
67I like to skip.<br />
68<br />
69Jump<br />
70<br />
71and sing.<br />
72<br />
73<pre>
74I am inside a pre tag. Please don't mess with me.
75
76k?
77</pre><br />
78<br />
79That's my story and I'm sticking to it.<br />
80<br />
81The End.
82EOH;
83
Andrey Andreevc1862882012-06-09 23:16:58 +030084 $this->assertEquals($expected, $this->type->nl2br_except_pre($str));
Greg Aker2cadade2011-04-21 15:00:49 -050085 }
86
87 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030088
Eric Barnes68286a42011-04-21 22:00:33 -040089 public function test_auto_typography()
Greg Aker2cadade2011-04-21 15:00:49 -050090 {
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 Andreevc1862882012-06-09 23:16:58 +0300101
Greg Aker2cadade2011-04-21 15:00:49 -0500102 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 Andreevc1862882012-06-09 23:16:58 +0300129
Greg Aker2cadade2011-04-21 15:00:49 -0500130 $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? -->&nbsp; But no!</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300139
Greg Aker2cadade2011-04-21 15:00:49 -0500140 $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 Andreevc1862882012-06-09 23:16:58 +0300149
Greg Aker2cadade2011-04-21 15:00:49 -0500150 $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 Andreevc1862882012-06-09 23:16:58 +0300159
Greg Aker2cadade2011-04-21 15:00:49 -0500160 $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 Andreevc1862882012-06-09 23:16:58 +0300168
Greg Aker2cadade2011-04-21 15:00:49 -0500169 $str = 'Test {parse="foobar"}';
170 $expect = '<p>Test {parse="foobar"}</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300171
Greg Aker2cadade2011-04-21 15:00:49 -0500172 $this->assertEquals($expect, $this->type->auto_typography($str));
173
174 $this->type->protect_braced_quotes = FALSE;
Andrey Andreevc1862882012-06-09 23:16:58 +0300175
Greg Aker2cadade2011-04-21 15:00:49 -0500176 $str = 'Test {parse="foobar"}';
177 $expect = '<p>Test {parse=&#8220;foobar&#8221;}</p>';
Andrey Andreevc1862882012-06-09 23:16:58 +0300178
Greg Aker2cadade2011-04-21 15:00:49 -0500179 $this->assertEquals($expect, $this->type->auto_typography($str));
Greg Aker2cadade2011-04-21 15:00:49 -0500180 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300181
Greg Aker2cadade2011-04-21 15:00:49 -0500182}