Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
 
308
	public function testReplaceWithQuestionMarkInString() {
309
		$string = ':a, :b and :c?';
310
		$expected = '2 and 3?';
311
		$result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
312
		$this->assertEquals($expected, $result);
313
	}
314
 
315
/**
316
 * test that wordWrap() works the same as built-in wordwrap function
317
 *
318
 * @dataProvider wordWrapProvider
319
 * @return void
320
 */
321
	public function testWordWrap($text, $width, $break = "\n", $cut = false) {
322
		$result = String::wordWrap($text, $width, $break, $cut);
323
		$expected = wordwrap($text, $width, $break, $cut);
324
		$this->assertTextEquals($expected, $result, 'Text not wrapped same as built-in function.');
325
	}
326
 
327
/**
328
 * data provider for testWordWrap method
329
 *
330
 * @return array
331
 */
332
	public function wordWrapProvider() {
333
		return array(
334
			array(
335
				'The quick brown fox jumped over the lazy dog.',
336
				33
337
			),
338
			array(
339
				'A very long woooooooooooord.',
340
				8
341
			),
342
			array(
343
				'A very long woooooooooooord. Right.',
344
				8
345
			),
346
		);
347
	}
348
 
349
/**
350
 * test that wordWrap() properly handle unicode strings.
351
 *
352
 * @return void
353
 */
354
	public function testWordWrapUnicodeAware() {
355
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
356
		$result = String::wordWrap($text, 33, "\n", true);
357
		$expected = <<<TEXT
358
Но вим омниюм факёльиси элыктрам,
359
мюнырэ лэгыры векж ыт. Выльёт квю
360
андо нюмквуам ты кюм. Зыд эю рыбю
361
м.
362
TEXT;
363
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
364
 
365
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
366
		$result = String::wordWrap($text, 33, "\n");
367
		$expected = <<<TEXT
368
Но вим омниюм факёльиси элыктрам,
369
мюнырэ лэгыры векж ыт. Выльёт
370
квюандо нюмквуам ты кюм. Зыд эю
371
рыбюм.
372
TEXT;
373
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
374
	}
375
 
376
/**
377
 * test wrap method.
378
 *
379
 * @return void
380
 */
381
	public function testWrap() {
382
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
383
		$result = String::wrap($text, 33);
384
		$expected = <<<TEXT
385
This is the song that never ends.
386
This is the song that never ends.
387
This is the song that never ends.
388
TEXT;
389
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
390
 
391
		$result = String::wrap($text, array('width' => 20, 'wordWrap' => false));
392
		$expected = 'This is the song th' . "\n" .
393
			'at never ends. This' . "\n" .
394
			' is the song that n' . "\n" .
395
			'ever ends. This is ' . "\n" .
396
			'the song that never' . "\n" .
397
			' ends.';
398
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
399
 
400
		$text = 'Но вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.';
401
		$result = String::wrap($text, 33);
402
		$expected = <<<TEXT
403
Но вим омниюм факёльиси элыктрам,
404
мюнырэ лэгыры векж ыт. Выльёт
405
квюандо нюмквуам ты кюм. Зыд эю
406
рыбюм.
407
TEXT;
408
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
409
	}
410
 
411
/**
412
 * test wrap() indenting
413
 *
414
 * @return void
415
 */
416
	public function testWrapIndent() {
417
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
418
		$result = String::wrap($text, array('width' => 33, 'indent' => "\t", 'indentAt' => 1));
419
		$expected = <<<TEXT
420
This is the song that never ends.
421
	This is the song that never ends.
422
	This is the song that never ends.
423
TEXT;
424
		$this->assertTextEquals($expected, $result);
425
	}
426
 
427
/**
428
 * testTruncate method
429
 *
430
 * @return void
431
 */
432
	public function testTruncate() {
433
		$text1 = 'The quick brown fox jumps over the lazy dog';
434
		$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
435
		$text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
436
		$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?';
437
		$text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
438
		$text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
439
		$text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
440
		$text8 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
441
		$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
442
		$text10 = 'http://example.com/something/foo:bar';
443
 
444
		$elipsis = "\xe2\x80\xa6";
445
		$this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
446
		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
447
		$this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
448
		$this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
449
		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
450
		$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
451
		$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
452
		$this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
453
		$this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick brow' . $elipsis);
454
		$this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick' . $elipsis);
455
		$this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr&uuml;c' . $elipsis);
456
		$this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), $elipsis);
457
		$this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake S' . $elipsis . '</b>');
458
		$this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ta' . $elipsis);
459
		$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>');
460
		$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);
461
		$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>');
462
		$this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
463
		$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>");
464
		$this->assertSame($this->Text->truncate($text7, 255), $text7);
465
		$this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
466
		$this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
467
		$this->assertSame($this->Text->truncate($text9, 10), 'НОПРСТУ...');
468
		$this->assertSame($this->Text->truncate($text10, 30), 'http://example.com/somethin...');
469
 
470
		$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
471
		$result = $this->Text->truncate($text, 10, array(
472
			'ellipsis' => '...',
473
			'exact' => false,
474
			'html' => true
475
		));
476
		$expected = '<p><span style="font-size: medium;"><a>...</a></span></p>';
477
		$this->assertEquals($expected, $result);
478
 
479
		$text = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
480
Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
481
este artículo de El País.</span></p>
482
<p><span style="font-size: medium;"><span style="font-size:
483
large;">Por qué Steve era distinto.</span></span></p>
484
<p><span style="font-size: medium;"><a href="http://www.elpais.com/
485
articulo/primer/plano/Steve/era/distinto/elpepueconeg/
486
20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
487
Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
488
<p><span style="font-size: medium;">Ya se ha publicado la biografía de
489
Steve Jobs escrita por Walter Isaacson  "<strong>Steve Jobs by Walter
490
Isaacson</strong>", aquí os dejamos la dirección de amazon donde
491
podeís adquirirla.</span></p>
492
<p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
493
Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
494
		$result = $this->Text->truncate($text, 500, array(
495
			'ellipsis' => '... ',
496
			'exact' => false,
497
			'html' => true
498
		));
499
		$expected = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
500
Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
501
este artículo de El País.</span></p>
502
<p><span style="font-size: medium;"><span style="font-size:
503
large;">Por qué Steve era distinto.</span></span></p>
504
<p><span style="font-size: medium;"><a href="http://www.elpais.com/
505
articulo/primer/plano/Steve/era/distinto/elpepueconeg/
506
20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
507
Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
508
<p><span style="font-size: medium;">Ya se ha publicado la biografía de
509
Steve Jobs escrita por Walter Isaacson  "<strong>Steve Jobs by Walter
510
Isaacson</strong>", aquí os dejamos la dirección de amazon donde
511
podeís adquirirla.</span></p>
512
<p><span style="font-size: medium;"><a>... </a></span></p>';
513
		$this->assertEquals($expected, $result);
514
 
515
		// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
516
		$result = $this->Text->truncate($text1, 31, array(
517
			'ending' => '.',
518
			'exact' => false,
519
		));
520
		$expected = 'The quick brown fox jumps.';
521
		$this->assertEquals($expected, $result);
522
 
523
		$result = $this->Text->truncate($text1, 31, array(
524
			'ellipsis' => '..',
525
			'ending' => '.',
526
			'exact' => false,
527
		));
528
		$expected = 'The quick brown fox jumps..';
529
		$this->assertEquals($expected, $result);
530
	}
531
 
532
/**
533
 * testTruncate method with non utf8 sites
534
 *
535
 * @return void
536
 */
537
	public function testTruncateLegacy() {
538
		Configure::write('App.encoding', 'ISO-8859-1');
539
		$text = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
540
		$result = $this->Text->truncate($text, 31, array(
541
			'html' => true,
542
			'exact' => false,
543
		));
544
		$expected = '<b>&copy; 2005-2007, Cake Software...</b>';
545
		$this->assertEquals($expected, $result);
546
 
547
		$result = $this->Text->truncate($text, 31, array(
548
			'html' => true,
549
			'exact' => true,
550
		));
551
		$expected = '<b>&copy; 2005-2007, Cake Software F...</b>';
552
		$this->assertEquals($expected, $result);
553
	}
554
 
555
/**
556
 * testTail method
557
 *
558
 * @return void
559
 */
560
	public function testTail() {
561
		$text1 = 'The quick brown fox jumps over the lazy dog';
562
		$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
563
		$text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
564
		$text4 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
565
		$text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
566
 
567
		$result = $this->Text->tail($text1, 13);
568
		$this->assertEquals('...e lazy dog', $result);
569
 
570
		$result = $this->Text->tail($text1, 13, array('exact' => false));
571
		$this->assertEquals('...lazy dog', $result);
572
 
573
		$result = $this->Text->tail($text1, 100);
574
		$this->assertEquals('The quick brown fox jumps over the lazy dog', $result);
575
 
576
		$result = $this->Text->tail($text2, 10);
577
		$this->assertEquals('...;mpfung', $result);
578
 
579
		$result = $this->Text->tail($text2, 10, array('exact' => false));
580
		$this->assertEquals('...', $result);
581
 
582
		$result = $this->Text->tail($text3, 255);
583
		$this->assertEquals($text3, $result);
584
 
585
		$result = $this->Text->tail($text3, 21);
586
		$this->assertEquals('...á dicho la verdad?', $result);
587
 
588
		$result = $this->Text->tail($text4, 25);
589
		$this->assertEquals('...a R' . chr(195) . chr(169) . 'publique de France', $result);
590
 
591
		$result = $this->Text->tail($text5, 10);
592
		$this->assertEquals('...цчшщъыь', $result);
593
 
594
		$result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
595
		$this->assertEquals('чшщъыь', $result);
596
	}
597
 
598
/**
599
 * testHighlight method
600
 *
601
 * @return void
602
 */
603
	public function testHighlight() {
604
		$text = 'This is a test text';
605
		$phrases = array('This', 'text');
606
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
607
		$expected = '<b>This</b> is a test <b>text</b>';
608
		$this->assertEquals($expected, $result);
609
 
610
		$phrases = array('is', 'text');
611
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>', 'regex' => "|\b%s\b|iu"));
612
		$expected = 'This <b>is</b> a test <b>text</b>';
613
		$this->assertEquals($expected, $result);
614
 
615
		$text = 'This is a test text';
616
		$phrases = null;
617
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
618
		$this->assertEquals($text, $result);
619
 
620
		$text = 'This is a (test) text';
621
		$phrases = '(test';
622
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
623
		$this->assertEquals('This is a <b>(test</b>) text', $result);
624
 
625
		$text = 'Ich saß in einem Café am Übergang';
626
		$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
627
		$phrases = array('saß', 'café', 'übergang');
628
		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
629
		$this->assertEquals($expected, $result);
630
	}
631
 
632
/**
633
 * testHighlightHtml method
634
 *
635
 * @return void
636
 */
637
	public function testHighlightHtml() {
638
		$text1 = '<p>strongbow isn&rsquo;t real cider</p>';
639
		$text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
640
		$text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
641
		$text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
642
		$options = array('format' => '<b>\1</b>', 'html' => true);
643
 
644
		$expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
645
		$this->assertEquals($expected, $this->Text->highlight($text1, 'strong', $options));
646
 
647
		$expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
648
		$this->assertEquals($expected, $this->Text->highlight($text2, 'strong', $options));
649
 
650
		$this->assertEquals($this->Text->highlight($text3, 'strong', $options), $text3);
651
 
652
		$this->assertEquals($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
653
 
654
		$expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
655
		$this->assertEquals($this->Text->highlight($text4, array('strong', 'what'), $options), $expected);
656
	}
657
 
658
/**
659
 * testHighlightMulti method
660
 *
661
 * @return void
662
 */
663
	public function testHighlightMulti() {
664
		$text = 'This is a test text';
665
		$phrases = array('This', 'text');
666
		$result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
667
		$expected = '<b>This</b> is a test <em>text</em>';
668
		$this->assertEquals($expected, $result);
669
	}
670
 
671
/**
672
 * testStripLinks method
673
 *
674
 * @return void
675
 */
676
	public function testStripLinks() {
677
		$text = 'This is a test text';
678
		$expected = 'This is a test text';
679
		$result = $this->Text->stripLinks($text);
680
		$this->assertEquals($expected, $result);
681
 
682
		$text = 'This is a <a href="#">test</a> text';
683
		$expected = 'This is a test text';
684
		$result = $this->Text->stripLinks($text);
685
		$this->assertEquals($expected, $result);
686
 
687
		$text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
688
		$expected = 'This <strong>is</strong> a test text';
689
		$result = $this->Text->stripLinks($text);
690
		$this->assertEquals($expected, $result);
691
 
692
		$text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
693
		$expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
694
		$result = $this->Text->stripLinks($text);
695
		$this->assertEquals($expected, $result);
696
	}
697
 
698
/**
699
 * testHighlightCaseInsensitivity method
700
 *
701
 * @return void
702
 */
703
	public function testHighlightCaseInsensitivity() {
704
		$text = 'This is a Test text';
705
		$expected = 'This is a <b>Test</b> text';
706
 
707
		$result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
708
		$this->assertEquals($expected, $result);
709
 
710
		$result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
711
		$this->assertEquals($expected, $result);
712
	}
713
 
714
/**
715
 * testExcerpt method
716
 *
717
 * @return void
718
 */
719
	public function testExcerpt() {
720
		$text = 'This is a phrase with test text to play with';
721
 
722
		$expected = '...ase with test text to ...';
723
		$result = $this->Text->excerpt($text, 'test', 9, '...');
724
		$this->assertEquals($expected, $result);
725
 
726
		$expected = 'This is a...';
727
		$result = $this->Text->excerpt($text, 'not_found', 9, '...');
728
		$this->assertEquals($expected, $result);
729
 
730
		$expected = 'This is a phras...';
731
		$result = $this->Text->excerpt($text, null, 9, '...');
732
		$this->assertEquals($expected, $result);
733
 
734
		$expected = $text;
735
		$result = $this->Text->excerpt($text, null, 200, '...');
736
		$this->assertEquals($expected, $result);
737
 
738
		$expected = '...a phrase w...';
739
		$result = $this->Text->excerpt($text, 'phrase', 2, '...');
740
		$this->assertEquals($expected, $result);
741
 
742
		$phrase = 'This is a phrase with test text';
743
		$expected = $text;
744
		$result = $this->Text->excerpt($text, $phrase, 13, '...');
745
		$this->assertEquals($expected, $result);
746
 
747
		$text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
748
		$phrase = 'bbbbbbbb';
749
		$result = $this->Text->excerpt($text, $phrase, 10);
750
		$expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
751
		$this->assertEquals($expected, $result);
752
	}
753
 
754
/**
755
 * testExcerptCaseInsensitivity method
756
 *
757
 * @return void
758
 */
759
	public function testExcerptCaseInsensitivity() {
760
		$text = 'This is a phrase with test text to play with';
761
 
762
		$expected = '...ase with test text to ...';
763
		$result = $this->Text->excerpt($text, 'TEST', 9, '...');
764
		$this->assertEquals($expected, $result);
765
 
766
		$expected = 'This is a...';
767
		$result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
768
		$this->assertEquals($expected, $result);
769
	}
770
 
771
/**
772
 * testListGeneration method
773
 *
774
 * @return void
775
 */
776
	public function testListGeneration() {
777
		$result = $this->Text->toList(array());
778
		$this->assertEquals('', $result);
779
 
780
		$result = $this->Text->toList(array('One'));
781
		$this->assertEquals('One', $result);
782
 
783
		$result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
784
		$this->assertEquals('Larry, Curly and Moe', $result);
785
 
786
		$result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
787
		$this->assertEquals('Dusty, Lucky y Ned', $result);
788
 
789
		$result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
790
		$this->assertEquals('Dusty, Lucky y Ned', $result);
791
 
792
		$result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
793
		$this->assertEquals('Dusty + Lucky and Ned', $result);
794
 
795
		$result = $this->Text->toList(array('name1' => 'Dusty', 'name2' => 'Lucky'));
796
		$this->assertEquals('Dusty and Lucky', $result);
797
 
798
		$result = $this->Text->toList(array('test_0' => 'banana', 'test_1' => 'apple', 'test_2' => 'lemon'));
799
		$this->assertEquals('banana, apple and lemon', $result);
800
	}
801
 
802
}