TextHelperTest.php 17 KB

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