Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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
	public function testTextHelperProxyMethodCalls() {
79
		$methods = array(
80
			'highlight', 'stripLinks', 'truncate', 'excerpt', 'toList',
81
			);
82
		$String = $this->getMock('StringMock', $methods);
83
		$Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock'));
84
		$Text->attach($String);
85
		foreach ($methods as $method) {
86
			$String->expects($this->at(0))->method($method);
87
			$Text->{$method}('who', 'what', 'when', 'where', 'how');
88
		}
89
	}
90
 
91
/**
92
 * test engine override
93
 */
94
	public function testEngineOverride() {
95
		App::build(array(
96
			'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
97
		), App::REGISTER);
98
		$Text = new TextHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
99
		$this->assertInstanceOf('TestAppEngine', $Text->engine());
100
 
101
		App::build(array(
102
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
103
		));
104
		CakePlugin::load('TestPlugin');
105
		$Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
106
		$this->assertInstanceOf('TestPluginEngine', $Text->engine());
107
		CakePlugin::unload('TestPlugin');
108
	}
109
 
110
/**
111
 * testAutoLink method
112
 *
113
 * @return void
114
 */
115
	public function testAutoLink() {
116
		$text = 'This is a test text';
117
		$expected = 'This is a test text';
118
		$result = $this->Text->autoLink($text);
119
		$this->assertEquals($expected, $result);
120
 
121
		$text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
122
		$result = $this->Text->autoLink($text);
123
		$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';
124
		$this->assertRegExp('#^' . $expected . '$#', $result);
125
 
126
		$text = 'This is a test text with URL http://www.cakephp.org';
127
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
128
		$result = $this->Text->autoLink($text);
129
		$this->assertEquals($expected, $result);
130
 
131
		$text = 'This is a test text with URL http://www.cakephp.org and some more text';
132
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
133
		$result = $this->Text->autoLink($text);
134
		$this->assertEquals($expected, $result);
135
 
136
		$text = "This is a test text with URL http://www.cakephp.org\tand some more text";
137
		$expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
138
		$result = $this->Text->autoLink($text);
139
		$this->assertEquals($expected, $result);
140
 
141
		$text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
142
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
143
		$result = $this->Text->autoLink($text);
144
		$this->assertEquals($expected, $result);
145
	}
146
 
147
/**
148
 * Test mixing URLs and Email addresses in one confusing string.
149
 *
150
 * @return void
151
 */
152
	public function testAutoLinkMixed() {
153
		$text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
154
		$expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
155
			'http://example.com/store?email=mark@example.com</a> and email.';
156
		$result = $this->Text->autoLink($text);
157
		$this->assertEquals($expected, $result);
158
	}
159
 
160
/**
161
 * test autoLink() and options.
162
 *
163
 * @return void
164
 */
165
	public function testAutoLinkOptions() {
166
		$text = 'This is a test text with URL http://www.cakephp.org';
167
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
168
		$result = $this->Text->autoLink($text, array('class' => 'link'));
169
		$this->assertEquals($expected, $result);
170
 
171
		$text = 'This is a test text with URL http://www.cakephp.org';
172
		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
173
		$result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
174
		$this->assertEquals($expected, $result);
175
	}
176
 
177
/**
178
 * Test escaping for autoLink
179
 *
180
 * @return void
181
 */
182
	public function testAutoLinkEscape() {
183
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
184
		$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>';
185
		$result = $this->Text->autoLink($text);
186
		$this->assertEquals($expected, $result);
187
 
188
		$text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
189
		$expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
190
		$result = $this->Text->autoLink($text, array('escape' => false));
191
		$this->assertEquals($expected, $result);
192
 
193
		$text = 'test <ul>
194
		<li>lorem: http://example.org?some</li>
195
		<li>ipsum: http://othersite.com/abc</li>
196
		</ul> test';
197
		$expected = 'test <ul>
198
		<li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
199
		<li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
200
		</ul> test';
201
		$result = $this->Text->autoLink($text, array('escape' => false));
202
		$this->assertEquals($expected, $result);
203
	}
204
 
205
/**
206
 * Data provider for autoLinking
207
 */
208
	public static function autoLinkProvider() {
209
		return array(
210
			array(
211
				'This is a test text',
212
				'This is a test text',
213
			),
214
			array(
215
				'This is a test that includes (www.cakephp.org)',
216
				'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
217
			),
218
			array(
219
				'This is a test that includes www.cakephp.org:8080',
220
				'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
221
			),
222
			array(
223
				'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
224
				'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>',
225
			),
226
			array(
227
				'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
228
				'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
229
			),
230
			array(
231
				'This is a test that includes http://example.com/test.php?foo=bar text',
232
				'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
233
			),
234
			array(
235
				'This is a test that includes www.example.com/test.php?foo=bar text',
236
				'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',
237
			),
238
			array(
239
				'Text with a partial www.cakephp.org URL',
240
				'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
241
			),
242
			array(
243
				'Text with a partial WWW.cakephp.org URL',
244
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
245
			),
246
			array(
247
				'Text with a partial WWW.cakephp.org &copy, URL',
248
				'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
249
			),
250
			array(
251
				'Text with a url www.cot.ag/cuIb2Q and more',
252
				'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
253
			),
254
			array(
255
				'Text with a url http://www.does--not--work.com and more',
256
				'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
257
			),
258
			array(
259
				'Text with a url http://www.not--work.com and more',
260
				'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
261
			),
262
			array(
263
				'Text with a partial www.küchenschöhn-not-working.de URL',
264
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
265
			),
266
			array(
267
				'Text with a partial http://www.küchenschöhn-not-working.de URL',
268
				'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
269
			),
270
		);
271
	}
272
 
273
/**
274
 * testAutoLinkUrls method
275
 *
276
 * @dataProvider autoLinkProvider
277
 * @return void
278
 */
279
	public function testAutoLinkUrls($text, $expected) {
280
		$result = $this->Text->autoLinkUrls($text);
281
		$this->assertEquals($expected, $result);
282
	}
283
 
284
/**
285
 * Test the options for autoLinkUrls
286
 *
287
 * @return void
288
 */
289
	public function testAutoLinkUrlsOptions() {
290
		$text = 'Text with a partial www.cakephp.org URL';
291
		$expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
292
		$result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
293
		$this->assertRegExp('#^' . $expected . '$#', $result);
294
 
295
		$text = 'Text with a partial WWW.cakephp.org &copy; URL';
296
		$expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
297
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
298
		$this->assertRegExp('#^' . $expected . '$#', $result);
299
	}
300
 
301
/**
302
 * Test autoLinkUrls with the escape option.
303
 *
304
 * @return void
305
 */
306
	public function testAutoLinkUrlsEscape() {
307
		$text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
308
		$expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
309
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
310
		$this->assertEquals($expected, $result);
311
 
312
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
313
		$expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
314
		$result = $this->Text->autoLinkUrls($text, array('escape' => false));
315
		$this->assertEquals($expected, $result);
316
 
317
		$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
318
		$expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
319
		$result = $this->Text->autoLinkUrls($text, array('escape' => true));
320
		$this->assertEquals($expected, $result);
321
 
322
		$text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
323
		$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';
324
		$result = $this->Text->autoLinkUrls($text);
325
		$this->assertEquals($expected, $result);
326
 
327
		$text = 'Text with a url www.not-working-www.com and more';
328
		$expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
329
		$result = $this->Text->autoLinkUrls($text);
330
		$this->assertEquals($expected, $result);
331
 
332
		$text = 'Text with a url http://www.not-working-www.com and more';
333
		$expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
334
		$result = $this->Text->autoLinkUrls($text);
335
		$this->assertEquals($expected, $result);
336
 
337
		$text = 'Text with a url http://www.www.not-working-www.com and more';
338
		$expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
339
		$result = $this->Text->autoLinkUrls($text);
340
		$this->assertEquals($expected, $result);
341
	}
342
 
343
/**
344
 * Test autoLinkUrls with query strings.
345
 *
346
 * @return void
347
 */
348
	public function testAutoLinkUrlsQueryString() {
349
		$text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
350
		$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';
351
		$result = $this->Text->autoLinkUrls($text);
352
		$this->assertEquals($expected, $result);
353
	}
354
 
355
/**
356
 * testAutoLinkEmails method
357
 *
358
 * @return void
359
 */
360
	public function testAutoLinkEmails() {
361
		$text = 'This is a test text';
362
		$expected = 'This is a test text';
363
		$result = $this->Text->autoLinkUrls($text);
364
		$this->assertEquals($expected, $result);
365
 
366
		$text = 'email@example.com address';
367
		$expected = '<a href="mailto:email@example.com">email@example.com</a> address';
368
		$result = $this->Text->autoLinkEmails($text);
369
		$this->assertEquals($expected, $result);
370
 
371
		$text = 'email@example.com address';
372
		$expected = '<a href="mailto:email@example.com">email@example.com</a> address';
373
		$result = $this->Text->autoLinkEmails($text);
374
		$this->assertEquals($expected, $result);
375
 
376
		$text = '(email@example.com) address';
377
		$expected = '(<a href="mailto:email@example.com">email@example.com</a>) address';
378
		$result = $this->Text->autoLinkEmails($text);
379
		$this->assertEquals($expected, $result);
380
 
381
		$text = 'Text with email@example.com address';
382
		$expected = 'Text with <a href="mailto:email@example.com">email@example.com</a> address';
383
		$result = $this->Text->autoLinkEmails($text);
384
		$this->assertEquals($expected, $result);
385
 
386
		$text = "Text with o'hare._-bob@example.com address";
387
		$expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
388
		$result = $this->Text->autoLinkEmails($text);
389
		$this->assertEquals($expected, $result);
390
 
391
		$text = 'Text with email@example.com address';
392
		$expected = 'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address';
393
		$result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
394
		$this->assertEquals($expected, $result);
395
 
396
		$text = 'Text with düsentrieb@küchenschöhn-not-working.de address';
397
		$expected = 'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address';
398
		$result = $this->Text->autoLinkEmails($text);
399
		$this->assertEquals($expected, $result);
400
 
401
		$text = 'Text with me@subdomain.küchenschöhn.de address';
402
		$expected = 'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address';
403
		$result = $this->Text->autoLinkEmails($text);
404
		$this->assertEquals($expected, $result);
405
	}
406
 
407
/**
408
 * test invalid email addresses.
409
 *
410
 * @return void
411
 */
412
	public function testAutoLinkEmailInvalid() {
413
		$result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
414
		$expected = 'this is a myaddress@gmx-de test';
415
		$this->assertEquals($expected, $result);
416
	}
417
 
418
/**
419
 * testAutoParagraph method
420
 *
421
 * @return void
422
 */
423
	public function testAutoParagraph() {
424
		$text = 'This is a test text';
425
		$expected = <<<TEXT
426
<p>This is a test text</p>
427
 
428
TEXT;
429
		$result = $this->Text->autoParagraph($text);
430
		$text = 'This is a <br/> <BR> test text';
431
		$expected = <<<TEXT
432
<p>This is a </p>
433
<p> test text</p>
434
 
435
TEXT;
436
		$result = $this->Text->autoParagraph($text);
437
		$this->assertTextEquals($expected, $result);
438
		$result = $this->Text->autoParagraph($text);
439
		$text = 'This is a <BR id="test"/><br class="test"> test text';
440
		$expected = <<<TEXT
441
<p>This is a </p>
442
<p> test text</p>
443
 
444
TEXT;
445
		$result = $this->Text->autoParagraph($text);
446
		$this->assertTextEquals($expected, $result);
447
		$text = <<<TEXT
448
This is a test text.
449
This is a line return.
450
TEXT;
451
		$expected = <<<TEXT
452
<p>This is a test text.<br />
453
This is a line return.</p>
454
 
455
TEXT;
456
		$result = $this->Text->autoParagraph($text);
457
		$this->assertTextEquals($expected, $result);
458
		$text = <<<TEXT
459
This is a test text.
460
 
461
This is a new line.
462
TEXT;
463
		$expected = <<<TEXT
464
<p>This is a test text.</p>
465
<p>This is a new line.</p>
466
 
467
TEXT;
468
		$result = $this->Text->autoParagraph($text);
469
		$this->assertTextEquals($expected, $result);
470
	}
471
 
472
}