Completed
Pull Request — master (#4551)
by Sam
10:45
created

DBTextTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testLimitCharacters() 0 12 2
B testLimitCharactersToClosestWord() 0 25 2
A testLimitWordCount() 0 23 2
A testLimitWordCountXML() 0 14 2
A testLimitSentences() 0 18 2
A testFirstSentance() 0 20 2
A testBigSummaryPlain() 0 14 2
A testBigSummary() 0 14 2
B testContextSummary() 0 40 1
A testRAW() 0 4 1
A testXML() 0 4 1
A testHTML() 0 4 1
A testJS() 0 4 1
A testATT() 0 4 1
1
<?php
2
3
use SilverStripe\Model\FieldType\DBField;
4
use SilverStripe\Model\FieldType\DBText;
5
6
/**
7
 * @package framework
8
 * @subpackage tests
9
 */
10
class DBTextTest extends SapphireTest {
11
12
	/**
13
	 * Test {@link Text->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>' => '<p>This is some text...'
19
		);
20
21
		foreach($cases as $originalValue => $expectedValue) {
22
			$textObj = new DBText('Test');
23
			$textObj->setValue($originalValue);
24
			$this->assertEquals($expectedValue, $textObj->LimitCharacters());
25
		}
26
	}
27
28
	/**
29
	 * Test {@link Text->LimitCharactersToClosestWord()}
30
	 */
31
	public function testLimitCharactersToClosestWord() {
32
		$cases = array(
33
			/* Standard words limited, ellipsis added if truncated */
34
			'Lorem ipsum dolor sit amet' => 'Lorem ipsum dolor sit...',
35
36
			/* Complete words less than the character limit don't get truncated, ellipsis not added */
37
			'Lorem ipsum' => 'Lorem ipsum',
38
			'Lorem' => 'Lorem',
39
			'' => '',	// No words produces nothing!
40
41
			/* HTML tags get stripped out, leaving the raw text */
42
			'<p>Lorem ipsum dolor sit amet</p>' => 'Lorem ipsum dolor sit...',
43
			'<p><span>Lorem ipsum dolor sit amet</span></p>' => 'Lorem ipsum dolor sit...',
44
			'<p>Lorem ipsum</p>' => 'Lorem ipsum',
45
46
			/* HTML entities are treated as a single character */
47
			'Lorem &amp; ipsum dolor sit amet' => 'Lorem &amp; ipsum dolor...'
48
		);
49
50
		foreach($cases as $originalValue => $expectedValue) {
51
			$textObj = new DBText('Test');
52
			$textObj->setValue($originalValue);
53
			$this->assertEquals($expectedValue, $textObj->LimitCharactersToClosestWord(24));
54
		}
55
	}
56
57
	/**
58
	 * Test {@link Text->LimitWordCount()}
59
	 */
60
	public function testLimitWordCount() {
61
		$cases = array(
62
			/* Standard words limited, ellipsis added if truncated */
63
			'The little brown fox jumped over the lazy cow.' => 'The little brown...',
64
			' This text has white space around the ends ' => 'This text has...',
65
66
			/* Words less than the limt word count don't get truncated, ellipsis not added */
67
			'Two words' => 'Two words',	// Two words shouldn't have an ellipsis
68
			'One' => 'One',	// Neither should one word
69
			'' => '',	// No words produces nothing!
70
71
			/* HTML tags get stripped out, leaving the raw text */
72
			'<p>Text inside a paragraph tag should also work</p>' => 'Text inside a...',
73
			'<p><span>Text nested inside another tag should also work</span></p>' => 'Text nested inside...',
74
			'<p>Two words</p>' => 'Two words'
75
		);
76
77
		foreach($cases as $originalValue => $expectedValue) {
78
			$textObj = new DBText('Test');
79
			$textObj->setValue($originalValue);
80
			$this->assertEquals($expectedValue, $textObj->LimitWordCount(3));
81
		}
82
	}
83
84
	/**
85
	 * Test {@link Text->LimitWordCountXML()}
86
	 */
87
	public function testLimitWordCountXML() {
88
		$cases = array(
89
			'<p>Stuff & stuff</p>' => 'Stuff &amp;...',
90
			"Stuff\nBlah Blah Blah" => "Stuff\nBlah Blah...",
91
			"Stuff<Blah Blah" => "Stuff&lt;Blah Blah",
92
			"Stuff>Blah Blah" => "Stuff&gt;Blah Blah"
93
		);
94
95
		foreach($cases as $originalValue => $expectedValue) {
96
			$textObj = new DBText('Test');
97
			$textObj->setValue($originalValue);
98
			$this->assertEquals($expectedValue, $textObj->LimitWordCountXML(3));
99
		}
100
	}
101
102
	/**
103
	 * Test {@link Text->LimitSentences()}
104
	 */
105
	public function testLimitSentences() {
106
		$cases = array(
107
			'' => '',
108
			'First sentence.' => 'First sentence.',
109
			'First sentence. Second sentence' => 'First sentence. Second sentence.',
110
			'<p>First sentence.</p>' => 'First sentence.',
111
			'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence. Second sentence.',
112
			'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence. Second sentence.',
113
			'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>'
114
				=> 'First sentence. Second sentence.'
115
		);
116
117
		foreach($cases as $originalValue => $expectedValue) {
118
			$textObj = new DBText('Test');
119
			$textObj->setValue($originalValue);
120
			$this->assertEquals($expectedValue, $textObj->LimitSentences(2));
121
		}
122
	}
123
124
	public function testFirstSentance() {
125
		$cases = array(
126
			'' => '',
127
			'First sentence.' => 'First sentence.',
128
			'First sentence. Second sentence' => 'First sentence.',
129
			'First sentence? Second sentence' => 'First sentence?',
130
			'First sentence! Second sentence' => 'First sentence!',
131
			'<p>First sentence.</p>' => 'First sentence.',
132
			'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence.',
133
			'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence.',
134
			'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>'
135
				=> 'First sentence.'
136
		);
137
138
		foreach($cases as $originalValue => $expectedValue) {
139
			$textObj = new DBText('Test');
140
			$textObj->setValue($originalValue);
141
			$this->assertEquals($expectedValue, $textObj->FirstSentence());
142
		}
143
	}
144
145
	/**
146
	 * Test {@link Text->BigSummary()}
147
	 */
148
	public function testBigSummaryPlain() {
149
		$cases = array(
150
			'<p>This text has multiple sentences. Big Summary uses this to split sentences up.</p>'
151
				=> 'This text has multiple...',
152
			'This text does not have multiple sentences' => 'This text does not...',
153
			'Very short' => 'Very short',
154
			'' => ''
155
		);
156
157
		foreach($cases as $originalValue => $expectedValue) {
158
			$textObj = DBField::create_field('Text', $originalValue);
159
			$this->assertEquals($expectedValue, $textObj->BigSummary(4, true));
160
		}
161
	}
162
163
	/**
164
	 * Test {@link Text->BigSummary()}
165
	 */
166
	public function testBigSummary() {
167
		$cases = array(
168
			'<strong>This</strong> text has multiple sentences. Big Summary uses this to split sentences up.</p>'
169
				=> '<strong>This</strong> text has multiple...',
170
			'This text does not have multiple sentences' => 'This text does not...',
171
			'Very short' => 'Very short',
172
			'' => ''
173
		);
174
175
		foreach($cases as $originalValue => $expectedValue) {
176
			$textObj = DBField::create_field('Text', $originalValue);
177
			$this->assertEquals($expectedValue, $textObj->BigSummary(4, false));
178
		}
179
	}
180
181
	public function testContextSummary() {
182
		$testString1 = '<p>This is some text. It is a test</p>';
183
		$testKeywords1 = 'test';
184
185
		$testString2 = '<p>This is some test text. Test test what if you have multiple keywords.</p>';
186
		$testKeywords2 = 'some test';
187
188
		$testString3 = '<p>A dog ate a cat while looking at a Foobar</p>';
189
		$testKeyword3 = 'a';
190
		$testKeyword3a = 'ate';
191
192
		$textObj = DBField::create_field('Text', $testString1, 'Text');
193
194
		$this->assertEquals(
195
			'... text. It is a <span class="highlight">test</span>...',
196
			$textObj->ContextSummary(20, $testKeywords1)
197
		);
198
199
		$textObj->setValue($testString2);
200
201
		$this->assertEquals(
202
			'This is <span class="highlight">some</span> <span class="highlight">test</span> text.'
203
				. ' <span class="highlight">test</span> <span class="highlight">test</span> what if you have...',
204
			$textObj->ContextSummary(50, $testKeywords2)
205
		);
206
207
		$textObj->setValue($testString3);
208
209
		// test that it does not highlight too much (eg every a)
210
		$this->assertEquals(
211
			'A dog ate a cat while looking at a Foobar',
212
			$textObj->ContextSummary(100, $testKeyword3)
213
		);
214
215
		// it should highlight 3 letters or more.
216
		$this->assertEquals(
217
			'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
218
			$textObj->ContextSummary(100, $testKeyword3a)
219
		);
220
	}
221
222
	public function testRAW() {
223
		$data = DBField::create_field('Text', 'This &amp; This');
224
		$this->assertEquals($data->RAW(), 'This &amp; This');
225
	}
226
227
	public function testXML() {
228
		$data = DBField::create_field('Text', 'This & This');
229
		$this->assertEquals($data->XML(), 'This &amp; This');
230
	}
231
232
	public function testHTML() {
233
		$data = DBField::create_field('Text', 'This & This');
234
		$this->assertEquals($data->HTML(), 'This &amp; This');
235
	}
236
237
	public function testJS() {
238
		$data = DBField::create_field('Text', '"this is a test"');
239
		$this->assertEquals($data->JS(), '\"this is a test\"');
240
	}
241
242
	public function testATT() {
243
		$data = DBField::create_field('Text', '"this is a test"');
244
		$this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
245
	}
246
}
247