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