Completed
Push — master ( 03a324...633eb0 )
by Damian
21s
created

DBHTMLTextTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 190
rs 10

14 Methods

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