TextHelperTest.php 15 KB

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