Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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