comparison cake/tests/cases/libs/view/helpers/text.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * TextHelperTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs.view.helpers
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Helper', 'Text');
21
22 /**
23 * TextHelperTest class
24 *
25 * @package cake
26 * @subpackage cake.tests.cases.libs.view.helpers
27 */
28 class TextHelperTest extends CakeTestCase {
29
30 /**
31 * helper property
32 *
33 * @var mixed null
34 * @access public
35 */
36 var $helper = null;
37
38 /**
39 * setUp method
40 *
41 * @access public
42 * @return void
43 */
44 function setUp() {
45 $this->Text = new TextHelper();
46 }
47
48 /**
49 * tearDown method
50 *
51 * @access public
52 * @return void
53 */
54 function tearDown() {
55 unset($this->Text);
56 }
57
58 /**
59 * testTruncate method
60 *
61 * @access public
62 * @return void
63 */
64 function testTruncate() {
65 $text1 = 'The quick brown fox jumps over the lazy dog';
66 $text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
67 $text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
68 $text4 = '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?';
69 $text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
70 $text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
71 $text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
72 $text8 = 'Vive la R'.chr(195).chr(169).'publique de France';
73 $text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
74
75 $this->assertIdentical($this->Text->truncate($text1, 15), 'The quick br...');
76 $this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
77 $this->assertIdentical($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
78 $this->assertIdentical($this->Text->truncate($text2, 10), 'Heiz&ou...');
79 $this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false)), '...');
80 $this->assertIdentical($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
81 $this->assertIdentical($this->Text->truncate($text4, 15), '<img src="my...');
82 $this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
83 $this->assertIdentical($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
84 $this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
85 $this->assertIdentical($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
86 $this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...');
87 $this->assertIdentical($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake...</b>');
88 $this->assertIdentical($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
89 $this->assertIdentical($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
90 $this->assertIdentical($this->Text->truncate($text4, 90, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Grea...');
91 $this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
92 $this->assertIdentical($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5);
93 $this->assertIdentical($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
94 $this->assertIdentical($this->Text->truncate($text7, 255), $text7);
95 $this->assertIdentical($this->Text->truncate($text7, 15), 'El moño está...');
96 $this->assertIdentical($this->Text->truncate($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
97 $this->assertIdentical($this->Text->truncate($text9, 10), 'НОПРСТУ...');
98 }
99 /**
100 * testHighlight method
101 *
102 * @access public
103 * @return void
104 */
105 function testHighlight() {
106 $text = 'This is a test text';
107 $phrases = array('This', 'text');
108 $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
109 $expected = '<b>This</b> is a test <b>text</b>';
110 $this->assertEqual($expected, $result);
111
112 $text = 'This is a test text';
113 $phrases = null;
114 $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
115 $this->assertEqual($result, $text);
116
117 $text = 'Ich saß in einem Café am Übergang';
118 $expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
119 $phrases = array('saß', 'café', 'übergang');
120 $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
121 $this->assertEqual($result, $expected);
122 }
123
124 /**
125 * testHighlightHtml method
126 *
127 * @access public
128 * @return void
129 */
130 function testHighlightHtml() {
131 $text1 = '<p>strongbow isn&rsquo;t real cider</p>';
132 $text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
133 $text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
134 $text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
135 $options = array('format' => '<b>\1</b>', 'html' => true);
136
137 $expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
138 $this->assertEqual($this->Text->highlight($text1, 'strong', $options), $expected);
139
140 $expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
141 $this->assertEqual($this->Text->highlight($text2, 'strong', $options), $expected);
142
143 $this->assertEqual($this->Text->highlight($text3, 'strong', $options), $text3);
144
145 $this->assertEqual($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
146
147 $expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
148 $this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), $options), $expected);
149 }
150
151 /**
152 * testHighlightMulti method
153 *
154 * @access public
155 * @return void
156 */
157 function testHighlightMulti() {
158 $text = 'This is a test text';
159 $phrases = array('This', 'text');
160 $result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
161 $expected = '<b>This</b> is a test <em>text</em>';
162 $this->assertEqual($expected, $result);
163
164 }
165
166 /**
167 * testStripLinks method
168 *
169 * @access public
170 * @return void
171 */
172 function testStripLinks() {
173 $text = 'This is a test text';
174 $expected = 'This is a test text';
175 $result = $this->Text->stripLinks($text);
176 $this->assertEqual($expected, $result);
177
178 $text = 'This is a <a href="#">test</a> text';
179 $expected = 'This is a test text';
180 $result = $this->Text->stripLinks($text);
181 $this->assertEqual($expected, $result);
182
183 $text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
184 $expected = 'This <strong>is</strong> a test text';
185 $result = $this->Text->stripLinks($text);
186 $this->assertEqual($expected, $result);
187
188 $text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
189 $expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
190 $result = $this->Text->stripLinks($text);
191 $this->assertEqual($expected, $result);
192 }
193
194 /**
195 * testAutoLink method
196 *
197 * @access public
198 * @return void
199 */
200 function testAutoLink() {
201 $text = 'This is a test text';
202 $expected = 'This is a test text';
203 $result = $this->Text->autoLink($text);
204 $this->assertEqual($expected, $result);
205
206 $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
207 $result = $this->Text->autoLink($text);
208 $expected = 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL and <a href="mailto:test@cakephp\.org">test@cakephp\.org</a> email address';
209 $this->assertPattern('#^' . $expected . '$#', $result);
210
211 $text = 'This is a test text with URL http://www.cakephp.org';
212 $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
213 $result = $this->Text->autoLink($text);
214 $this->assertEqual($result, $expected);
215
216 $text = 'This is a test text with URL http://www.cakephp.org and some more text';
217 $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
218 $result = $this->Text->autoLink($text);
219 $this->assertEqual($result, $expected);
220
221 $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
222 $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
223 $result = $this->Text->autoLink($text);
224 $this->assertEqual($result, $expected);
225
226 $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
227 $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
228 $result = $this->Text->autoLink($text);
229 $this->assertEqual($result, $expected);
230
231 $text = 'This is a test text with URL http://www.cakephp.org';
232 $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
233 $result = $this->Text->autoLink($text, array('class'=>'link'));
234 $this->assertEqual($result, $expected);
235
236 $text = 'This is a test text with URL http://www.cakephp.org';
237 $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
238 $result = $this->Text->autoLink($text, array('class'=>'link', 'id'=>'MyLink'));
239 $this->assertEqual($result, $expected);
240 }
241
242 /**
243 * testAutoLinkUrls method
244 *
245 * @access public
246 * @return void
247 */
248 function testAutoLinkUrls() {
249 $text = 'This is a test text';
250 $expected = 'This is a test text';
251 $result = $this->Text->autoLinkUrls($text);
252 $this->assertEqual($expected, $result);
253
254 $text = 'This is a test that includes (www.cakephp.org)';
255 $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
256 $result = $this->Text->autoLinkUrls($text);
257 $this->assertEqual($expected, $result);
258
259 $text = 'Text with a partial www.cakephp.org URL';
260 $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
261 $result = $this->Text->autoLinkUrls($text);
262 $this->assertPattern('#^' . $expected . '$#', $result);
263
264 $text = 'Text with a partial www.cakephp.org URL';
265 $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
266 $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
267 $this->assertPattern('#^' . $expected . '$#', $result);
268
269 $text = 'Text with a partial WWW.cakephp.org URL';
270 $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
271 $result = $this->Text->autoLinkUrls($text);
272 $this->assertPattern('#^' . $expected . '$#', $result);
273
274 $text = 'Text with a partial WWW.cakephp.org &copy; URL';
275 $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
276 $result = $this->Text->autoLinkUrls($text, array('escape' => false));
277 $this->assertPattern('#^' . $expected . '$#', $result);
278
279 $text = 'Text with a url www.cot.ag/cuIb2Q and more';
280 $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
281 $result = $this->Text->autoLinkUrls($text);
282 $this->assertEqual($expected, $result);
283
284 $text = 'Text with a url http://www.does--not--work.com and more';
285 $expected = 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more';
286 $result = $this->Text->autoLinkUrls($text);
287 $this->assertEqual($expected, $result);
288
289 $text = 'Text with a url http://www.not--work.com and more';
290 $expected = 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more';
291 $result = $this->Text->autoLinkUrls($text);
292 $this->assertEqual($expected, $result);
293 }
294
295 /**
296 * testAutoLinkEmails method
297 *
298 * @access public
299 * @return void
300 */
301 function testAutoLinkEmails() {
302 $text = 'This is a test text';
303 $expected = 'This is a test text';
304 $result = $this->Text->autoLinkUrls($text);
305 $this->assertEqual($expected, $result);
306
307 $text = 'Text with email@example.com address';
308 $expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address';
309 $result = $this->Text->autoLinkEmails($text);
310 $this->assertPattern('#^' . $expected . '$#', $result);
311
312 $text = "Text with o'hare._-bob@example.com address";
313 $expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
314 $result = $this->Text->autoLinkEmails($text);
315 $this->assertEqual($expected, $result);
316
317 $text = 'Text with email@example.com address';
318 $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
319 $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
320 $this->assertPattern('#^' . $expected . '$#', $result);
321 }
322
323 /**
324 * test invalid email addresses.
325 *
326 * @return void
327 */
328 function testAutoLinkEmailInvalid() {
329 $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
330 $expected = 'this is a myaddress@gmx-de test';
331 $this->assertEqual($expected, $result);
332 }
333
334 /**
335 * testHighlightCaseInsensitivity method
336 *
337 * @access public
338 * @return void
339 */
340 function testHighlightCaseInsensitivity() {
341 $text = 'This is a Test text';
342 $expected = 'This is a <b>Test</b> text';
343
344 $result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
345 $this->assertEqual($expected, $result);
346
347 $result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
348 $this->assertEqual($expected, $result);
349 }
350
351 /**
352 * testExcerpt method
353 *
354 * @access public
355 * @return void
356 */
357 function testExcerpt() {
358 $text = 'This is a phrase with test text to play with';
359
360 $expected = '...with test text...';
361 $result = $this->Text->excerpt($text, 'test', 9, '...');
362 $this->assertEqual($expected, $result);
363
364 $expected = 'This is a...';
365 $result = $this->Text->excerpt($text, 'not_found', 9, '...');
366 $this->assertEqual($expected, $result);
367
368 $expected = 'This is a phras...';
369 $result = $this->Text->excerpt($text, null, 9, '...');
370 $this->assertEqual($expected, $result);
371
372 $expected = $text;
373 $result = $this->Text->excerpt($text, null, 200, '...');
374 $this->assertEqual($expected, $result);
375
376 $expected = '...phrase...';
377 $result = $this->Text->excerpt($text, 'phrase', 2, '...');
378 $this->assertEqual($expected, $result);
379
380 $phrase = 'This is a phrase with test';
381 $expected = $text;
382 $result = $this->Text->excerpt($text, $phrase, strlen($phrase) + 3, '...');
383 $this->assertEqual($expected, $result);
384
385 $phrase = 'This is a phrase with text';
386 $expected = $text;
387 $result = $this->Text->excerpt($text, $phrase, 10, '...');
388 $this->assertEqual($expected, $result);
389 }
390
391 /**
392 * testExcerptCaseInsensitivity method
393 *
394 * @access public
395 * @return void
396 */
397 function testExcerptCaseInsensitivity() {
398 $text = 'This is a phrase with test text to play with';
399
400 $expected = '...with test text...';
401 $result = $this->Text->excerpt($text, 'TEST', 9, '...');
402 $this->assertEqual($expected, $result);
403
404 $expected = 'This is a...';
405 $result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
406 $this->assertEqual($expected, $result);
407 }
408
409 /**
410 * testListGeneration method
411 *
412 * @access public
413 * @return void
414 */
415 function testListGeneration() {
416 $result = $this->Text->toList(array());
417 $this->assertEqual($result, '');
418
419 $result = $this->Text->toList(array('One'));
420 $this->assertEqual($result, 'One');
421
422 $result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
423 $this->assertEqual($result, 'Larry, Curly and Moe');
424
425 $result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
426 $this->assertEqual($result, 'Dusty, Lucky y Ned');
427
428 $result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
429 $this->assertEqual($result, 'Dusty, Lucky y Ned');
430
431 $result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
432 $this->assertEqual($result, 'Dusty + Lucky and Ned');
433
434 $result = $this->Text->toList(array( 'name1' => 'Dusty', 'name2' => 'Lucky'));
435 $this->assertEqual($result, 'Dusty and Lucky');
436
437 $result = $this->Text->toList(array( 'test_0' => 'banana', 'test_1' => 'apple', 'test_2' => 'lemon'));
438 $this->assertEqual($result, 'banana, apple and lemon');
439 }
440 }