Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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