TextHelperTest.php 16 KB

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