summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Typography_test.php
blob: 5dba062438e2c2a3a9c937c19f7c41ec11bfce36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php

class Typography_test extends CI_TestCase {

	public function set_up()
	{
		$this->type = new Mock_Libraries_Typography();
		$this->ci_instance('type', $this->type);
	}

	// --------------------------------------------------------------------

	/**
	 * Tests the format_characters() function.
	 *
	 * this can and should grow.
	 */
	public function test_format_characters()
	{
		$strs = array(
			'"double quotes"' 				=> '&#8220;double quotes&#8221;',
			'"testing" in "theory" that is' => '&#8220;testing&#8221; in &#8220;theory&#8221; that is',
			"Here's what I'm" 				=> 'Here&#8217;s what I&#8217;m',
			'&' 							=> '&amp;',
			'&amp;' 						=> '&amp;',
			'&nbsp;'						=> '&nbsp;',
			'--'							=> '&#8212;',
			'foo...'						=> 'foo&#8230;',
			'foo..'							=> 'foo..',
			'foo...bar.'					=> 'foo&#8230;bar.',
			'test.  new'					=> 'test.&nbsp; new',
		);

		foreach ($strs as $str => $expected)
		{
			$this->assertEquals($expected, $this->type->format_characters($str));
		}
	}

	// --------------------------------------------------------------------

	public function test_nl2br_except_pre()
	{
		$str = <<<EOH
Hello, I'm a happy string with some new lines.  

I like to skip.

Jump

and sing.

<pre>
I am inside a pre tag.  Please don't mess with me.

k?
</pre>

That's my story and I'm sticking to it.

The End.
EOH;

		$expected = <<<EOH
Hello, I'm a happy string with some new lines.  <br />
<br />
I like to skip.<br />
<br />
Jump<br />
<br />
and sing.<br />
<br />
<pre>
I am inside a pre tag.  Please don't mess with me.

k?
</pre><br />
<br />
That's my story and I'm sticking to it.<br />
<br />
The End.
EOH;

		$this->assertEquals($expected, $this->type->nl2br_except_pre($str));
	}

	// --------------------------------------------------------------------

	public function test_auto_typography()
	{
		$this->_blank_string();
		$this->_standardize_new_lines();
		$this->_reduce_linebreaks();
		$this->_remove_comments();
		$this->_protect_pre();
		$this->_no_opening_block();
		$this->_protect_braced_quotes();
	}

	// --------------------------------------------------------------------

	private function _blank_string()
	{
		// Test blank string
		$this->assertEquals('', $this->type->auto_typography(''));
	}

	// --------------------------------------------------------------------

	private function _standardize_new_lines()
	{
		$strs = array(
			"My string\rhas return characters"	=> "<p>My string<br />\nhas return characters</p>",
			'This one does not!' 				=> '<p>This one does not!</p>'
		);

		foreach ($strs as $str => $expect)
		{
			$this->assertEquals($expect, $this->type->auto_typography($str));
		}
	}

	// --------------------------------------------------------------------

	private function _reduce_linebreaks()
	{
		$str = "This has way too many linebreaks.\n\n\n\nSee?";
		$expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>";

		$this->assertEquals($expect, $this->type->auto_typography($str, TRUE));
	}

	// --------------------------------------------------------------------

	private function _remove_comments()
	{
		$str = '<!-- I can haz comments? -->  But no!';
		$expect = '<p><!-- I can haz comments? -->&nbsp; But no!</p>';

		$this->assertEquals($expect, $this->type->auto_typography($str));
	}

	// --------------------------------------------------------------------

	private function _protect_pre()
	{
		$str = '<p>My Sentence</p><pre>var_dump($this);</pre>';
		$expect = '<p>My Sentence</p><pre>var_dump($this);</pre>';

		$this->assertEquals($expect, $this->type->auto_typography($str));
	}

	// --------------------------------------------------------------------

	private function _no_opening_block()
	{
		$str = 'My Sentence<pre>var_dump($this);</pre>';
		$expect = '<p>My Sentence</p><pre>var_dump($this);</pre>';

		$this->assertEquals($expect, $this->type->auto_typography($str));
	}

	// --------------------------------------------------------------------

	public function _protect_braced_quotes()
	{
		$this->type->protect_braced_quotes = TRUE;

		$str = 'Test {parse="foobar"}';
		$expect = '<p>Test {parse="foobar"}</p>';

		$this->assertEquals($expect, $this->type->auto_typography($str));

		$this->type->protect_braced_quotes = FALSE;

		$str = 'Test {parse="foobar"}';
		$expect = '<p>Test {parse=&#8220;foobar&#8221;}</p>';

		$this->assertEquals($expect, $this->type->auto_typography($str));
	}

}