Completed
Push — master ( 327ddb...5776a0 )
by Daniel
17:43
created

DBHTMLTextTest::testSummaryEndings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
5
use SilverStripe\ORM\FieldType\DBHTMLText;
6
use SilverStripe\ORM\FieldType\DBField;
7
8
9
/**
10
 * @package framework
11
 * @subpackage tests
12
 */
13
class DBHTMLTextTest extends SapphireTest {
14
15
	/**
16
	 * Test {@link HTMLText->LimitCharacters()}
17
	 */
18
	public function testLimitCharacters() {
19
		$cases = array(
20
			'The little brown fox jumped over the lazy cow.' => 'The little brown fox...',
21
			'<p>This is some text in a paragraph.</p>' => 'This is some text in...',
22
			'This text contains &amp; in it' => 'This text contains &amp;...'
23
		);
24
25
		foreach($cases as $originalValue => $expectedValue) {
26
			$textObj = new DBHTMLText('Test');
27
			$textObj->setValue($originalValue);
28
			$this->assertEquals($expectedValue, $textObj->LimitCharacters());
29
		}
30
	}
31
32
	public function testSummaryBasics() {
33
		$cases = array(
34
			'<h1>Should not take header</h1><p>Should take paragraph</p>' => 'Should take paragraph',
35
			'<p>Should strip <b>tags, but leave</b> text</p>' => 'Should strip tags, but leave text',
36
			'<p>Unclosed tags <br>should not phase it</p>' => 'Unclosed tags should not phase it',
37
			'<p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph',
38
			'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>'
39
				=> 'Second paragraph',
40
			'  <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>'
41
				=> 'Second paragraph',
42
			'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>'
43
				=> 'example text words hello',
44
		);
45
46
		foreach($cases as $originalValue => $expectedValue) {
47
			$textObj = new DBHTMLText('Test');
48
			$textObj->setValue($originalValue);
49
			$this->assertEquals($expectedValue, $textObj->Summary());
50
		}
51
	}
52
53
	public function testSummaryLimits() {
54
		$cases = array(
55
			'<p>A long paragraph should be cut off if limit is set</p>' => 'A long paragraph should be...',
56
			'<p>No matter <i>how many <b>tags</b></i> are in it</p>' => 'No matter how many tags...',
57
			'<p>A sentence is. nicer than hard limits</p>' => 'A sentence is.',
58
			'<p>But not. If it\'s too short</p>' => 'But not. If it\'s too...'
59
		);
60
61
		foreach($cases as $originalValue => $expectedValue) {
62
			$textObj = new DBHTMLText('Test');
63
			$textObj->setValue($originalValue);
64
			$this->assertEquals($expectedValue, $textObj->Summary(5, 3, '...'));
65
		}
66
	}
67
68
	public function testSummaryEndings() {
69
		$cases = array(
70
			'...', ' -> more', ''
71
		);
72
73
		$orig = '<p>Cut it off, cut it off</p>';
74
		$match = 'Cut it off, cut';
75
76
		foreach($cases as $add) {
77
			$textObj = new DBHTMLText();
78
			$textObj->setValue($orig);
79
			$this->assertEquals($match.$add, $textObj->Summary(4, 0, $add));
80
		}
81
	}
82
83
	public function testSummaryFlexTooBigShouldNotCauseError() {
84
		$orig = '<p>Cut it off, cut it off</p>';
85
		$match = 'Cut it off, cut';
86
87
		$textObj = new DBHTMLText();
88
		$textObj->setValue($orig);
89
		$this->assertEquals($match, $textObj->Summary(4, 10, ''));
90
	}
91
92
	public function testSummaryInvalidHTML() {
93
		$cases = array(
94
			'It\'s got a <p<> tag, but<p junk true>This doesn\'t <a id="boo">make</b class="wa"> < ><any< sense</p>'
95
				=> 'This doesn\'t make any',
96
			'This doesn\'t <a style="much horray= true>even</b> < ><have< a <i>p tag' => 'This doesn\'t even have'
97
		);
98
99
		foreach($cases as $orig => $match) {
100
			$textObj = new DBHTMLText();
101
			$textObj->setValue($orig);
102
			$this->assertEquals($match, $textObj->Summary(4, 0, ''));
103
		}
104
	}
105
106
	public function testFirstSentence() {
107
		$many = str_repeat('many ', 100);
108
		$cases = array(
109
			'<h1>should ignore</h1><p>First sentence. Second sentence.</p>' => 'First sentence.',
110
			'<h1>should ignore</h1><p>First Mr. sentence. Second sentence.</p>' => 'First Mr. sentence.',
111
			"<h1>should ignore</h1><p>Sentence with {$many}words. Second sentence.</p>"
112
				=> "Sentence with {$many}words.",
113
			'<p>This classic picture book features a repetitive format that lends itself to audience interaction.'.
114
			'&nbsp; Illustrator Eric Carle submitted new, bolder artwork for the 25th anniversary edition.</p>'
115
				=> 'This classic picture book features a repetitive format that lends itself to audience interaction.'
116
		);
117
118
		foreach($cases as $orig => $match) {
119
			$textObj = new DBHTMLText();
120
			$textObj->setValue($orig);
121
			$this->assertEquals($match, $textObj->FirstSentence());
122
		}
123
	}
124
125
	public function testRAW() {
126
		$data = DBField::create_field('HTMLText', 'This &amp; This');
127
		$this->assertEquals($data->RAW(), 'This &amp; This');
128
129
		$data = DBField::create_field('HTMLText', 'This & This');
130
		$this->assertEquals($data->RAW(), 'This & This');
131
	}
132
133
	public function testXML() {
134
		$data = DBField::create_field('HTMLText', 'This & This');
135
		$this->assertEquals($data->XML(), 'This &amp; This');
136
	}
137
138
	public function testHTML() {
139
		$data = DBField::create_field('HTMLText', 'This & This');
140
		$this->assertEquals($data->HTML(), 'This &amp; This');
141
	}
142
143
	public function testJS() {
144
		$data = DBField::create_field('HTMLText', '"this is a test"');
145
		$this->assertEquals($data->JS(), '\"this is a test\"');
146
	}
147
148
	public function testATT() {
149
		$data = DBField::create_field('HTMLText', '"this is a test"');
150
		$this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
151
	}
152
153
	function testExists() {
154
		$h = new DBHTMLText();
155
		$h->setValue("");
156
		$this->assertFalse($h->exists());
157
		$h->setValue("<p></p>");
158
		$this->assertFalse($h->exists());
159
		$h->setValue("<p> </p>");
160
		$this->assertFalse($h->exists());
161
		$h->setValue("<h2/>");
162
		$this->assertFalse($h->exists());
163
		$h->setValue("<h2></h2>");
164
		$this->assertFalse($h->exists());
165
166
		$h->setValue("something");
167
		$this->assertTrue($h->exists());
168
		$h->setValue("<img src=\"dummy.png\">");
169
		$this->assertTrue($h->exists());
170
		$h->setValue("<img src=\"dummy.png\"><img src=\"dummy.png\">");
171
		$this->assertTrue($h->exists());
172
		$h->setValue("<p><img src=\"dummy.png\"></p>");
173
		$this->assertTrue($h->exists());
174
175
		$h->setValue("<iframe src=\"http://www.google.com\"></iframe>");
176
		$this->assertTrue($h->exists());
177
		$h->setValue("<embed src=\"test.swf\">");
178
		$this->assertTrue($h->exists());
179
		$h->setValue("<object width=\"400\" height=\"400\" data=\"test.swf\"></object>");
180
		$this->assertTrue($h->exists());
181
182
183
		$h->setValue("<p>test</p>");
184
		$this->assertTrue($h->exists());
185
	}
186
187
	function testWhitelist() {
188
		$textObj = new DBHTMLText('Test', 'meta,link');
189
		$this->assertEquals(
190
			'<meta content="Keep"><link href="Also Keep">',
191
			$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'),
192
			'Removes any elements not in whitelist excluding text elements'
193
		);
194
195
		$textObj = new DBHTMLText('Test', 'meta,link,text()');
196
		$this->assertEquals(
197
			'<meta content="Keep"><link href="Also Keep">Keep Text',
198
			$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'),
199
			'Removes any elements not in whitelist including text elements'
200
		);
201
	}
202
203
	public function testShortCodeParsedInRAW() {
204
		$parser = ShortcodeParser::get('HTMLTextTest');
205
		$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tagName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
			return 'replaced';
207
		});
208
		ShortcodeParser::set_active('HTMLTextTest');
209
		/** @var DBHTMLText $field */
210
		$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
211
		$this->assertEquals('<p>replaced</p>', $field->RAW());
212
		$this->assertEquals('<p>replaced</p>', (string)$field);
213
214
		$field->setOptions(array(
215
			'shortcodes' => false,
216
		));
217
218
		$this->assertEquals('<p>[shortcode]</p>', $field->RAW());
219
		$this->assertEquals('<p>[shortcode]</p>', (string)$field);
220
221
222
		ShortcodeParser::set_active('default');
223
	}
224
225
	public function testShortCodeParsedInTemplateHelpers() {
226
		$parser = ShortcodeParser::get('HTMLTextTest');
227
		$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tagName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
			return 'Replaced short code with this. <a href="home">home</a>';
229
		});
230
		ShortcodeParser::set_active('HTMLTextTest');
231
		/** @var DBHTMLText $field */
232
		$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
233
234
		$this->assertEquals(
235
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
236
			$field->HTMLATT()
237
		);
238
		$this->assertEquals(
239
			'%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
240
			$field->URLATT()
241
		);
242
		$this->assertEquals(
243
			'%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
244
			$field->RAWURLATT()
245
		);
246
		$this->assertEquals(
247
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
248
			$field->ATT()
249
		);
250
		$this->assertEquals(
251
			'<p>Replaced short code with this. <a href="home">home</a></p>',
252
			$field->RAW()
253
		);
254
		$this->assertEquals(
255
			'\x3cp\x3eReplaced short code with this. \x3ca href=\"home\"\x3ehome\x3c/a\x3e\x3c/p\x3e',
256
			$field->JS()
257
		);
258
		$this->assertEquals(
259
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
260
			$field->HTML()
261
		);
262
		$this->assertEquals(
263
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
264
			$field->XML()
265
		);
266
		$this->assertEquals(
267
			'Repl...',
268
			$field->LimitCharacters(4, '...')
269
		);
270
		$this->assertEquals(
271
			'Replaced...',
272
			$field->LimitCharactersToClosestWord(10, '...')
273
		);
274
		$this->assertEquals(
275
			'Replaced...',
276
			$field->LimitWordCount(1, '...')
277
		);
278
		$this->assertEquals(
279
			'<p>replaced short code with this. <a href="home">home</a></p>',
280
			$field->LowerCase()
281
		);
282
		$this->assertEquals(
283
			'<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>',
284
			$field->UpperCase()
285
		);
286
		$this->assertEquals(
287
			'Replaced short code with this. home',
288
			$field->NoHTML()
289
		);
290
		Config::nest();
291
		Config::inst()->update('Director', 'alternate_base_url', 'http://example.com/');
292
		$this->assertEquals(
293
			'<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>',
294
			$field->AbsoluteLinks()
295
		);
296
		Config::unnest();
297
		$this->assertEquals(
298
			'Replaced short code with this.',
299
			$field->LimitSentences(1)
300
		);
301
		$this->assertEquals(
302
			'Replaced short code with this.',
303
			$field->FirstSentence()
304
		);
305
		$this->assertEquals(
306
			'Replaced short...',
307
			$field->Summary(2)
308
		);
309
		$this->assertEquals(
310
			'Replaced short code with...',
311
			$field->BigSummary(4)
312
		);
313
		$this->assertEquals(
314
			'Replaced short code with this. home[home]',
315
			$field->FirstParagraph()
316
		);
317
		$this->assertEquals(
318
			'Replaced <span class="highlight">short</span> <span class="highlight">code</span> with this. home',
319
			$field->ContextSummary(500, 'short code')
320
		);
321
322
		ShortcodeParser::set_active('default');
323
	}
324
}
325