Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * TextHelperTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
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.View.Helper
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('View', 'View');
20
App::uses('TextHelper', 'View/Helper');
21
 
22
/**
23
 * Class TextHelperTestObject
24
 *
25
 * @package       Cake.Test.Case.View.Helper
26
 */
27
class TextHelperTestObject extends TextHelper {
28
 
29
	public function attach(CakeTextMock $string) {
30
		$this->_engine = $string;
31
	}
32
 
33
	public function engine() {
34
		return $this->_engine;
35
	}
36
 
37
}
38
 
39
/**
40
 * CakeTextMock class
41
 *
42
 * @package       Cake.Test.Case.View.Helper
43
 */
44
class CakeTextMock {
45
}
46
 
47
/**
48
 * TextHelperTest class
49
 *
50
 * @package       Cake.Test.Case.View.Helper
51
 */
52
class TextHelperTest extends CakeTestCase {
53
 
54
/**
55
 * setUp method
56
 *
57
 * @return void
58
 */
59
	public function setUp() {
60
		parent::setUp();
61
		$this->View = new View(null);
62
		$this->Text = new TextHelper($this->View);
63
	}
64
 
65
/**
66
 * tearDown method
67
 *
68
 * @return void
69
 */
70
	public function tearDown() {
71
		unset($this->View);
72
		parent::tearDown();
73
	}
74
 
75
/**
76
 * test String class methods are called correctly
77
 *
78
 * @return void
79
 */
80
	public function testTextHelperProxyMethodCalls() {
81
		$methods = array(
82
			'highlight', 'stripLinks', 'truncate', 'tail', 'excerpt', 'toList',
83
			);
84
		$CakeText = $this->getMock('CakeTextMock', $methods);
85
		$Text = new TextHelperTestObject($this->View, array('engine' => 'CakeTextMock'));
86
		$Text->attach($CakeText);
87
		foreach ($methods as $method) {
88
			$CakeText->expects($this->at(0))->method($method);
89
			$Text->{$method}('who', 'what', 'when', 'where', 'how');
90
		}
91
	}
92
 
93
/**
94
 * test engine override
95
 *
96
 * @return void
97
 */
98
	public function testEngineOverride() {
99
		App::build(array(
100
			'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
101
		), App::REGISTER);
102
		$Text = new TextHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
103
		$this->assertInstanceOf('TestAppEngine', $Text->engine());
104
 
105
		App::build(array(
106
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
107
		));
108
		CakePlugin::load('TestPlugin');
109
		$Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
110
		$this->assertInstanceOf('TestPluginEngine', $Text->engine());
111
		CakePlugin::unload('TestPlugin');
112
	}
113
 
114
/**
115
 * testAutoLink method
116
 *
117
 * @return void
118
 */
119
	public function testAutoLink() {
120
		$text = 'The AWWWARD show happened today';
121
		$result = $this->Text->autoLink($text);
122
		$this->assertEquals($text, $result);
123
 
124
		$text = 'This is a test text';
125
		$expected = 'This is a test text';
126
		$result = $this->Text->autoLink($text);
127
		$this->assertEquals($expected, $result);
128
 
129
		$text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
130
		$result = $this->Text->autoLink($text);
131
		$expected = 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL and <a href="mailto:test@cakephp\.org">test@cakephp\.org</a> email address';
132
		$this->assertRegExp('#^' . $expected . '$#', $result);
133
 
134
		$text = 'Text with a partial <a href="//www.cakephp.org">link</a> link';
135
		$result = $this->Text->autoLink($text, array('escape' => false));
136
		$this->assertEquals($text, $result);
137
 
138
		$text = 'This is a test text with URL http://www.cakephp.org';
139
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
140
		$result = $this->Text->autoLink($text);
141
		$this->assertEquals($expected, $result);
142
 
143
		$text = 'This is a test text with URL http://www.cakephp.org and some more text';
144
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
145
		$result = $this->Text->autoLink($text);
146
		$this->assertEquals($expected, $result);
147
 
148
		$text = "This is a test text with URL http://www.cakephp.org\tand some more text";
149
		$expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
150
		$result = $this->Text->autoLink($text);
151
		$this->assertEquals($expected, $result);
152
 
153
		$text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
154
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
155
		$result = $this->Text->autoLink($text);
156
		$this->assertEquals($expected, $result);
157
 
158
		$text = 'This is a test text with URL (http://www.cakephp.org/page/4) in brackets';
159
		$expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>) in brackets';
160
		$result = $this->Text->autoLink($text);
161
		$this->assertEquals($expected, $result);
162
 
163
		$text = 'This is a test text with URL [http://www.cakephp.org/page/4] in square brackets';
164
		$expected = 'This is a test text with URL [<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>] in square brackets';
165
		$result = $this->Text->autoLink($text);
166
		$this->assertEquals($expected, $result);
167
 
168
		$text = 'This is a test text with URL [http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3] in square brackets';
169
		$expected = 'This is a test text with URL [<a href="http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3">http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3</a>] in square brackets';
170
		$result = $this->Text->autoLink($text);
171
		$this->assertEquals($expected, $result);
172
 
173
		$text = 'This is a test text with URL ;http://www.cakephp.org/page/4; semi-colon';
174
		$expected = 'This is a test text with URL ;<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>; semi-colon';
175
		$result = $this->Text->autoLink($text);
176
		$this->assertEquals($expected, $result);
177
 
178
		$text = 'This is a test text with URL (http://www.cakephp.org/page/4/other(thing)) brackets';
179
		$expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4/other(thing)">http://www.cakephp.org/page/4/other(thing)</a>) brackets';
180
		$result = $this->Text->autoLink($text);
181
		$this->assertEquals($expected, $result);
182
	}
183
 
184
/**
185
 * Test mixing URLs and Email addresses in one confusing string.
186
 *
187
 * @return void
188
 */
189
	public function testAutoLinkMixed() {
190
		$text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
191
		$expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
192
			'http://example.com/store?email=mark@example.com</a> and email.';
193
		$result = $this->Text->autoLink($text);
194
		$this->assertEquals($expected, $result);
195
	}
196
 
197
/**
198
 * test autoLink() and options.
199
 *
200
 * @return void
201
 */
202
	public function testAutoLinkOptions() {
203
		$text = 'This is a test text with URL http://www.cakephp.org';
204
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
205
		$result = $this->Text->autoLink($text, array('class' => 'link'));
206
		$this->assertEquals($expected, $result);
207
 
208
		$text = 'This is a test text with URL http://www.cakephp.org';
209
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
210
		$result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
211
		$this->assertEquals($expected, $result);
212
	}
213
 
214
/**
215
 * Test escaping for autoLink
216
 *
217
 * @return void
218
 */
219
	public function testAutoLinkEscape() {
220
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
221
		$expected = 'This is a &lt;b&gt;test&lt;/b&gt; text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
222
		$result = $this->Text->autoLink($text);
223
		$this->assertEquals($expected, $result);
224
 
225
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
226
		$expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
227
		$result = $this->Text->autoLink($text, array('escape' => false));
228
		$this->assertEquals($expected, $result);
229
 
230
		$text = 'test <ul>
231
		<li>lorem: http://example.org?some</li>
232
		<li>ipsum: http://othersite.com/abc</li>
233
		</ul> test';
234
		$expected = 'test <ul>
235
		<li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
236
		<li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
237
		</ul> test';
238
		$result = $this->Text->autoLink($text, array('escape' => false));
239
		$this->assertEquals($expected, $result);
240
	}
241
 
242
/**
243
 * Data provider for autoLinking
244
 *
245
 * @return array
246
 */
247
	public static function autoLinkProvider() {
248
		return array(
249
			array(
250
				'This is a test text',
251
				'This is a test text',
252
			),
253
			array(
254
				'This is a test that includes (www.cakephp.org)',
255
				'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
256
			),
257
			array(
258
				'This is a test that includes www.cakephp.org:8080',
259
				'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
260
			),
261
			array(
262
				'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
263
				'This is a test that includes <a href="http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
264
			),
265
			array(
266
				'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
267
				'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
268
			),
269
			array(
270
				'This is a test that includes http://example.com/test.php?foo=bar text',
271
				'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
272
			),
273
			array(
274
				'This is a test that includes www.example.com/test.php?foo=bar text',
275
				'This is a test that includes <a href="http://www.example.com/test.php?foo=bar">www.example.com/test.php?foo=bar</a> text',
276
			),
277
			array(
278
				'Text with a partial www.cakephp.org URL',
279
				'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
280
			),
281
			array(
282
				'Text with a partial WWW.cakephp.org URL',
283
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
284
			),
285
			array(
286
				'Text with a partial WWW.cakephp.org &copy, URL',
287
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
288
			),
289
			array(
290
				'Text with a url www.cot.ag/cuIb2Q and more',
291
				'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
292
			),
293
			array(
294
				'Text with a url http://www.does--not--work.com and more',
295
				'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
296
			),
297
			array(
298
				'Text with a url http://www.not--work.com and more',
299
				'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
300
			),
301
			array(
302
				'Text with a url http://www.sub_domain.domain.pl and more',
303
				'Text with a url <a href="http://www.sub_domain.domain.pl">http://www.sub_domain.domain.pl</a> and more',
304
			),
305
			array(
306
				'Text with a partial www.küchenschöhn-not-working.de URL',
307
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
308
			),
309
			array(
310
				'Text with a partial http://www.küchenschöhn-not-working.de URL',
311
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
312
			),
313
		);
314
	}
315
 
316
/**
317
 * testAutoLinkUrls method
318
 *
319
 * @dataProvider autoLinkProvider
320
 * @return void
321
 */
322
	public function testAutoLinkUrls($text, $expected) {
323
		$result = $this->Text->autoLinkUrls($text);
324
		$this->assertEquals($expected, $result);
325
	}
326
 
327
/**
328
 * Test the options for autoLinkUrls
329
 *
330
 * @return void
331
 */
332
	public function testAutoLinkUrlsOptions() {
333
		$text = 'Text with a partial www.cakephp.org URL';
334
		$expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
335
		$result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
336
		$this->assertRegExp('#^' . $expected . '$#', $result);
337
 
338
		$text = 'Text with a partial WWW.cakephp.org &copy; URL';
339
		$expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
340
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
341
		$this->assertRegExp('#^' . $expected . '$#', $result);
342
	}
343
 
344
/**
345
 * Test autoLinkUrls with the escape option.
346
 *
347
 * @return void
348
 */
349
	public function testAutoLinkUrlsEscape() {
350
		$text = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
351
		$expected = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
352
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
353
		$this->assertEquals($expected, $result);
354
 
355
		$text = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
356
		$expected = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
357
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
358
		$this->assertEquals($expected, $result);
359
 
360
		$text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
361
		$expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
362
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
363
		$this->assertEquals($expected, $result);
364
 
365
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
366
		$expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
367
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
368
		$this->assertEquals($expected, $result);
369
 
370
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
371
		$expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
372
		$result = $this->Text->autoLinkUrls($text, array('escape' => true));
373
		$this->assertEquals($expected, $result);
374
 
375
		$text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
376
		$expected = 'Text with a url &lt;a href=&quot;http://www.not-working-www.com&quot;&gt;www.not-working-www.com&lt;/a&gt; and more';
377
		$result = $this->Text->autoLinkUrls($text, array('escape' => true));
378
		$this->assertEquals($expected, $result);
379
 
380
		$text = 'Text with a url www.not-working-www.com and more';
381
		$expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
382
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
383
		$this->assertEquals($expected, $result);
384
 
385
		$text = 'Text with a url http://www.not-working-www.com and more';
386
		$expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
387
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
388
		$this->assertEquals($expected, $result);
389
 
390
		$text = 'Text with a url http://www.www.not-working-www.com and more';
391
		$expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
392
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
393
		$this->assertEquals($expected, $result);
394
	}
395
 
396
/**
397
 * Test autoLinkUrls with query strings.
398
 *
399
 * @return void
400
 */
401
	public function testAutoLinkUrlsQueryString() {
402
		$text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
403
		$expected = 'Text with a partial <a href="http://www.cakephp.org?product_id=123&amp;foo=bar">http://www.cakephp.org?product_id=123&amp;foo=bar</a> link';
404
		$result = $this->Text->autoLinkUrls($text);
405
		$this->assertEquals($expected, $result);
406
	}
407
 
408
/**
409
 * Data provider for autoLinkEmail.
410
 *
411
 * @return void
412
 */
413
	public function autoLinkEmailProvider() {
414
		return array(
415
			array(
416
				'This is a test text',
417
				'This is a test text',
418
			),
419
 
420
			array(
421
				'email@example.com address',
422
				'<a href="mailto:email@example.com">email@example.com</a> address',
423
			),
424
 
425
			array(
426
				'email@example.com address',
427
				'<a href="mailto:email@example.com">email@example.com</a> address',
428
			),
429
 
430
			array(
431
				'(email@example.com) address',
432
				'(<a href="mailto:email@example.com">email@example.com</a>) address',
433
			),
434
 
435
			array(
436
				'Text with email@example.com address',
437
				'Text with <a href="mailto:email@example.com">email@example.com</a> address',
438
			),
439
 
440
			array(
441
				"Text with o'hare._-bob@example.com address",
442
				'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address',
443
			),
444
 
445
			array(
446
				'Text with düsentrieb@küchenschöhn-not-working.de address',
447
				'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address',
448
			),
449
 
450
			array(
451
				'Text with me@subdomain.küchenschöhn.de address',
452
				'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address',
453
			),
454
 
455
			array(
456
				'Text with email@example.com address',
457
				'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address',
458
				array('class' => 'link'),
459
			),
460
 
461
			array(
462
				'<p>mark@example.com</p>',
463
				'<p><a href="mailto:mark@example.com">mark@example.com</a></p>',
464
				array('escape' => false)
465
			),
466
 
467
			array(
468
				'Some&nbsp;mark@example.com&nbsp;Text',
469
				'Some&nbsp;<a href="mailto:mark@example.com">mark@example.com</a>&nbsp;Text',
470
				array('escape' => false)
471
			),
472
		);
473
	}
474
 
475
/**
476
 * testAutoLinkEmails method
477
 *
478
 * @param string $text The text to link
479
 * @param string $expected The expected results.
480
 * @dataProvider autoLinkEmailProvider
481
 * @return void
482
 */
483
	public function testAutoLinkEmails($text, $expected, $attrs = array()) {
484
		$result = $this->Text->autoLinkEmails($text, $attrs);
485
		$this->assertEquals($expected, $result);
486
	}
487
 
488
/**
489
 * test invalid email addresses.
490
 *
491
 * @return void
492
 */
493
	public function testAutoLinkEmailInvalid() {
494
		$result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
495
		$expected = 'this is a myaddress@gmx-de test';
496
		$this->assertEquals($expected, $result);
497
	}
498
 
499
/**
500
 * testAutoParagraph method
501
 *
502
 * @return void
503
 */
504
	public function testAutoParagraph() {
505
		$text = 'This is a test text';
506
		$expected = <<<TEXT
507
<p>This is a test text</p>
508
 
509
TEXT;
510
		$result = $this->Text->autoParagraph($text);
511
		$text = 'This is a <br/> <BR> test text';
512
		$expected = <<<TEXT
513
<p>This is a </p>
514
<p> test text</p>
515
 
516
TEXT;
517
		$result = $this->Text->autoParagraph($text);
518
		$this->assertTextEquals($expected, $result);
519
		$result = $this->Text->autoParagraph($text);
520
		$text = 'This is a <BR id="test"/><br class="test"> test text';
521
		$expected = <<<TEXT
522
<p>This is a </p>
523
<p> test text</p>
524
 
525
TEXT;
526
		$result = $this->Text->autoParagraph($text);
527
		$this->assertTextEquals($expected, $result);
528
		$text = <<<TEXT
529
This is a test text.
530
This is a line return.
531
TEXT;
532
		$expected = <<<TEXT
533
<p>This is a test text.<br />
534
This is a line return.</p>
535
 
536
TEXT;
537
		$result = $this->Text->autoParagraph($text);
538
		$this->assertTextEquals($expected, $result);
539
		$text = <<<TEXT
540
This is a test text.
541
 
542
This is a new line.
543
TEXT;
544
		$expected = <<<TEXT
545
<p>This is a test text.</p>
546
<p>This is a new line.</p>
547
 
548
TEXT;
549
		$result = $this->Text->autoParagraph($text);
550
		$this->assertTextEquals($expected, $result);
551
	}
552
 
553
}