Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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(StringMock $string) {
30
		$this->_engine = $string;
31
	}
32
 
33
	public function engine() {
34
		return $this->_engine;
35
	}
36
 
37
}
38
 
39
/**
40
 * StringMock class
41
 *
42
 * @package       Cake.Test.Case.View.Helper
43
 */
44
class StringMock {
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
		$String = $this->getMock('StringMock', $methods);
85
		$Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock'));
86
		$Text->attach($String);
87
		foreach ($methods as $method) {
88
			$String->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 = 'This is a test text';
121
		$expected = 'This is a test text';
122
		$result = $this->Text->autoLink($text);
123
		$this->assertEquals($expected, $result);
124
 
125
		$text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
126
		$result = $this->Text->autoLink($text);
127
		$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';
128
		$this->assertRegExp('#^' . $expected . '$#', $result);
129
 
130
		$text = 'This is a test text with URL http://www.cakephp.org';
131
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
132
		$result = $this->Text->autoLink($text);
133
		$this->assertEquals($expected, $result);
134
 
135
		$text = 'This is a test text with URL http://www.cakephp.org and some more text';
136
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
137
		$result = $this->Text->autoLink($text);
138
		$this->assertEquals($expected, $result);
139
 
140
		$text = "This is a test text with URL http://www.cakephp.org\tand some more text";
141
		$expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
142
		$result = $this->Text->autoLink($text);
143
		$this->assertEquals($expected, $result);
144
 
145
		$text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
146
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
147
		$result = $this->Text->autoLink($text);
148
		$this->assertEquals($expected, $result);
149
	}
150
 
151
/**
152
 * Test mixing URLs and Email addresses in one confusing string.
153
 *
154
 * @return void
155
 */
156
	public function testAutoLinkMixed() {
157
		$text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
158
		$expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
159
			'http://example.com/store?email=mark@example.com</a> and email.';
160
		$result = $this->Text->autoLink($text);
161
		$this->assertEquals($expected, $result);
162
	}
163
 
164
/**
165
 * test autoLink() and options.
166
 *
167
 * @return void
168
 */
169
	public function testAutoLinkOptions() {
170
		$text = 'This is a test text with URL http://www.cakephp.org';
171
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
172
		$result = $this->Text->autoLink($text, array('class' => 'link'));
173
		$this->assertEquals($expected, $result);
174
 
175
		$text = 'This is a test text with URL http://www.cakephp.org';
176
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
177
		$result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
178
		$this->assertEquals($expected, $result);
179
	}
180
 
181
/**
182
 * Test escaping for autoLink
183
 *
184
 * @return void
185
 */
186
	public function testAutoLinkEscape() {
187
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
188
		$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>';
189
		$result = $this->Text->autoLink($text);
190
		$this->assertEquals($expected, $result);
191
 
192
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
193
		$expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
194
		$result = $this->Text->autoLink($text, array('escape' => false));
195
		$this->assertEquals($expected, $result);
196
 
197
		$text = 'test <ul>
198
		<li>lorem: http://example.org?some</li>
199
		<li>ipsum: http://othersite.com/abc</li>
200
		</ul> test';
201
		$expected = 'test <ul>
202
		<li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
203
		<li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
204
		</ul> test';
205
		$result = $this->Text->autoLink($text, array('escape' => false));
206
		$this->assertEquals($expected, $result);
207
	}
208
 
209
/**
210
 * Data provider for autoLinking
211
 *
212
 * @return array
213
 */
214
	public static function autoLinkProvider() {
215
		return array(
216
			array(
217
				'This is a test text',
218
				'This is a test text',
219
			),
220
			array(
221
				'This is a test that includes (www.cakephp.org)',
222
				'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
223
			),
224
			array(
225
				'This is a test that includes www.cakephp.org:8080',
226
				'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
227
			),
228
			array(
229
				'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
230
				'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>',
231
			),
232
			array(
233
				'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
234
				'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
235
			),
236
			array(
237
				'This is a test that includes http://example.com/test.php?foo=bar text',
238
				'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
239
			),
240
			array(
241
				'This is a test that includes www.example.com/test.php?foo=bar text',
242
				'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',
243
			),
244
			array(
245
				'Text with a partial www.cakephp.org URL',
246
				'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
247
			),
248
			array(
249
				'Text with a partial WWW.cakephp.org URL',
250
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
251
			),
252
			array(
253
				'Text with a partial WWW.cakephp.org &copy, URL',
254
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
255
			),
256
			array(
257
				'Text with a url www.cot.ag/cuIb2Q and more',
258
				'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
259
			),
260
			array(
261
				'Text with a url http://www.does--not--work.com and more',
262
				'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
263
			),
264
			array(
265
				'Text with a url http://www.not--work.com and more',
266
				'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
267
			),
268
			array(
269
				'Text with a url http://www.sub_domain.domain.pl and more',
270
				'Text with a url <a href="http://www.sub_domain.domain.pl">http://www.sub_domain.domain.pl</a> and more',
271
			),
272
			array(
273
				'Text with a partial www.küchenschöhn-not-working.de URL',
274
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
275
			),
276
			array(
277
				'Text with a partial http://www.küchenschöhn-not-working.de URL',
278
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
279
			),
280
		);
281
	}
282
 
283
/**
284
 * testAutoLinkUrls method
285
 *
286
 * @dataProvider autoLinkProvider
287
 * @return void
288
 */
289
	public function testAutoLinkUrls($text, $expected) {
290
		$result = $this->Text->autoLinkUrls($text);
291
		$this->assertEquals($expected, $result);
292
	}
293
 
294
/**
295
 * Test the options for autoLinkUrls
296
 *
297
 * @return void
298
 */
299
	public function testAutoLinkUrlsOptions() {
300
		$text = 'Text with a partial www.cakephp.org URL';
301
		$expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
302
		$result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
303
		$this->assertRegExp('#^' . $expected . '$#', $result);
304
 
305
		$text = 'Text with a partial WWW.cakephp.org &copy; URL';
306
		$expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
307
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
308
		$this->assertRegExp('#^' . $expected . '$#', $result);
309
	}
310
 
311
/**
312
 * Test autoLinkUrls with the escape option.
313
 *
314
 * @return void
315
 */
316
	public function testAutoLinkUrlsEscape() {
317
		$text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
318
		$expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
319
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
320
		$this->assertEquals($expected, $result);
321
 
322
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
323
		$expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
324
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
325
		$this->assertEquals($expected, $result);
326
 
327
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
328
		$expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
329
		$result = $this->Text->autoLinkUrls($text, array('escape' => true));
330
		$this->assertEquals($expected, $result);
331
 
332
		$text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
333
		$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';
334
		$result = $this->Text->autoLinkUrls($text);
335
		$this->assertEquals($expected, $result);
336
 
337
		$text = 'Text with a url www.not-working-www.com and more';
338
		$expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
339
		$result = $this->Text->autoLinkUrls($text);
340
		$this->assertEquals($expected, $result);
341
 
342
		$text = 'Text with a url http://www.not-working-www.com and more';
343
		$expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
344
		$result = $this->Text->autoLinkUrls($text);
345
		$this->assertEquals($expected, $result);
346
 
347
		$text = 'Text with a url http://www.www.not-working-www.com and more';
348
		$expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
349
		$result = $this->Text->autoLinkUrls($text);
350
		$this->assertEquals($expected, $result);
351
	}
352
 
353
/**
354
 * Test autoLinkUrls with query strings.
355
 *
356
 * @return void
357
 */
358
	public function testAutoLinkUrlsQueryString() {
359
		$text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
360
		$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';
361
		$result = $this->Text->autoLinkUrls($text);
362
		$this->assertEquals($expected, $result);
363
	}
364
 
365
/**
366
 * Data provider for autoLinkEmail.
367
 *
368
 * @return void
369
 */
370
	public function autoLinkEmailProvider() {
371
		return array(
372
			array(
373
				'This is a test text',
374
				'This is a test text',
375
			),
376
 
377
			array(
378
				'email@example.com address',
379
				'<a href="mailto:email@example.com">email@example.com</a> address',
380
			),
381
 
382
			array(
383
				'email@example.com address',
384
				'<a href="mailto:email@example.com">email@example.com</a> address',
385
			),
386
 
387
			array(
388
				'(email@example.com) address',
389
				'(<a href="mailto:email@example.com">email@example.com</a>) address',
390
			),
391
 
392
			array(
393
				'Text with email@example.com address',
394
				'Text with <a href="mailto:email@example.com">email@example.com</a> address',
395
			),
396
 
397
			array(
398
				"Text with o'hare._-bob@example.com address",
399
				'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address',
400
			),
401
 
402
			array(
403
				'Text with düsentrieb@küchenschöhn-not-working.de address',
404
				'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address',
405
			),
406
 
407
			array(
408
				'Text with me@subdomain.küchenschöhn.de address',
409
				'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address',
410
			),
411
 
412
			array(
413
				'Text with email@example.com address',
414
				'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address',
415
				array('class' => 'link'),
416
			),
417
 
418
			array(
419
				'<p>mark@example.com</p>',
420
				'<p><a href="mailto:mark@example.com">mark@example.com</a></p>',
421
				array('escape' => false)
422
			),
423
 
424
			array(
425
				'Some&nbsp;mark@example.com&nbsp;Text',
426
				'Some&nbsp;<a href="mailto:mark@example.com">mark@example.com</a>&nbsp;Text',
427
				array('escape' => false)
428
			),
429
		);
430
	}
431
 
432
/**
433
 * testAutoLinkEmails method
434
 *
435
 * @param string $text The text to link
436
 * @param string $expected The expected results.
437
 * @dataProvider autoLinkEmailProvider
438
 * @return void
439
 */
440
	public function testAutoLinkEmails($text, $expected, $attrs = array()) {
441
		$result = $this->Text->autoLinkEmails($text, $attrs);
442
		$this->assertEquals($expected, $result);
443
	}
444
 
445
/**
446
 * test invalid email addresses.
447
 *
448
 * @return void
449
 */
450
	public function testAutoLinkEmailInvalid() {
451
		$result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
452
		$expected = 'this is a myaddress@gmx-de test';
453
		$this->assertEquals($expected, $result);
454
	}
455
 
456
/**
457
 * testAutoParagraph method
458
 *
459
 * @return void
460
 */
461
	public function testAutoParagraph() {
462
		$text = 'This is a test text';
463
		$expected = <<<TEXT
464
<p>This is a test text</p>
465
 
466
TEXT;
467
		$result = $this->Text->autoParagraph($text);
468
		$text = 'This is a <br/> <BR> test text';
469
		$expected = <<<TEXT
470
<p>This is a </p>
471
<p> test text</p>
472
 
473
TEXT;
474
		$result = $this->Text->autoParagraph($text);
475
		$this->assertTextEquals($expected, $result);
476
		$result = $this->Text->autoParagraph($text);
477
		$text = 'This is a <BR id="test"/><br class="test"> test text';
478
		$expected = <<<TEXT
479
<p>This is a </p>
480
<p> test text</p>
481
 
482
TEXT;
483
		$result = $this->Text->autoParagraph($text);
484
		$this->assertTextEquals($expected, $result);
485
		$text = <<<TEXT
486
This is a test text.
487
This is a line return.
488
TEXT;
489
		$expected = <<<TEXT
490
<p>This is a test text.<br />
491
This is a line return.</p>
492
 
493
TEXT;
494
		$result = $this->Text->autoParagraph($text);
495
		$this->assertTextEquals($expected, $result);
496
		$text = <<<TEXT
497
This is a test text.
498
 
499
This is a new line.
500
TEXT;
501
		$expected = <<<TEXT
502
<p>This is a test text.</p>
503
<p>This is a new line.</p>
504
 
505
TEXT;
506
		$result = $this->Text->autoParagraph($text);
507
		$this->assertTextEquals($expected, $result);
508
	}
509
 
510
}