TextHelperTest.php 17 KB

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