TextHelperTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\TextHelper;
  21. use Cake\View\View;
  22. /**
  23. * Class TextHelperTestObject
  24. *
  25. */
  26. class TextHelperTestObject extends TextHelper {
  27. public function attach(StringMock $string) {
  28. $this->_engine = $string;
  29. }
  30. public function engine() {
  31. return $this->_engine;
  32. }
  33. }
  34. /**
  35. * StringMock class
  36. *
  37. */
  38. class StringMock {
  39. }
  40. /**
  41. * TextHelperTest class
  42. *
  43. */
  44. class TextHelperTest extends TestCase {
  45. /**
  46. * setUp method
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $this->View = new View();
  53. $this->Text = new TextHelper($this->View);
  54. $this->_appNamespace = Configure::read('App.namespace');
  55. Configure::write('App.namespace', 'TestApp');
  56. }
  57. /**
  58. * tearDown method
  59. *
  60. * @return void
  61. */
  62. public function tearDown() {
  63. unset($this->Text, $this->View);
  64. Configure::write('App.namespace', $this->_appNamespace);
  65. parent::tearDown();
  66. }
  67. /**
  68. * test String class methods are called correctly
  69. *
  70. * @return void
  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. $methods = array(
  94. 'tail'
  95. );
  96. $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods);
  97. $Text = new TextHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\StringMock'));
  98. $Text->attach($String);
  99. foreach ($methods as $method) {
  100. $String->expects($this->at(0))->method($method);
  101. $Text->{$method}('who', 1, array('what'));
  102. }
  103. }
  104. /**
  105. * test engine override
  106. *
  107. * @return void
  108. */
  109. public function testEngineOverride() {
  110. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  111. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Text->engine());
  112. Plugin::load('TestPlugin');
  113. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  114. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Text->engine());
  115. Plugin::unload('TestPlugin');
  116. }
  117. /**
  118. * testAutoLink method
  119. *
  120. * @return void
  121. */
  122. public function testAutoLink() {
  123. $text = 'This is a test text';
  124. $expected = 'This is a test text';
  125. $result = $this->Text->autoLink($text);
  126. $this->assertEquals($expected, $result);
  127. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  128. $result = $this->Text->autoLink($text);
  129. $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';
  130. $this->assertRegExp('#^' . $expected . '$#', $result);
  131. $text = 'This is a test text with URL http://www.cakephp.org';
  132. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  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. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  140. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  141. $result = $this->Text->autoLink($text);
  142. $this->assertEquals($expected, $result);
  143. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  144. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  145. $result = $this->Text->autoLink($text);
  146. $this->assertEquals($expected, $result);
  147. }
  148. /**
  149. * Test mixing URLs and Email addresses in one confusing string.
  150. *
  151. * @return void
  152. */
  153. public function testAutoLinkMixed() {
  154. $text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
  155. $expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
  156. 'http://example.com/store?email=mark@example.com</a> and email.';
  157. $result = $this->Text->autoLink($text);
  158. $this->assertEquals($expected, $result);
  159. }
  160. /**
  161. * test autoLink() and options.
  162. *
  163. * @return void
  164. */
  165. public function testAutoLinkOptions() {
  166. $text = 'This is a test text with URL http://www.cakephp.org';
  167. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  168. $result = $this->Text->autoLink($text, array('class' => 'link'));
  169. $this->assertEquals($expected, $result);
  170. $text = 'This is a test text with URL http://www.cakephp.org';
  171. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  172. $result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * Test escaping for autoLink
  177. *
  178. * @return void
  179. */
  180. public function testAutoLinkEscape() {
  181. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  182. $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>';
  183. $result = $this->Text->autoLink($text);
  184. $this->assertEquals($expected, $result);
  185. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  186. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  187. $result = $this->Text->autoLink($text, array('escape' => false));
  188. $this->assertEquals($expected, $result);
  189. $text = 'test <ul>
  190. <li>lorem: http://example.org?some</li>
  191. <li>ipsum: http://othersite.com/abc</li>
  192. </ul> test';
  193. $expected = 'test <ul>
  194. <li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
  195. <li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
  196. </ul> test';
  197. $result = $this->Text->autoLink($text, array('escape' => false));
  198. $this->assertEquals($expected, $result);
  199. }
  200. /**
  201. * Data provider for autoLinking
  202. *
  203. * @return array
  204. */
  205. public static function autoLinkProvider() {
  206. return array(
  207. array(
  208. 'This is a test text',
  209. 'This is a test text',
  210. ),
  211. array(
  212. 'This is a test that includes (www.cakephp.org)',
  213. 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
  214. ),
  215. array(
  216. 'This is a test that includes www.cakephp.org:8080',
  217. 'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
  218. ),
  219. array(
  220. 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  221. '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>',
  222. ),
  223. array(
  224. 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  225. 'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  226. ),
  227. array(
  228. 'This is a test that includes http://example.com/test.php?foo=bar text',
  229. 'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
  230. ),
  231. array(
  232. 'This is a test that includes www.example.com/test.php?foo=bar text',
  233. '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',
  234. ),
  235. array(
  236. 'Text with a partial www.cakephp.org URL',
  237. 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
  238. ),
  239. array(
  240. 'Text with a partial WWW.cakephp.org URL',
  241. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
  242. ),
  243. array(
  244. 'Text with a partial WWW.cakephp.org &copy, URL',
  245. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
  246. ),
  247. array(
  248. 'Text with a url www.cot.ag/cuIb2Q and more',
  249. 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
  250. ),
  251. array(
  252. 'Text with a url http://www.does--not--work.com and more',
  253. 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
  254. ),
  255. array(
  256. 'Text with a url http://www.not--work.com and more',
  257. 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
  258. ),
  259. array(
  260. 'Text with a url http://www.sub_domain.domain.pl and more',
  261. 'Text with a url <a href="http://www.sub_domain.domain.pl">http://www.sub_domain.domain.pl</a> and more',
  262. ),
  263. array(
  264. 'Text with a partial www.küchenschöhn-not-working.de URL',
  265. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
  266. ),
  267. array(
  268. 'Text with a partial http://www.küchenschöhn-not-working.de URL',
  269. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
  270. ),
  271. );
  272. }
  273. /**
  274. * testAutoLinkUrls method
  275. *
  276. * @dataProvider autoLinkProvider
  277. * @return void
  278. */
  279. public function testAutoLinkUrls($text, $expected) {
  280. $result = $this->Text->autoLinkUrls($text);
  281. $this->assertEquals($expected, $result);
  282. }
  283. /**
  284. * Test the options for autoLinkUrls
  285. *
  286. * @return void
  287. */
  288. public function testAutoLinkUrlsOptions() {
  289. $text = 'Text with a partial www.cakephp.org URL';
  290. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  291. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  292. $this->assertRegExp('#^' . $expected . '$#', $result);
  293. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  294. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  295. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  296. $this->assertRegExp('#^' . $expected . '$#', $result);
  297. }
  298. /**
  299. * Test autoLinkUrls with the escape option.
  300. *
  301. * @return void
  302. */
  303. public function testAutoLinkUrlsEscape() {
  304. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  305. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  306. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  307. $this->assertEquals($expected, $result);
  308. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  309. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  310. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  311. $this->assertEquals($expected, $result);
  312. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  313. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  314. $result = $this->Text->autoLinkUrls($text, array('escape' => true));
  315. $this->assertEquals($expected, $result);
  316. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  317. $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';
  318. $result = $this->Text->autoLinkUrls($text);
  319. $this->assertEquals($expected, $result);
  320. $text = 'Text with a url www.not-working-www.com and more';
  321. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  322. $result = $this->Text->autoLinkUrls($text);
  323. $this->assertEquals($expected, $result);
  324. $text = 'Text with a url http://www.not-working-www.com and more';
  325. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  326. $result = $this->Text->autoLinkUrls($text);
  327. $this->assertEquals($expected, $result);
  328. $text = 'Text with a url http://www.www.not-working-www.com and more';
  329. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  330. $result = $this->Text->autoLinkUrls($text);
  331. $this->assertEquals($expected, $result);
  332. }
  333. /**
  334. * Test autoLinkUrls with query strings.
  335. *
  336. * @return void
  337. */
  338. public function testAutoLinkUrlsQueryString() {
  339. $text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
  340. $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';
  341. $result = $this->Text->autoLinkUrls($text);
  342. $this->assertEquals($expected, $result);
  343. }
  344. /**
  345. * Data provider for autoLinkEmail.
  346. *
  347. * @return void
  348. */
  349. public function autoLinkEmailProvider() {
  350. return array(
  351. array(
  352. 'This is a test text',
  353. 'This is a test text',
  354. ),
  355. array(
  356. 'email@example.com address',
  357. '<a href="mailto:email@example.com">email@example.com</a> address',
  358. ),
  359. array(
  360. 'email@example.com address',
  361. '<a href="mailto:email@example.com">email@example.com</a> address',
  362. ),
  363. array(
  364. '(email@example.com) address',
  365. '(<a href="mailto:email@example.com">email@example.com</a>) address',
  366. ),
  367. array(
  368. 'Text with email@example.com address',
  369. 'Text with <a href="mailto:email@example.com">email@example.com</a> address',
  370. ),
  371. array(
  372. "Text with o'hare._-bob@example.com address",
  373. 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address',
  374. ),
  375. array(
  376. 'Text with düsentrieb@küchenschöhn-not-working.de address',
  377. 'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address',
  378. ),
  379. array(
  380. 'Text with me@subdomain.küchenschöhn.de address',
  381. 'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address',
  382. ),
  383. array(
  384. 'Text with email@example.com address',
  385. 'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address',
  386. array('class' => 'link'),
  387. ),
  388. array(
  389. '<p>mark@example.com</p>',
  390. '<p><a href="mailto:mark@example.com">mark@example.com</a></p>',
  391. array('escape' => false)
  392. ),
  393. array(
  394. 'Some&nbsp;mark@example.com&nbsp;Text',
  395. 'Some&nbsp;<a href="mailto:mark@example.com">mark@example.com</a>&nbsp;Text',
  396. array('escape' => false)
  397. ),
  398. );
  399. }
  400. /**
  401. * testAutoLinkEmails method
  402. *
  403. * @param string $text The text to link
  404. * @param string $expected The expected results.
  405. * @dataProvider autoLinkEmailProvider
  406. * @return void
  407. */
  408. public function testAutoLinkEmails($text, $expected, $attrs = array()) {
  409. $result = $this->Text->autoLinkEmails($text, $attrs);
  410. $this->assertEquals($expected, $result);
  411. }
  412. /**
  413. * test invalid email addresses.
  414. *
  415. * @return void
  416. */
  417. public function testAutoLinkEmailInvalid() {
  418. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  419. $expected = 'this is a myaddress@gmx-de test';
  420. $this->assertEquals($expected, $result);
  421. }
  422. /**
  423. * testAutoParagraph method
  424. *
  425. * @return void
  426. */
  427. public function testAutoParagraph() {
  428. $text = 'This is a test text';
  429. $expected = <<<TEXT
  430. <p>This is a test text</p>
  431. TEXT;
  432. $result = $this->Text->autoParagraph($text);
  433. $text = 'This is a <br/> <BR> test text';
  434. $expected = <<<TEXT
  435. <p>This is a </p>
  436. <p> test text</p>
  437. TEXT;
  438. $result = $this->Text->autoParagraph($text);
  439. $this->assertTextEquals($expected, $result);
  440. $result = $this->Text->autoParagraph($text);
  441. $text = 'This is a <BR id="test"/><br class="test"> test text';
  442. $expected = <<<TEXT
  443. <p>This is a </p>
  444. <p> test text</p>
  445. TEXT;
  446. $result = $this->Text->autoParagraph($text);
  447. $this->assertTextEquals($expected, $result);
  448. $text = <<<TEXT
  449. This is a test text.
  450. This is a line return.
  451. TEXT;
  452. $expected = <<<TEXT
  453. <p>This is a test text.<br />
  454. This is a line return.</p>
  455. TEXT;
  456. $result = $this->Text->autoParagraph($text);
  457. $this->assertTextEquals($expected, $result);
  458. $text = <<<TEXT
  459. This is a test text.
  460. This is a new line.
  461. TEXT;
  462. $expected = <<<TEXT
  463. <p>This is a test text.</p>
  464. <p>This is a new line.</p>
  465. TEXT;
  466. $result = $this->Text->autoParagraph($text);
  467. $this->assertTextEquals($expected, $result);
  468. }
  469. }