TextHelperTest.php 17 KB

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