Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * CakeTextTest file
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Utility
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakeText', 'Utility');
20
 
21
/**
22
 * CakeText Tests
23
 *
24
 * @package Cake.Test.Case.Utility
25
 * @coversDefaultClass CakeText
26
 */
27
class CakeTextTest extends CakeTestCase {
28
 
29
/**
30
 * Setup object under test
31
 *
32
 * @return void
33
 */
34
	public function setUp() {
35
		parent::setUp();
36
		$this->Text = new CakeText();
37
	}
38
 
39
/**
40
 * Tear down object under test
41
 *
42
 * @return void
43
 */
44
	public function tearDown() {
45
		parent::tearDown();
46
		unset($this->Text);
47
	}
48
 
49
/**
50
 * testUuidGeneration method
51
 *
52
 * @return void
53
 * @covers ::uuid
54
 */
55
	public function testUuidGeneration() {
56
		$result = CakeText::uuid();
57
		$pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
58
		$match = (bool)preg_match($pattern, $result);
59
		$this->assertTrue($match);
60
	}
61
 
62
/**
63
 * testMultipleUuidGeneration method
64
 *
65
 * @return void
66
 * @covers ::uuid
67
 */
68
	public function testMultipleUuidGeneration() {
69
		$check = array();
70
		$count = mt_rand(10, 1000);
71
		$pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
72
 
73
		for ($i = 0; $i < $count; $i++) {
74
			$result = CakeText::uuid();
75
			$match = (bool)preg_match($pattern, $result);
76
			$this->assertTrue($match);
77
			$this->assertFalse(in_array($result, $check));
78
			$check[] = $result;
79
		}
80
	}
81
 
82
/**
83
 * testInsert method
84
 *
85
 * @return void
86
 * @covers ::insert
87
 */
88
	public function testInsert() {
89
		$string = 'some string';
90
		$expected = 'some string';
91
		$result = CakeText::insert($string, array());
92
		$this->assertEquals($expected, $result);
93
 
94
		$string = '2 + 2 = :sum. Cake is :adjective.';
95
		$expected = '2 + 2 = 4. Cake is yummy.';
96
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
97
		$this->assertEquals($expected, $result);
98
 
99
		$string = '2 + 2 = %sum. Cake is %adjective.';
100
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
101
		$this->assertEquals($expected, $result);
102
 
103
		$string = '2 + 2 = 2sum2. Cake is 9adjective9.';
104
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])%s\\1/'));
105
		$this->assertEquals($expected, $result);
106
 
107
		$string = '2 + 2 = 12sum21. Cake is 23adjective45.';
108
		$expected = '2 + 2 = 4. Cake is 23adjective45.';
109
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])([\d])%s\\2\\1/'));
110
		$this->assertEquals($expected, $result);
111
 
112
		$string = ':web :web_site';
113
		$expected = 'www http';
114
		$result = CakeText::insert($string, array('web' => 'www', 'web_site' => 'http'));
115
		$this->assertEquals($expected, $result);
116
 
117
		$string = '2 + 2 = <sum. Cake is <adjective>.';
118
		$expected = '2 + 2 = <sum. Cake is yummy.';
119
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>'));
120
		$this->assertEquals($expected, $result);
121
 
122
		$string = '2 + 2 = \:sum. Cake is :adjective.';
123
		$expected = '2 + 2 = :sum. Cake is yummy.';
124
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
125
		$this->assertEquals($expected, $result);
126
 
127
		$string = '2 + 2 = !:sum. Cake is :adjective.';
128
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!'));
129
		$this->assertEquals($expected, $result);
130
 
131
		$string = '2 + 2 = \%sum. Cake is %adjective.';
132
		$expected = '2 + 2 = %sum. Cake is yummy.';
133
		$result = CakeText::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
134
		$this->assertEquals($expected, $result);
135
 
136
		$string = ':a :b \:a :a';
137
		$expected = '1 2 :a 1';
138
		$result = CakeText::insert($string, array('a' => 1, 'b' => 2));
139
		$this->assertEquals($expected, $result);
140
 
141
		$string = ':a :b :c';
142
		$expected = '2 3';
143
		$result = CakeText::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
144
		$this->assertEquals($expected, $result);
145
 
146
		$string = ':a :b :c';
147
		$expected = '1 3';
148
		$result = CakeText::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
149
		$this->assertEquals($expected, $result);
150
 
151
		$string = ':a :b :c';
152
		$expected = '2 3';
153
		$result = CakeText::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
154
		$this->assertEquals($expected, $result);
155
 
156
		$string = ':a, :b and :c';
157
		$expected = '2 and 3';
158
		$result = CakeText::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
159
		$this->assertEquals($expected, $result);
160
 
161
		$string = '":a, :b and :c"';
162
		$expected = '"1, 2"';
163
		$result = CakeText::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
164
		$this->assertEquals($expected, $result);
165
 
166
		$string = '"${a}, ${b} and ${c}"';
167
		$expected = '"1, 2"';
168
		$result = CakeText::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true));
169
		$this->assertEquals($expected, $result);
170
 
171
		$string = '<img src=":src" alt=":alt" class="foo :extra bar"/>';
172
		$expected = '<img src="foo" class="foo bar"/>';
173
		$result = CakeText::insert($string, array('src' => 'foo'), array('clean' => 'html'));
174
 
175
		$this->assertEquals($expected, $result);
176
 
177
		$string = '<img src=":src" class=":no :extra"/>';
178
		$expected = '<img src="foo"/>';
179
		$result = CakeText::insert($string, array('src' => 'foo'), array('clean' => 'html'));
180
		$this->assertEquals($expected, $result);
181
 
182
		$string = '<img src=":src" class=":no :extra"/>';
183
		$expected = '<img src="foo" class="bar"/>';
184
		$result = CakeText::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
185
		$this->assertEquals($expected, $result);
186
 
187
		$result = CakeText::insert("this is a ? string", "test");
188
		$expected = "this is a test string";
189
		$this->assertEquals($expected, $result);
190
 
191
		$result = CakeText::insert("this is a ? string with a ? ? ?", array('long', 'few?', 'params', 'you know'));
192
		$expected = "this is a long string with a few? params you know";
193
		$this->assertEquals($expected, $result);
194
 
195
		$result = CakeText::insert('update saved_urls set url = :url where id = :id', array('url' => 'http://www.testurl.com/param1:url/param2:id', 'id' => 1));
196
		$expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
197
		$this->assertEquals($expected, $result);
198
 
199
		$result = CakeText::insert('update saved_urls set url = :url where id = :id', array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'));
200
		$expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
201
		$this->assertEquals($expected, $result);
202
 
203
		$result = CakeText::insert(':me cake. :subject :verb fantastic.', array('me' => 'I :verb', 'subject' => 'cake', 'verb' => 'is'));
204
		$expected = "I :verb cake. cake is fantastic.";
205
		$this->assertEquals($expected, $result);
206
 
207
		$result = CakeText::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
208
		$expected = "We are of course passing.";
209
		$this->assertEquals($expected, $result);
210
 
211
		$result = CakeText::insert(
212
			':I.am: :not.yet: passing.',
213
			array('I.am' => 'We are'),
214
			array('before' => ':', 'after' => ':', 'clean' => true)
215
		);
216
		$expected = "We are passing.";
217
		$this->assertEquals($expected, $result);
218
 
219
		$result = CakeText::insert('?-pended result', array('Pre'));
220
		$expected = "Pre-pended result";
221
		$this->assertEquals($expected, $result);
222
 
223
		$string = 'switching :timeout / :timeout_count';
224
		$expected = 'switching 5 / 10';
225
		$result = CakeText::insert($string, array('timeout' => 5, 'timeout_count' => 10));
226
		$this->assertEquals($expected, $result);
227
 
228
		$string = 'switching :timeout / :timeout_count';
229
		$expected = 'switching 5 / 10';
230
		$result = CakeText::insert($string, array('timeout_count' => 10, 'timeout' => 5));
231
		$this->assertEquals($expected, $result);
232
 
233
		$string = 'switching :timeout_count by :timeout';
234
		$expected = 'switching 10 by 5';
235
		$result = CakeText::insert($string, array('timeout' => 5, 'timeout_count' => 10));
236
		$this->assertEquals($expected, $result);
237
 
238
		$string = 'switching :timeout_count by :timeout';
239
		$expected = 'switching 10 by 5';
240
		$result = CakeText::insert($string, array('timeout_count' => 10, 'timeout' => 5));
241
		$this->assertEquals($expected, $result);
242
	}
243
 
244
/**
245
 * test Clean Insert
246
 *
247
 * @return void
248
 * @covers ::cleanInsert
249
 */
250
	public function testCleanInsert() {
251
		$result = CakeText::cleanInsert(':incomplete', array(
252
			'clean' => true, 'before' => ':', 'after' => ''
253
		));
254
		$this->assertEquals('', $result);
255
 
256
		$result = CakeText::cleanInsert(':incomplete', array(
257
			'clean' => array('method' => 'text', 'replacement' => 'complete'),
258
			'before' => ':', 'after' => '')
259
		);
260
		$this->assertEquals('complete', $result);
261
 
262
		$result = CakeText::cleanInsert(':in.complete', array(
263
			'clean' => true, 'before' => ':', 'after' => ''
264
		));
265
		$this->assertEquals('', $result);
266
 
267
		$result = CakeText::cleanInsert(':in.complete and', array(
268
			'clean' => true, 'before' => ':', 'after' => '')
269
		);
270
		$this->assertEquals('', $result);
271
 
272
		$result = CakeText::cleanInsert(':in.complete or stuff', array(
273
			'clean' => true, 'before' => ':', 'after' => ''
274
		));
275
		$this->assertEquals('stuff', $result);
276
 
277
		$result = CakeText::cleanInsert(
278
			'<p class=":missing" id=":missing">Text here</p>',
279
			array('clean' => 'html', 'before' => ':', 'after' => '')
280
		);
281
		$this->assertEquals('<p>Text here</p>', $result);
282
	}
283
 
284
/**
285
 * Tests that non-insertable variables (i.e. arrays) are skipped when used as values in
286
 * CakeText::insert().
287
 *
288
 * @return void
289
 * @covers ::insert
290
 */
291
	public function testAutoIgnoreBadInsertData() {
292
		$data = array('foo' => 'alpha', 'bar' => 'beta', 'fale' => array());
293
		$result = CakeText::insert('(:foo > :bar || :fale!)', $data, array('clean' => 'text'));
294
		$this->assertEquals('(alpha > beta || !)', $result);
295
	}
296
 
297
/**
298
 * testTokenize method
299
 *
300
 * @return void
301
 * @covers ::tokenize
302
 */
303
	public function testTokenize() {
304
		$result = CakeText::tokenize('A,(short,boring test)');
305
		$expected = array('A', '(short,boring test)');
306
		$this->assertEquals($expected, $result);
307
 
308
		$result = CakeText::tokenize('A,(short,more interesting( test)');
309
		$expected = array('A', '(short,more interesting( test)');
310
		$this->assertEquals($expected, $result);
311
 
312
		$result = CakeText::tokenize('A,(short,very interesting( test))');
313
		$expected = array('A', '(short,very interesting( test))');
314
		$this->assertEquals($expected, $result);
315
 
316
		$result = CakeText::tokenize('"single tag"', ' ', '"', '"');
317
		$expected = array('"single tag"');
318
		$this->assertEquals($expected, $result);
319
 
320
		$result = CakeText::tokenize('tagA "single tag" tagB', ' ', '"', '"');
321
		$expected = array('tagA', '"single tag"', 'tagB');
322
		$this->assertEquals($expected, $result);
323
 
324
		// Ideographic width space.
325
		$result = CakeText::tokenize("tagA\xe3\x80\x80\"single\xe3\x80\x80tag\"\xe3\x80\x80tagB", "\xe3\x80\x80", '"', '"');
326
		$expected = array('tagA', '"single tag"', 'tagB');
327
		$this->assertEquals($expected, $result);
328
 
329
		$result = CakeText::tokenize('');
330
		$expected = array();
331
		$this->assertEquals($expected, $result);
332
	}
333
 
334
/**
335
 * testReplaceWithQuestionMarkInString method
336
 *
337
 * @return void
338
 * @covers ::insert
339
 */
340
	public function testReplaceWithQuestionMarkInString() {
341
		$string = ':a, :b and :c?';
342
		$expected = '2 and 3?';
343
		$result = CakeText::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
344
		$this->assertEquals($expected, $result);
345
	}
346
 
347
/**
348
 * test that wordWrap() works the same as built-in wordwrap function
349
 *
350
 * @dataProvider wordWrapProvider
351
 * @return void
352
 * @covers ::wordWrap
353
 * @covers ::_wordWrap
354
 */
355
	public function testWordWrap($text, $width, $break = "\n", $cut = false) {
356
		$result = CakeText::wordWrap($text, $width, $break, $cut);
357
		$expected = wordwrap($text, $width, $break, $cut);
358
		$this->assertTextEquals($expected, $result, 'Text not wrapped same as built-in function.');
359
	}
360
 
361
/**
362
 * data provider for testWordWrap method
363
 *
364
 * @return array
365
 */
366
	public function wordWrapProvider() {
367
		return array(
368
			array(
369
				'The quick brown fox jumped over the lazy dog.',
370
				33
371
			),
372
			array(
373
				'A very long woooooooooooord.',
374
				8
375
			),
376
			array(
377
				'A very long woooooooooooord. Right.',
378
				8
379
			),
380
		);
381
	}
382
 
383
/**
384
 * test that wordWrap() properly handle unicode strings.
385
 *
386
 * @return void
387
 * @covers ::wordWrap
388
 * @covers ::_wordWrap
389
 */
390
	public function testWordWrapUnicodeAware() {
391
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
392
		$result = CakeText::wordWrap($text, 33, "\n", true);
393
		$expected = <<<TEXT
394
Но вим омниюм факёльиси элыктрам,
395
мюнырэ лэгыры векж ыт. Выльёт квю
396
андо нюмквуам ты кюм. Зыд эю рыбю
397
м.
398
TEXT;
399
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
400
 
401
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
402
		$result = CakeText::wordWrap($text, 33, "\n");
403
		$expected = <<<TEXT
404
Но вим омниюм факёльиси элыктрам,
405
мюнырэ лэгыры векж ыт. Выльёт
406
квюандо нюмквуам ты кюм. Зыд эю
407
рыбюм.
408
TEXT;
409
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
410
	}
411
 
412
/**
413
 * test that wordWrap() properly handle newline characters.
414
 *
415
 * @return void
416
 * @covers ::wordWrap
417
 * @covers ::_wordWrap
418
 */
419
	public function testWordWrapNewlineAware() {
420
		$text = 'This is a line that is almost the 55 chars long.
421
This is a new sentence which is manually newlined, but is so long it needs two lines.';
422
		$result = CakeText::wordWrap($text, 55);
423
		$expected = <<<TEXT
424
This is a line that is almost the 55 chars long.
425
This is a new sentence which is manually newlined, but
426
is so long it needs two lines.
427
TEXT;
428
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
429
	}
430
 
431
/**
432
 * test wrap method.
433
 *
434
 * @return void
435
 * @covers ::wrap
436
 * @covers ::wordWrap
437
 * @covers ::_wordWrap
438
 */
439
	public function testWrap() {
440
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
441
		$result = CakeText::wrap($text, 33);
442
		$expected = <<<TEXT
443
This is the song that never ends.
444
This is the song that never ends.
445
This is the song that never ends.
446
TEXT;
447
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
448
 
449
		$result = CakeText::wrap($text, array('width' => 20, 'wordWrap' => false));
450
		$expected = 'This is the song th' . "\n" .
451
			'at never ends. This' . "\n" .
452
			' is the song that n' . "\n" .
453
			'ever ends. This is ' . "\n" .
454
			'the song that never' . "\n" .
455
			' ends.';
456
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
457
 
458
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
459
		$result = CakeText::wrap($text, 33);
460
		$expected = <<<TEXT
461
Но вим омниюм факёльиси элыктрам,
462
мюнырэ лэгыры векж ыт. Выльёт
463
квюандо нюмквуам ты кюм. Зыд эю
464
рыбюм.
465
TEXT;
466
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
467
	}
468
 
469
/**
470
 * test wrap() indenting
471
 *
472
 * @return void
473
 * @covers ::wrap
474
 * @covers ::wordWrap
475
 * @covers ::_wordWrap
476
 */
477
	public function testWrapIndent() {
478
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
479
		$result = CakeText::wrap($text, array('width' => 33, 'indent' => "\t", 'indentAt' => 1));
480
		$expected = <<<TEXT
481
This is the song that never ends.
482
	This is the song that never ends.
483
	This is the song that never ends.
484
TEXT;
485
		$this->assertTextEquals($expected, $result);
486
	}
487
 
488
/**
489
 * testTruncate method
490
 *
491
 * @return void
492
 * @covers ::truncate
493
 */
494
	public function testTruncate() {
495
		$text1 = 'The quick brown fox jumps over the lazy dog';
496
		$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
497
		$text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
498
		$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?';
499
		$text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
500
		$text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
501
		$text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
502
		$text8 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
503
		$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
504
		$text10 = 'http://example.com/something/foo:bar';
505
 
506
		$elipsis = "\xe2\x80\xa6";
507
		$this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
508
		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
509
		$this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
510
		$this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
511
		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
512
		$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
513
		$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
514
		$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
515
		$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick brow' . $elipsis);
516
		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick' . $elipsis);
517
		$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr&uuml;c' . $elipsis);
518
		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), $elipsis);
519
		$this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake S' . $elipsis . '</b>');
520
		$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ta' . $elipsis);
521
		$this->assertSame($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the' . $elipsis . '</b>');
522
		$this->assertSame($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 />Great,' . $elipsis);
523
		$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
524
		$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
525
		$this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's" . $elipsis . "</strong></p>");
526
		$this->assertSame($this->Text->truncate($text7, 255), $text7);
527
		$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
528
		$this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
529
		$this->assertSame($this->Text->truncate($text9, 10), 'НОПРСТУ...');
530
		$this->assertSame($this->Text->truncate($text10, 30), 'http://example.com/somethin...');
531
 
532
		$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
533
		$result = $this->Text->truncate($text, 10, array(
534
			'ellipsis' => '...',
535
			'exact' => false,
536
			'html' => true
537
		));
538
		$expected = '<p><span style="font-size: medium;"><a>...</a></span></p>';
539
		$this->assertEquals($expected, $result);
540
 
541
		$text = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
542
Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
543
este artículo de El País.</span></p>
544
<p><span style="font-size: medium;"><span style="font-size:
545
large;">Por qué Steve era distinto.</span></span></p>
546
<p><span style="font-size: medium;"><a href="http://www.elpais.com/
547
articulo/primer/plano/Steve/era/distinto/elpepueconeg/
548
20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
549
Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
550
<p><span style="font-size: medium;">Ya se ha publicado la biografía de
551
Steve Jobs escrita por Walter Isaacson  "<strong>Steve Jobs by Walter
552
Isaacson</strong>", aquí os dejamos la dirección de amazon donde
553
podeís adquirirla.</span></p>
554
<p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
555
Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
556
		$result = $this->Text->truncate($text, 500, array(
557
			'ellipsis' => '... ',
558
			'exact' => false,
559
			'html' => true
560
		));
561
		$expected = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
562
Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
563
este artículo de El País.</span></p>
564
<p><span style="font-size: medium;"><span style="font-size:
565
large;">Por qué Steve era distinto.</span></span></p>
566
<p><span style="font-size: medium;"><a href="http://www.elpais.com/
567
articulo/primer/plano/Steve/era/distinto/elpepueconeg/
568
20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
569
Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
570
<p><span style="font-size: medium;">Ya se ha publicado la biografía de
571
Steve Jobs escrita por Walter Isaacson  "<strong>Steve Jobs by Walter
572
Isaacson</strong>", aquí os dejamos la dirección de amazon donde
573
podeís adquirirla.</span></p>
574
<p><span style="font-size: medium;"><a>... </a></span></p>';
575
		$this->assertEquals($expected, $result);
576
 
577
		// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
578
		$result = $this->Text->truncate($text1, 31, array(
579
			'ending' => '.',
580
			'exact' => false,
581
		));
582
		$expected = 'The quick brown fox jumps.';
583
		$this->assertEquals($expected, $result);
584
 
585
		$result = $this->Text->truncate($text1, 31, array(
586
			'ellipsis' => '..',
587
			'ending' => '.',
588
			'exact' => false,
589
		));
590
		$expected = 'The quick brown fox jumps..';
591
		$this->assertEquals($expected, $result);
592
	}
593
 
594
/**
595
 * testTruncate method with non utf8 sites
596
 *
597
 * @return void
598
 * @covers ::truncate
599
 */
600
	public function testTruncateLegacy() {
601
		Configure::write('App.encoding', 'ISO-8859-1');
602
		$text = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
603
		$result = $this->Text->truncate($text, 31, array(
604
			'html' => true,
605
			'exact' => false,
606
		));
607
		$expected = '<b>&copy; 2005-2007, Cake Software...</b>';
608
		$this->assertEquals($expected, $result);
609
 
610
		$result = $this->Text->truncate($text, 31, array(
611
			'html' => true,
612
			'exact' => true,
613
		));
614
		$expected = '<b>&copy; 2005-2007, Cake Software F...</b>';
615
		$this->assertEquals($expected, $result);
616
	}
617
 
618
/**
619
 * testTail method
620
 *
621
 * @return void
622
 * @covers ::tail
623
 */
624
	public function testTail() {
625
		$text1 = 'The quick brown fox jumps over the lazy dog';
626
		$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
627
		$text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
628
		$text4 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
629
		$text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
630
 
631
		$result = $this->Text->tail($text1, 13);
632
		$this->assertEquals('...e lazy dog', $result);
633
 
634
		$result = $this->Text->tail($text1, 13, array('exact' => false));
635
		$this->assertEquals('...lazy dog', $result);
636
 
637
		$result = $this->Text->tail($text1, 100);
638
		$this->assertEquals('The quick brown fox jumps over the lazy dog', $result);
639
 
640
		$result = $this->Text->tail($text2, 10);
641
		$this->assertEquals('...;mpfung', $result);
642
 
643
		$result = $this->Text->tail($text2, 10, array('exact' => false));
644
		$this->assertEquals('...', $result);
645
 
646
		$result = $this->Text->tail($text3, 255);
647
		$this->assertEquals($text3, $result);
648
 
649
		$result = $this->Text->tail($text3, 21);
650
		$this->assertEquals('...á dicho la verdad?', $result);
651
 
652
		$result = $this->Text->tail($text4, 25);
653
		$this->assertEquals('...a R' . chr(195) . chr(169) . 'publique de France', $result);
654
 
655
		$result = $this->Text->tail($text5, 10);
656
		$this->assertEquals('...цчшщъыь', $result);
657
 
658
		$result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
659
		$this->assertEquals('чшщъыь', $result);
660
	}
661
 
662
/**
663
 * testHighlight method
664
 *
665
 * @return void
666
 * @covers ::highlight
667
 */
668
	public function testHighlight() {
669
		$text = 'This is a test text';
670
		$phrases = array('This', 'text');
671
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
672
		$expected = '<b>This</b> is a test <b>text</b>';
673
		$this->assertEquals($expected, $result);
674
 
675
		$phrases = array('is', 'text');
676
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>', 'regex' => "|\b%s\b|iu"));
677
		$expected = 'This <b>is</b> a test <b>text</b>';
678
		$this->assertEquals($expected, $result);
679
 
680
		$text = 'This is a test text';
681
		$phrases = null;
682
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
683
		$this->assertEquals($text, $result);
684
 
685
		$text = 'This is a (test) text';
686
		$phrases = '(test';
687
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
688
		$this->assertEquals('This is a <b>(test</b>) text', $result);
689
 
690
		$text = 'Ich saß in einem Café am Übergang';
691
		$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
692
		$phrases = array('saß', 'café', 'übergang');
693
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
694
		$this->assertEquals($expected, $result);
695
	}
696
 
697
/**
698
 * testHighlightHtml method
699
 *
700
 * @return void
701
 * @covers ::highlight
702
 */
703
	public function testHighlightHtml() {
704
		$text1 = '<p>strongbow isn&rsquo;t real cider</p>';
705
		$text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
706
		$text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
707
		$text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
708
		$options = array('format' => '<b>\1</b>', 'html' => true);
709
 
710
		$expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
711
		$this->assertEquals($expected, $this->Text->highlight($text1, 'strong', $options));
712
 
713
		$expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
714
		$this->assertEquals($expected, $this->Text->highlight($text2, 'strong', $options));
715
 
716
		$this->assertEquals($this->Text->highlight($text3, 'strong', $options), $text3);
717
 
718
		$this->assertEquals($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
719
 
720
		$expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
721
		$this->assertEquals($expected, $this->Text->highlight($text4, array('strong', 'what'), $options));
722
	}
723
 
724
/**
725
 * testHighlightMulti method
726
 *
727
 * @return void
728
 * @covers ::highlight
729
 */
730
	public function testHighlightMulti() {
731
		$text = 'This is a test text';
732
		$phrases = array('This', 'text');
733
		$result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
734
		$expected = '<b>This</b> is a test <em>text</em>';
735
		$this->assertEquals($expected, $result);
736
	}
737
 
738
/**
739
 * testStripLinks method
740
 *
741
 * @return void
742
 * @covers ::stripLinks
743
 */
744
	public function testStripLinks() {
745
		$text = 'This is a test text';
746
		$expected = 'This is a test text';
747
		$result = $this->Text->stripLinks($text);
748
		$this->assertEquals($expected, $result);
749
 
750
		$text = 'This is a <a href="#">test</a> text';
751
		$expected = 'This is a test text';
752
		$result = $this->Text->stripLinks($text);
753
		$this->assertEquals($expected, $result);
754
 
755
		$text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
756
		$expected = 'This <strong>is</strong> a test text';
757
		$result = $this->Text->stripLinks($text);
758
		$this->assertEquals($expected, $result);
759
 
760
		$text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
761
		$expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
762
		$result = $this->Text->stripLinks($text);
763
		$this->assertEquals($expected, $result);
764
	}
765
 
766
/**
767
 * testHighlightCaseInsensitivity method
768
 *
769
 * @return void
770
 * @covers ::highlight
771
 */
772
	public function testHighlightCaseInsensitivity() {
773
		$text = 'This is a Test text';
774
		$expected = 'This is a <b>Test</b> text';
775
 
776
		$result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
777
		$this->assertEquals($expected, $result);
778
 
779
		$result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
780
		$this->assertEquals($expected, $result);
781
	}
782
 
783
/**
784
 * testExcerpt method
785
 *
786
 * @return void
787
 * @covers ::excerpt
788
 */
789
	public function testExcerpt() {
790
		$text = 'This is a phrase with test text to play with';
791
 
792
		$expected = '...ase with test text to ...';
793
		$result = $this->Text->excerpt($text, 'test', 9, '...');
794
		$this->assertEquals($expected, $result);
795
 
796
		$expected = 'This is a...';
797
		$result = $this->Text->excerpt($text, 'not_found', 9, '...');
798
		$this->assertEquals($expected, $result);
799
 
800
		$expected = 'This is a phras...';
801
		$result = $this->Text->excerpt($text, null, 9, '...');
802
		$this->assertEquals($expected, $result);
803
 
804
		$expected = $text;
805
		$result = $this->Text->excerpt($text, null, 200, '...');
806
		$this->assertEquals($expected, $result);
807
 
808
		$expected = '...a phrase w...';
809
		$result = $this->Text->excerpt($text, 'phrase', 2, '...');
810
		$this->assertEquals($expected, $result);
811
 
812
		$phrase = 'This is a phrase with test text';
813
		$expected = $text;
814
		$result = $this->Text->excerpt($text, $phrase, 13, '...');
815
		$this->assertEquals($expected, $result);
816
 
817
		$text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
818
		$phrase = 'bbbbbbbb';
819
		$result = $this->Text->excerpt($text, $phrase, 10);
820
		$expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
821
		$this->assertEquals($expected, $result);
822
	}
823
 
824
/**
825
 * testExcerptCaseInsensitivity method
826
 *
827
 * @return void
828
 * @covers ::excerpt
829
 */
830
	public function testExcerptCaseInsensitivity() {
831
		$text = 'This is a phrase with test text to play with';
832
 
833
		$expected = '...ase with test text to ...';
834
		$result = $this->Text->excerpt($text, 'TEST', 9, '...');
835
		$this->assertEquals($expected, $result);
836
 
837
		$expected = 'This is a...';
838
		$result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
839
		$this->assertEquals($expected, $result);
840
	}
841
 
842
/**
843
 * testListGeneration method
844
 *
845
 * @return void
846
 * @covers ::toList
847
 */
848
	public function testListGeneration() {
849
		$result = $this->Text->toList(array());
850
		$this->assertEquals('', $result);
851
 
852
		$result = $this->Text->toList(array('One'));
853
		$this->assertEquals('One', $result);
854
 
855
		$result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
856
		$this->assertEquals('Larry, Curly and Moe', $result);
857
 
858
		$result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
859
		$this->assertEquals('Dusty, Lucky y Ned', $result);
860
 
861
		$result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
862
		$this->assertEquals('Dusty, Lucky y Ned', $result);
863
 
864
		$result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
865
		$this->assertEquals('Dusty + Lucky and Ned', $result);
866
 
867
		$result = $this->Text->toList(array('name1' => 'Dusty', 'name2' => 'Lucky'));
868
		$this->assertEquals('Dusty and Lucky', $result);
869
 
870
		$result = $this->Text->toList(array('test_0' => 'banana', 'test_1' => 'apple', 'test_2' => 'lemon'));
871
		$this->assertEquals('banana, apple and lemon', $result);
872
	}
873
 
874
}