TextHelperTest.php 17 KB

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