StringTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. <?php
  2. /**
  3. * StringTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('String', 'Utility');
  20. /**
  21. * StringTest class
  22. *
  23. * @package Cake.Test.Case.Utility
  24. */
  25. class StringTest extends CakeTestCase {
  26. public function setUp() {
  27. parent::setUp();
  28. $this->Text = new String();
  29. }
  30. public function tearDown() {
  31. parent::tearDown();
  32. unset($this->Text);
  33. }
  34. /**
  35. * testUuidGeneration method
  36. *
  37. * @return void
  38. */
  39. public function testUuidGeneration() {
  40. $result = String::uuid();
  41. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  42. $match = (bool)preg_match($pattern, $result);
  43. $this->assertTrue($match);
  44. }
  45. /**
  46. * testMultipleUuidGeneration method
  47. *
  48. * @return void
  49. */
  50. public function testMultipleUuidGeneration() {
  51. $check = array();
  52. $count = mt_rand(10, 1000);
  53. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  54. for ($i = 0; $i < $count; $i++) {
  55. $result = String::uuid();
  56. $match = (bool)preg_match($pattern, $result);
  57. $this->assertTrue($match);
  58. $this->assertFalse(in_array($result, $check));
  59. $check[] = $result;
  60. }
  61. }
  62. /**
  63. * testInsert method
  64. *
  65. * @return void
  66. */
  67. public function testInsert() {
  68. $string = 'some string';
  69. $expected = 'some string';
  70. $result = String::insert($string, array());
  71. $this->assertEquals($expected, $result);
  72. $string = '2 + 2 = :sum. Cake is :adjective.';
  73. $expected = '2 + 2 = 4. Cake is yummy.';
  74. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
  75. $this->assertEquals($expected, $result);
  76. $string = '2 + 2 = %sum. Cake is %adjective.';
  77. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
  78. $this->assertEquals($expected, $result);
  79. $string = '2 + 2 = 2sum2. Cake is 9adjective9.';
  80. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])%s\\1/'));
  81. $this->assertEquals($expected, $result);
  82. $string = '2 + 2 = 12sum21. Cake is 23adjective45.';
  83. $expected = '2 + 2 = 4. Cake is 23adjective45.';
  84. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])([\d])%s\\2\\1/'));
  85. $this->assertEquals($expected, $result);
  86. $string = ':web :web_site';
  87. $expected = 'www http';
  88. $result = String::insert($string, array('web' => 'www', 'web_site' => 'http'));
  89. $this->assertEquals($expected, $result);
  90. $string = '2 + 2 = <sum. Cake is <adjective>.';
  91. $expected = '2 + 2 = <sum. Cake is yummy.';
  92. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>'));
  93. $this->assertEquals($expected, $result);
  94. $string = '2 + 2 = \:sum. Cake is :adjective.';
  95. $expected = '2 + 2 = :sum. Cake is yummy.';
  96. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
  97. $this->assertEquals($expected, $result);
  98. $string = '2 + 2 = !:sum. Cake is :adjective.';
  99. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!'));
  100. $this->assertEquals($expected, $result);
  101. $string = '2 + 2 = \%sum. Cake is %adjective.';
  102. $expected = '2 + 2 = %sum. Cake is yummy.';
  103. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
  104. $this->assertEquals($expected, $result);
  105. $string = ':a :b \:a :a';
  106. $expected = '1 2 :a 1';
  107. $result = String::insert($string, array('a' => 1, 'b' => 2));
  108. $this->assertEquals($expected, $result);
  109. $string = ':a :b :c';
  110. $expected = '2 3';
  111. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  112. $this->assertEquals($expected, $result);
  113. $string = ':a :b :c';
  114. $expected = '1 3';
  115. $result = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
  116. $this->assertEquals($expected, $result);
  117. $string = ':a :b :c';
  118. $expected = '2 3';
  119. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  120. $this->assertEquals($expected, $result);
  121. $string = ':a, :b and :c';
  122. $expected = '2 and 3';
  123. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  124. $this->assertEquals($expected, $result);
  125. $string = '":a, :b and :c"';
  126. $expected = '"1, 2"';
  127. $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
  128. $this->assertEquals($expected, $result);
  129. $string = '"${a}, ${b} and ${c}"';
  130. $expected = '"1, 2"';
  131. $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true));
  132. $this->assertEquals($expected, $result);
  133. $string = '<img src=":src" alt=":alt" class="foo :extra bar"/>';
  134. $expected = '<img src="foo" class="foo bar"/>';
  135. $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
  136. $this->assertEquals($expected, $result);
  137. $string = '<img src=":src" class=":no :extra"/>';
  138. $expected = '<img src="foo"/>';
  139. $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
  140. $this->assertEquals($expected, $result);
  141. $string = '<img src=":src" class=":no :extra"/>';
  142. $expected = '<img src="foo" class="bar"/>';
  143. $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
  144. $this->assertEquals($expected, $result);
  145. $result = String::insert("this is a ? string", "test");
  146. $expected = "this is a test string";
  147. $this->assertEquals($expected, $result);
  148. $result = String::insert("this is a ? string with a ? ? ?", array('long', 'few?', 'params', 'you know'));
  149. $expected = "this is a long string with a few? params you know";
  150. $this->assertEquals($expected, $result);
  151. $result = String::insert('update saved_urls set url = :url where id = :id', array('url' => 'http://www.testurl.com/param1:url/param2:id','id' => 1));
  152. $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
  153. $this->assertEquals($expected, $result);
  154. $result = String::insert('update saved_urls set url = :url where id = :id', array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'));
  155. $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
  156. $this->assertEquals($expected, $result);
  157. $result = String::insert(':me cake. :subject :verb fantastic.', array('me' => 'I :verb', 'subject' => 'cake', 'verb' => 'is'));
  158. $expected = "I :verb cake. cake is fantastic.";
  159. $this->assertEquals($expected, $result);
  160. $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
  161. $expected = "We are of course passing.";
  162. $this->assertEquals($expected, $result);
  163. $result = String::insert(
  164. ':I.am: :not.yet: passing.',
  165. array('I.am' => 'We are'),
  166. array('before' => ':', 'after' => ':', 'clean' => true)
  167. );
  168. $expected = "We are passing.";
  169. $this->assertEquals($expected, $result);
  170. $result = String::insert('?-pended result', array('Pre'));
  171. $expected = "Pre-pended result";
  172. $this->assertEquals($expected, $result);
  173. $string = 'switching :timeout / :timeout_count';
  174. $expected = 'switching 5 / 10';
  175. $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
  176. $this->assertEquals($expected, $result);
  177. $string = 'switching :timeout / :timeout_count';
  178. $expected = 'switching 5 / 10';
  179. $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
  180. $this->assertEquals($expected, $result);
  181. $string = 'switching :timeout_count by :timeout';
  182. $expected = 'switching 10 by 5';
  183. $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
  184. $this->assertEquals($expected, $result);
  185. $string = 'switching :timeout_count by :timeout';
  186. $expected = 'switching 10 by 5';
  187. $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
  188. $this->assertEquals($expected, $result);
  189. }
  190. /**
  191. * test Clean Insert
  192. *
  193. * @return void
  194. */
  195. public function testCleanInsert() {
  196. $result = String::cleanInsert(':incomplete', array(
  197. 'clean' => true, 'before' => ':', 'after' => ''
  198. ));
  199. $this->assertEquals('', $result);
  200. $result = String::cleanInsert(':incomplete', array(
  201. 'clean' => array('method' => 'text', 'replacement' => 'complete'),
  202. 'before' => ':', 'after' => '')
  203. );
  204. $this->assertEquals('complete', $result);
  205. $result = String::cleanInsert(':in.complete', array(
  206. 'clean' => true, 'before' => ':', 'after' => ''
  207. ));
  208. $this->assertEquals('', $result);
  209. $result = String::cleanInsert(':in.complete and', array(
  210. 'clean' => true, 'before' => ':', 'after' => '')
  211. );
  212. $this->assertEquals('', $result);
  213. $result = String::cleanInsert(':in.complete or stuff', array(
  214. 'clean' => true, 'before' => ':', 'after' => ''
  215. ));
  216. $this->assertEquals('stuff', $result);
  217. $result = String::cleanInsert(
  218. '<p class=":missing" id=":missing">Text here</p>',
  219. array('clean' => 'html', 'before' => ':', 'after' => '')
  220. );
  221. $this->assertEquals('<p>Text here</p>', $result);
  222. }
  223. /**
  224. * Tests that non-insertable variables (i.e. arrays) are skipped when used as values in
  225. * String::insert().
  226. *
  227. * @return void
  228. */
  229. public function testAutoIgnoreBadInsertData() {
  230. $data = array('foo' => 'alpha', 'bar' => 'beta', 'fale' => array());
  231. $result = String::insert('(:foo > :bar || :fale!)', $data, array('clean' => 'text'));
  232. $this->assertEquals('(alpha > beta || !)', $result);
  233. }
  234. /**
  235. * testTokenize method
  236. *
  237. * @return void
  238. */
  239. public function testTokenize() {
  240. $result = String::tokenize('A,(short,boring test)');
  241. $expected = array('A', '(short,boring test)');
  242. $this->assertEquals($expected, $result);
  243. $result = String::tokenize('A,(short,more interesting( test)');
  244. $expected = array('A', '(short,more interesting( test)');
  245. $this->assertEquals($expected, $result);
  246. $result = String::tokenize('A,(short,very interesting( test))');
  247. $expected = array('A', '(short,very interesting( test))');
  248. $this->assertEquals($expected, $result);
  249. $result = String::tokenize('"single tag"', ' ', '"', '"');
  250. $expected = array('"single tag"');
  251. $this->assertEquals($expected, $result);
  252. $result = String::tokenize('tagA "single tag" tagB', ' ', '"', '"');
  253. $expected = array('tagA', '"single tag"', 'tagB');
  254. $this->assertEquals($expected, $result);
  255. }
  256. public function testReplaceWithQuestionMarkInString() {
  257. $string = ':a, :b and :c?';
  258. $expected = '2 and 3?';
  259. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  260. $this->assertEquals($expected, $result);
  261. }
  262. /**
  263. * test wrap method.
  264. *
  265. * @return void
  266. */
  267. public function testWrap() {
  268. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  269. $result = String::wrap($text, 33);
  270. $expected = <<<TEXT
  271. This is the song that never ends.
  272. This is the song that never ends.
  273. This is the song that never ends.
  274. TEXT;
  275. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  276. $result = String::wrap($text, array('width' => 20, 'wordWrap' => false));
  277. $expected = <<<TEXT
  278. This is the song th
  279. at never ends. This
  280. is the song that n
  281. ever ends. This is
  282. the song that never
  283. ends.
  284. TEXT;
  285. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  286. }
  287. /**
  288. * test wrap() indenting
  289. *
  290. * @return void
  291. */
  292. public function testWrapIndent() {
  293. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  294. $result = String::wrap($text, array('width' => 33, 'indent' => "\t", 'indentAt' => 1));
  295. $expected = <<<TEXT
  296. This is the song that never ends.
  297. This is the song that never ends.
  298. This is the song that never ends.
  299. TEXT;
  300. $this->assertTextEquals($expected, $result);
  301. }
  302. /**
  303. * testTruncate method
  304. *
  305. * @return void
  306. */
  307. public function testTruncate() {
  308. $text1 = 'The quick brown fox jumps over the lazy dog';
  309. $text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
  310. $text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
  311. $text4 = '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?';
  312. $text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
  313. $text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
  314. $text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
  315. $text8 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
  316. $text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
  317. $text10 = 'http://example.com/something/foo:bar';
  318. $elipsis = "\xe2\x80\xa6";
  319. $this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
  320. $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
  321. $this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
  322. $this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
  323. $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
  324. $this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
  325. $this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
  326. $this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '')), '0<b>1<');
  327. $this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick brow' . $elipsis);
  328. $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick' . $elipsis);
  329. $this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr&uuml;c' . $elipsis);
  330. $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), $elipsis);
  331. $this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake S' . $elipsis . '</b>');
  332. $this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ta' . $elipsis);
  333. $this->assertSame($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the' . $elipsis . '</b>');
  334. $this->assertSame($this->Text->truncate($text4, 90, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great,' . $elipsis);
  335. $this->assertSame($this->Text->truncate($text5, 6, array('ellipsis' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
  336. $this->assertSame($this->Text->truncate($text5, 20, array('ellipsis' => '', 'html' => true)), $text5);
  337. $this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's" . $elipsis . "</strong></p>");
  338. $this->assertSame($this->Text->truncate($text7, 255), $text7);
  339. $this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
  340. $this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
  341. $this->assertSame($this->Text->truncate($text9, 10), 'НОПРСТУ...');
  342. $this->assertSame($this->Text->truncate($text10, 30), 'http://example.com/somethin...');
  343. $text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
  344. $result = $this->Text->truncate($text, 10, array(
  345. 'ellipsis' => '...',
  346. 'exact' => false,
  347. 'html' => true
  348. ));
  349. $expected = '<p><span style="font-size: medium;"><a>...</a></span></p>';
  350. $this->assertEquals($expected, $result);
  351. $text = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
  352. Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
  353. este artículo de El País.</span></p>
  354. <p><span style="font-size: medium;"><span style="font-size:
  355. large;">Por qué Steve era distinto.</span></span></p>
  356. <p><span style="font-size: medium;"><a href="http://www.elpais.com/
  357. articulo/primer/plano/Steve/era/distinto/elpepueconeg/
  358. 20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
  359. Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
  360. <p><span style="font-size: medium;">Ya se ha publicado la biografía de
  361. Steve Jobs escrita por Walter Isaacson "<strong>Steve Jobs by Walter
  362. Isaacson</strong>", aquí os dejamos la dirección de amazon donde
  363. podeís adquirirla.</span></p>
  364. <p><span style="font-size: medium;"><a>http://www.amazon.com/Steve-
  365. Jobs-Walter-Isaacson/dp/1451648537</a></span></p>';
  366. $result = $this->Text->truncate($text, 500, array(
  367. 'ellipsis' => '... ',
  368. 'exact' => false,
  369. 'html' => true
  370. ));
  371. $expected = '<p><span style="font-size: medium;">El biógrafo de Steve Jobs, Walter
  372. Isaacson, explica porqué Jobs le pidió que le hiciera su biografía en
  373. este artículo de El País.</span></p>
  374. <p><span style="font-size: medium;"><span style="font-size:
  375. large;">Por qué Steve era distinto.</span></span></p>
  376. <p><span style="font-size: medium;"><a href="http://www.elpais.com/
  377. articulo/primer/plano/Steve/era/distinto/elpepueconeg/
  378. 20111009elpneglse_4/Tes">http://www.elpais.com/articulo/primer/plano/
  379. Steve/era/distinto/elpepueconeg/20111009elpneglse_4/Tes</a></span></p>
  380. <p><span style="font-size: medium;">Ya se ha publicado la biografía de
  381. Steve Jobs escrita por Walter Isaacson "<strong>Steve Jobs by Walter
  382. Isaacson</strong>", aquí os dejamos la dirección de amazon donde
  383. podeís adquirirla.</span></p>
  384. <p><span style="font-size: medium;"><a>... </a></span></p>';
  385. $this->assertEquals($expected, $result);
  386. // test deprecated `ending` (`ellipsis` taking precedence if both are defined)
  387. $result = $this->Text->truncate($text1, 31, array(
  388. 'ending' => '.',
  389. 'exact' => false,
  390. ));
  391. $expected = 'The quick brown fox jumps.';
  392. $this->assertEquals($expected, $result);
  393. $result = $this->Text->truncate($text1, 31, array(
  394. 'ellipsis' => '..',
  395. 'ending' => '.',
  396. 'exact' => false,
  397. ));
  398. $expected = 'The quick brown fox jumps..';
  399. $this->assertEquals($expected, $result);
  400. }
  401. /**
  402. * testTruncate method with non utf8 sites
  403. *
  404. * @return void
  405. */
  406. public function testTruncateLegacy() {
  407. Configure::write('App.encoding', 'ISO-8859-1');
  408. $text = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
  409. $result = $this->Text->truncate($text, 31, array(
  410. 'html' => true,
  411. 'exact' => false,
  412. ));
  413. $expected = '<b>&copy; 2005-2007, Cake Software...</b>';
  414. $this->assertEquals($expected, $result);
  415. $result = $this->Text->truncate($text, 31, array(
  416. 'html' => true,
  417. 'exact' => true,
  418. ));
  419. $expected = '<b>&copy; 2005-2007, Cake Software F...</b>';
  420. $this->assertEquals($expected, $result);
  421. }
  422. /**
  423. * testTail method
  424. *
  425. * @return void
  426. */
  427. public function testTail() {
  428. $text1 = 'The quick brown fox jumps over the lazy dog';
  429. $text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
  430. $text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
  431. $text4 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
  432. $text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
  433. $result = $this->Text->tail($text1, 13);
  434. $this->assertEquals('...e lazy dog', $result);
  435. $result = $this->Text->tail($text1, 13, array('exact' => false));
  436. $this->assertEquals('...lazy dog', $result);
  437. $result = $this->Text->tail($text1, 100);
  438. $this->assertEquals('The quick brown fox jumps over the lazy dog', $result);
  439. $result = $this->Text->tail($text2, 10);
  440. $this->assertEquals('...;mpfung', $result);
  441. $result = $this->Text->tail($text2, 10, array('exact' => false));
  442. $this->assertEquals('...', $result);
  443. $result = $this->Text->tail($text3, 255);
  444. $this->assertEquals($text3, $result);
  445. $result = $this->Text->tail($text3, 21);
  446. $this->assertEquals('...á dicho la verdad?', $result);
  447. $result = $this->Text->tail($text4, 25);
  448. $this->assertEquals('...a R' . chr(195) . chr(169) . 'publique de France', $result);
  449. $result = $this->Text->tail($text5, 10);
  450. $this->assertEquals('...цчшщъыь', $result);
  451. $result = $this->Text->tail($text5, 6, array('ellipsis' => ''));
  452. $this->assertEquals('чшщъыь', $result);
  453. }
  454. /**
  455. * testHighlight method
  456. *
  457. * @return void
  458. */
  459. public function testHighlight() {
  460. $text = 'This is a test text';
  461. $phrases = array('This', 'text');
  462. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  463. $expected = '<b>This</b> is a test <b>text</b>';
  464. $this->assertEquals($expected, $result);
  465. $phrases = array('is', 'text');
  466. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>', 'regex' => "|\b%s\b|iu"));
  467. $expected = 'This <b>is</b> a test <b>text</b>';
  468. $this->assertEquals($expected, $result);
  469. $text = 'This is a test text';
  470. $phrases = null;
  471. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  472. $this->assertEquals($text, $result);
  473. $text = 'This is a (test) text';
  474. $phrases = '(test';
  475. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  476. $this->assertEquals('This is a <b>(test</b>) text', $result);
  477. $text = 'Ich saß in einem Café am Übergang';
  478. $expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
  479. $phrases = array('saß', 'café', 'übergang');
  480. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  481. $this->assertEquals($expected, $result);
  482. }
  483. /**
  484. * testHighlightHtml method
  485. *
  486. * @return void
  487. */
  488. public function testHighlightHtml() {
  489. $text1 = '<p>strongbow isn&rsquo;t real cider</p>';
  490. $text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
  491. $text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  492. $text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  493. $options = array('format' => '<b>\1</b>', 'html' => true);
  494. $expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
  495. $this->assertEquals($expected, $this->Text->highlight($text1, 'strong', $options));
  496. $expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
  497. $this->assertEquals($expected, $this->Text->highlight($text2, 'strong', $options));
  498. $this->assertEquals($this->Text->highlight($text3, 'strong', $options), $text3);
  499. $this->assertEquals($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
  500. $expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  501. $this->assertEquals($this->Text->highlight($text4, array('strong', 'what'), $options), $expected);
  502. }
  503. /**
  504. * testHighlightMulti method
  505. *
  506. * @return void
  507. */
  508. public function testHighlightMulti() {
  509. $text = 'This is a test text';
  510. $phrases = array('This', 'text');
  511. $result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
  512. $expected = '<b>This</b> is a test <em>text</em>';
  513. $this->assertEquals($expected, $result);
  514. }
  515. /**
  516. * testStripLinks method
  517. *
  518. * @return void
  519. */
  520. public function testStripLinks() {
  521. $text = 'This is a test text';
  522. $expected = 'This is a test text';
  523. $result = $this->Text->stripLinks($text);
  524. $this->assertEquals($expected, $result);
  525. $text = 'This is a <a href="#">test</a> text';
  526. $expected = 'This is a test text';
  527. $result = $this->Text->stripLinks($text);
  528. $this->assertEquals($expected, $result);
  529. $text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
  530. $expected = 'This <strong>is</strong> a test text';
  531. $result = $this->Text->stripLinks($text);
  532. $this->assertEquals($expected, $result);
  533. $text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
  534. $expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
  535. $result = $this->Text->stripLinks($text);
  536. $this->assertEquals($expected, $result);
  537. }
  538. /**
  539. * testHighlightCaseInsensitivity method
  540. *
  541. * @return void
  542. */
  543. public function testHighlightCaseInsensitivity() {
  544. $text = 'This is a Test text';
  545. $expected = 'This is a <b>Test</b> text';
  546. $result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
  547. $this->assertEquals($expected, $result);
  548. $result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
  549. $this->assertEquals($expected, $result);
  550. }
  551. /**
  552. * testExcerpt method
  553. *
  554. * @return void
  555. */
  556. public function testExcerpt() {
  557. $text = 'This is a phrase with test text to play with';
  558. $expected = '...ase with test text to ...';
  559. $result = $this->Text->excerpt($text, 'test', 9, '...');
  560. $this->assertEquals($expected, $result);
  561. $expected = 'This is a...';
  562. $result = $this->Text->excerpt($text, 'not_found', 9, '...');
  563. $this->assertEquals($expected, $result);
  564. $expected = 'This is a phras...';
  565. $result = $this->Text->excerpt($text, null, 9, '...');
  566. $this->assertEquals($expected, $result);
  567. $expected = $text;
  568. $result = $this->Text->excerpt($text, null, 200, '...');
  569. $this->assertEquals($expected, $result);
  570. $expected = '...a phrase w...';
  571. $result = $this->Text->excerpt($text, 'phrase', 2, '...');
  572. $this->assertEquals($expected, $result);
  573. $phrase = 'This is a phrase with test text';
  574. $expected = $text;
  575. $result = $this->Text->excerpt($text, $phrase, 13, '...');
  576. $this->assertEquals($expected, $result);
  577. $text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
  578. $phrase = 'bbbbbbbb';
  579. $result = $this->Text->excerpt($text, $phrase, 10);
  580. $expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
  581. $this->assertEquals($expected, $result);
  582. }
  583. /**
  584. * testExcerptCaseInsensitivity method
  585. *
  586. * @return void
  587. */
  588. public function testExcerptCaseInsensitivity() {
  589. $text = 'This is a phrase with test text to play with';
  590. $expected = '...ase with test text to ...';
  591. $result = $this->Text->excerpt($text, 'TEST', 9, '...');
  592. $this->assertEquals($expected, $result);
  593. $expected = 'This is a...';
  594. $result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
  595. $this->assertEquals($expected, $result);
  596. }
  597. /**
  598. * testListGeneration method
  599. *
  600. * @return void
  601. */
  602. public function testListGeneration() {
  603. $result = $this->Text->toList(array());
  604. $this->assertEquals('', $result);
  605. $result = $this->Text->toList(array('One'));
  606. $this->assertEquals('One', $result);
  607. $result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
  608. $this->assertEquals('Larry, Curly and Moe', $result);
  609. $result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
  610. $this->assertEquals('Dusty, Lucky y Ned', $result);
  611. $result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
  612. $this->assertEquals('Dusty, Lucky y Ned', $result);
  613. $result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
  614. $this->assertEquals('Dusty + Lucky and Ned', $result);
  615. $result = $this->Text->toList(array('name1' => 'Dusty', 'name2' => 'Lucky'));
  616. $this->assertEquals('Dusty and Lucky', $result);
  617. $result = $this->Text->toList(array('test_0' => 'banana', 'test_1' => 'apple', 'test_2' => 'lemon'));
  618. $this->assertEquals('banana, apple and lemon', $result);
  619. }
  620. }