Completed
Pull Request — master (#5653)
by Damian
12:15
created

DBTextTest::testBigSummaryPlain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
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\DBText;
6
use SilverStripe\ORM\FieldType\DBField;
7
8
9
/**
10
 * Tests parsing and summary methods on DBText
11
 *
12
 * @package framework
13
 * @subpackage tests
14
 */
15
class DBTextTest extends SapphireTest {
16
17
	/**
18
	 * Test {@link Text->LimitCharacters()}
19
	 */
20
	public function providerLimitCharacters()
21
	{
22
		// Plain text values always encoded safely
23
		// HTML stored in non-html fields is treated literally.
24
		return [
25
			['The little brown fox jumped over the lazy cow.', 'The little brown fox...'],
26
			['<p>Short & Sweet</p>', '&lt;p&gt;Short &amp; Sweet&lt;/p&gt;'],
27
			['This text contains &amp; in it', 'This text contains &amp;...'],
28
		];
29
	}
30
31
	/**
32
	 * Test {@link Text->LimitCharacters()}
33
	 * @dataProvider providerLimitCharacters
34
	 * @param string $originalValue
35
	 * @param string $expectedValue
36
	 */
37
	public function testLimitCharacters($originalValue, $expectedValue) {
38
		$textObj = DBField::create_field('Text', $originalValue);
39
		$result = $textObj->obj('LimitCharacters')->forTemplate();
40
		$this->assertEquals($expectedValue, $result);
41
	}
42
43
	/**
44
	 * @return array
45
	 */
46
	public function providerLimitCharactersToClosestWord()
47
	{
48
		return [
49
			// Standard words limited, ellipsis added if truncated
50
			['Lorem ipsum dolor sit amet', 24, 'Lorem ipsum dolor sit...'],
51
52
			// Complete words less than the character limit don't get truncated, ellipsis not added
53
			['Lorem ipsum', 24, 'Lorem ipsum'],
54
			['Lorem', 24, 'Lorem'],
55
			['', 24, ''],    // No words produces nothing!
56
57
			// Special characters are encoded safely
58
			['Nice & Easy', 24, 'Nice &amp; Easy'],
59
60
			// HTML stored in non-html fields is treated literally.
61
			// If storing HTML you should use DBHTMLText instead
62
			['<p>Lorem ipsum dolor sit amet</p>', 24, '&lt;p&gt;Lorem ipsum dolor...'],
63
			['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, '&lt;p&gt;&lt;span&gt;Lorem ipsum...'],
64
			['<p>Lorem ipsum</p>', 24, '&lt;p&gt;Lorem ipsum&lt;/p&gt;'],
65
			['Lorem &amp; ipsum dolor sit amet', 24, 'Lorem &amp;amp; ipsum dolor...']
66
		];
67
	}
68
69
	/**
70
	 * Test {@link Text->LimitCharactersToClosestWord()}
71
	 * @dataProvider providerLimitCharactersToClosestWord
72
	 *
73
	 * @param string $originalValue Raw string input
74
	 * @param int $limit
75
	 * @param string $expectedValue Expected template value
76
	 */
77
	public function testLimitCharactersToClosestWord($originalValue, $limit, $expectedValue) {
78
		$textObj = DBField::create_field('Text', $originalValue);
79
		$result = $textObj->obj('LimitCharactersToClosestWord', [$limit])->forTemplate();
80
		$this->assertEquals($expectedValue, $result);
81
	}
82
83
	/**
84
	 * Test {@link Text->LimitWordCount()}
85
	 */
86
	public function providerLimitWordCount() {
87
		return [
88
			// Standard words limited, ellipsis added if truncated
89
			['The little brown fox jumped over the lazy cow.', 3, 'The little brown...'],
90
			[' This text has white space around the ends ', 3, 'This text has...'],
91
92
			// Words less than the limt word count don't get truncated, ellipsis not added
93
			['Two words', 3, 'Two words'],	// Two words shouldn't have an ellipsis
94
			['These three words', 3, 'These three words'], // Three words shouldn't have an ellipsis
95
			['One', 3, 'One'],	// Neither should one word
96
			['', 3, ''],	// No words produces nothing!
97
98
			// Text with special characters
99
			['Nice & Easy', 3, 'Nice &amp; Easy'],
100
			['One & Two & Three', 3, 'One &amp; Two...'],
101
102
			// HTML stored in non-html fields is treated literally.
103
			// If storing HTML you should use DBHTMLText instead
104
			['<p>Text inside a paragraph tag should also work</p>', 3, '&lt;p&gt;Text inside a...'],
105
			['<p>Two words</p>', 3, '&lt;p&gt;Two words&lt;/p&gt;'],
106
		];
107
	}
108
109
	/**
110
	 * Test {@link DBText->LimitWordCount()}
111
	 * @dataProvider providerLimitWordCount
112
	 *
113
	 * @param string $originalValue Raw string input
114
	 * @param int $limit Number of words
115
	 * @param string $expectedValue Expected template value
116
	 */
117
	public function testLimitWordCount($originalValue, $limit, $expectedValue) {
118
		$textObj = DBField::create_field('Text', $originalValue);
119
		$result = $textObj->obj('LimitWordCount', [$limit])->forTemplate();
120
		$this->assertEquals($expectedValue, $result);
121
	}
122
123
	/**
124
	 */
125
	public function providerLimitSentences()
126
	{
127
		return [
128
			['', 2, ''],
129
			['First sentence.', 2, 'First sentence.'],
130
			['First sentence. Second sentence.', 2, 'First sentence. Second sentence.'],
131
132
			// HTML stored in non-html fields is treated literally.
133
			// If storing HTML you should use DBHTMLText instead
134
			['<p>First sentence.</p>', 2, '&lt;p&gt;First sentence.&lt;/p&gt;'],
135
			['<p>First sentence. Second sentence. Third sentence</p>', 2, '&lt;p&gt;First sentence. Second sentence.'],
136
		];
137
	}
138
139
	/**
140
	 * Test {@link DBText->LimitSentences()}
141
	 *
142
	 * @dataProvider providerLimitSentences
143
	 * @param string $originalValue
144
	 * @param int $limit Number of sentences
145
	 * @param string $expectedValue Expected template value
146
     */
147
	public function testLimitSentences($originalValue, $limit, $expectedValue) {
148
		$textObj = DBField::create_field('Text', $originalValue);
149
		$result = $textObj->obj('LimitSentences', [$limit])->forTemplate();
150
		$this->assertEquals($expectedValue, $result);
151
	}
152
153
	public function providerFirstSentence()
154
	{
155
		return [
156
			['', ''],
157
			['First sentence.', 'First sentence.'],
158
			['First sentence. Second sentence', 'First sentence.'],
159
			['First sentence? Second sentence', 'First sentence?'],
160
			['First sentence! Second sentence', 'First sentence!'],
161
162
			// HTML stored in non-html fields is treated literally.
163
			// If storing HTML you should use DBHTMLText instead
164
			['<br />First sentence.', '&lt;br /&gt;First sentence.'],
165
			['<p>First sentence. Second sentence. Third sentence</p>', '&lt;p&gt;First sentence.'],
166
		];
167
	}
168
169
	/**
170
	 * @dataProvider providerFirstSentence
171
	 * @param string $originalValue
172
	 * @param string $expectedValue
173
     */
174
	public function testFirstSentence($originalValue, $expectedValue) {
175
		$textObj = DBField::create_field('Text', $originalValue);
176
		$result = $textObj->obj('FirstSentence')->forTemplate();
177
		$this->assertEquals($expectedValue, $result);
178
	}
179
180
	/**
181
	 * each test is in the format input, charactere limit, highlight, expected output
182
	 *
183
	 * @return array
184
	 */
185
	public function providerContextSummary()
186
	{
187
		return [
188
			[
189
				'This is some text. It is a test',
190
				20,
191
				'test',
192
				'... text. It is a <span class="highlight">test</span>'
193
			],
194
			[
195
				// Retains case of original string
196
				'This is some test text. Test test what if you have multiple keywords.',
197
				50,
198
				'some test',
199
				'This is <span class="highlight">some</span> <span class="highlight">test</span> text.'
200
				. ' <span class="highlight">Test</span> <span class="highlight">test</span> what if you have...'
201
			],
202
			[
203
				'Here is some text & HTML included',
204
				20,
205
				'html',
206
				'... text &amp; <span class="highlight">HTML</span> inc...'
207
			],
208
			[
209
				'A dog ate a cat while looking at a Foobar',
210
				100,
211
				'a',
212
				// test that it does not highlight too much (eg every a)
213
				'A dog ate a cat while looking at a Foobar',
214
			],
215
			[
216
				'A dog ate a cat while looking at a Foobar',
217
				100,
218
				'ate',
219
				// it should highlight 3 letters or more.
220
				'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
221
			]
222
		];
223
	}
224
225
	/**
226
	 * @dataProvider providerContextSummary
227
	 * @param string $originalValue Input
228
	 * @param int $limit Numer of characters
229
	 * @param string $keywords Keywords to highlight
230
	 * @param string $expectedValue Expected output (XML encoded safely)
231
     */
232
	public function testContextSummary($originalValue, $limit, $keywords, $expectedValue)
233
	{
234
		$text = DBField::create_field('Text', $originalValue);
235
		$result = $text->obj('ContextSummary', [$limit, $keywords])->forTemplate();
236
		// it should highlight 3 letters or more.
237
		$this->assertEquals($expectedValue, $result);
238
	}
239
240
	public function testRAW() {
241
		$data = DBField::create_field('Text', 'This &amp; This');
242
		$this->assertEquals($data->RAW(), 'This &amp; This');
243
	}
244
245
	public function testXML() {
246
		$data = DBField::create_field('Text', 'This & This');
247
		$this->assertEquals($data->XML(), 'This &amp; This');
248
	}
249
250
	public function testHTML() {
251
		$data = DBField::create_field('Text', 'This & This');
252
		$this->assertEquals($data->HTML(), 'This &amp; This');
253
	}
254
255
	public function testJS() {
256
		$data = DBField::create_field('Text', '"this is a test"');
257
		$this->assertEquals($data->JS(), '\"this is a test\"');
258
	}
259
260
	public function testATT() {
261
		$data = DBField::create_field('Text', '"this is a test"');
262
		$this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
263
	}
264
}
265