PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/string.test.php

https://github.com/t73biz/BaseApp
PHP | 292 lines | 186 code | 47 blank | 59 comment | 1 complexity | 3aabd7d63710a3f68735b0e5c7f50d87 MD5 | raw file
  1. <?php
  2. /**
  3. * StringTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.5432
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Core', 'String');
  21. /**
  22. * StringTest class
  23. *
  24. * @package cake
  25. * @subpackage cake.tests.cases.libs
  26. */
  27. class StringTest extends CakeTestCase {
  28. /**
  29. * testUuidGeneration method
  30. *
  31. * @access public
  32. * @return void
  33. */
  34. function testUuidGeneration() {
  35. $result = String::uuid();
  36. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  37. $match = preg_match($pattern, $result);
  38. $this->assertTrue($match);
  39. }
  40. /**
  41. * testMultipleUuidGeneration method
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. function testMultipleUuidGeneration() {
  47. $check = array();
  48. $count = mt_rand(10, 1000);
  49. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  50. for($i = 0; $i < $count; $i++) {
  51. $result = String::uuid();
  52. $match = preg_match($pattern, $result);
  53. $this->assertTrue($match);
  54. $this->assertFalse(in_array($result, $check));
  55. $check[] = $result;
  56. }
  57. }
  58. /**
  59. * testInsert method
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. function testInsert() {
  65. $string = 'some string';
  66. $expected = 'some string';
  67. $result = String::insert($string, array());
  68. $this->assertEqual($result, $expected);
  69. $string = '2 + 2 = :sum. Cake is :adjective.';
  70. $expected = '2 + 2 = 4. Cake is yummy.';
  71. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
  72. $this->assertEqual($result, $expected);
  73. $string = '2 + 2 = %sum. Cake is %adjective.';
  74. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
  75. $this->assertEqual($result, $expected);
  76. $string = '2 + 2 = 2sum2. Cake is 9adjective9.';
  77. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])%s\\1/'));
  78. $this->assertEqual($result, $expected);
  79. $string = '2 + 2 = 12sum21. Cake is 23adjective45.';
  80. $expected = '2 + 2 = 4. Cake is 23adjective45.';
  81. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])([\d])%s\\2\\1/'));
  82. $this->assertEqual($result, $expected);
  83. $string = ':web :web_site';
  84. $expected = 'www http';
  85. $result = String::insert($string, array('web' => 'www', 'web_site' => 'http'));
  86. $this->assertEqual($result, $expected);
  87. $string = '2 + 2 = <sum. Cake is <adjective>.';
  88. $expected = '2 + 2 = <sum. Cake is yummy.';
  89. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>'));
  90. $this->assertEqual($result, $expected);
  91. $string = '2 + 2 = \:sum. Cake is :adjective.';
  92. $expected = '2 + 2 = :sum. Cake is yummy.';
  93. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
  94. $this->assertEqual($result, $expected);
  95. $string = '2 + 2 = !:sum. Cake is :adjective.';
  96. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!'));
  97. $this->assertEqual($result, $expected);
  98. $string = '2 + 2 = \%sum. Cake is %adjective.';
  99. $expected = '2 + 2 = %sum. Cake is yummy.';
  100. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
  101. $this->assertEqual($result, $expected);
  102. $string = ':a :b \:a :a';
  103. $expected = '1 2 :a 1';
  104. $result = String::insert($string, array('a' => 1, 'b' => 2));
  105. $this->assertEqual($result, $expected);
  106. $string = ':a :b :c';
  107. $expected = '2 3';
  108. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  109. $this->assertEqual($result, $expected);
  110. $string = ':a :b :c';
  111. $expected = '1 3';
  112. $result = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
  113. $this->assertEqual($result, $expected);
  114. $string = ':a :b :c';
  115. $expected = '2 3';
  116. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  117. $this->assertEqual($result, $expected);
  118. $string = ':a, :b and :c';
  119. $expected = '2 and 3';
  120. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  121. $this->assertEqual($result, $expected);
  122. $string = '":a, :b and :c"';
  123. $expected = '"1, 2"';
  124. $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
  125. $this->assertEqual($result, $expected);
  126. $string = '"${a}, ${b} and ${c}"';
  127. $expected = '"1, 2"';
  128. $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true));
  129. $this->assertEqual($result, $expected);
  130. $string = '<img src=":src" alt=":alt" class="foo :extra bar"/>';
  131. $expected = '<img src="foo" class="foo bar"/>';
  132. $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
  133. $this->assertEqual($result, $expected);
  134. $string = '<img src=":src" class=":no :extra"/>';
  135. $expected = '<img src="foo"/>';
  136. $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
  137. $this->assertEqual($result, $expected);
  138. $string = '<img src=":src" class=":no :extra"/>';
  139. $expected = '<img src="foo" class="bar"/>';
  140. $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
  141. $this->assertEqual($result, $expected);
  142. $result = String::insert("this is a ? string", "test");
  143. $expected = "this is a test string";
  144. $this->assertEqual($result, $expected);
  145. $result = String::insert("this is a ? string with a ? ? ?", array('long', 'few?', 'params', 'you know'));
  146. $expected = "this is a long string with a few? params you know";
  147. $this->assertEqual($result, $expected);
  148. $result = String::insert('update saved_urls set url = :url where id = :id', array('url' => 'http://www.testurl.com/param1:url/param2:id','id' => 1));
  149. $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
  150. $this->assertEqual($result, $expected);
  151. $result = String::insert('update saved_urls set url = :url where id = :id', array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'));
  152. $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
  153. $this->assertEqual($result, $expected);
  154. $result = String::insert(':me cake. :subject :verb fantastic.', array('me' => 'I :verb', 'subject' => 'cake', 'verb' => 'is'));
  155. $expected = "I :verb cake. cake is fantastic.";
  156. $this->assertEqual($result, $expected);
  157. $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
  158. $expected = "We are of course passing.";
  159. $this->assertEqual($result, $expected);
  160. $result = String::insert(
  161. ':I.am: :not.yet: passing.',
  162. array('I.am' => 'We are'),
  163. array('before' => ':', 'after' => ':', 'clean' => true)
  164. );
  165. $expected = "We are passing.";
  166. $this->assertEqual($result, $expected);
  167. $result = String::insert('?-pended result', array('Pre'));
  168. $expected = "Pre-pended result";
  169. $this->assertEqual($result, $expected);
  170. }
  171. /**
  172. * test Clean Insert
  173. *
  174. * @return void
  175. */
  176. function testCleanInsert() {
  177. $result = String::cleanInsert(':incomplete', array(
  178. 'clean' => true, 'before' => ':', 'after' => ''
  179. ));
  180. $this->assertEqual($result, '');
  181. $result = String::cleanInsert(':incomplete', array(
  182. 'clean' => array('method' => 'text', 'replacement' => 'complete'),
  183. 'before' => ':', 'after' => '')
  184. );
  185. $this->assertEqual($result, 'complete');
  186. $result = String::cleanInsert(':in.complete', array(
  187. 'clean' => true, 'before' => ':', 'after' => ''
  188. ));
  189. $this->assertEqual($result, '');
  190. $result = String::cleanInsert(':in.complete and', array(
  191. 'clean' => true, 'before' => ':', 'after' => '')
  192. );
  193. $this->assertEqual($result, '');
  194. $result = String::cleanInsert(':in.complete or stuff', array(
  195. 'clean' => true, 'before' => ':', 'after' => ''
  196. ));
  197. $this->assertEqual($result, 'stuff');
  198. $result = String::cleanInsert(
  199. '<p class=":missing" id=":missing">Text here</p>',
  200. array('clean' => 'html', 'before' => ':', 'after' => '')
  201. );
  202. $this->assertEqual($result, '<p>Text here</p>');
  203. }
  204. /**
  205. * Tests that non-insertable variables (i.e. arrays) are skipped when used as values in
  206. * String::insert().
  207. *
  208. * @return void
  209. */
  210. function testAutoIgnoreBadInsertData() {
  211. $data = array('foo' => 'alpha', 'bar' => 'beta', 'fale' => array());
  212. $result = String::insert('(:foo > :bar || :fale!)', $data, array('clean' => 'text'));
  213. $this->assertEqual($result, '(alpha > beta || !)');
  214. }
  215. /**
  216. * testTokenize method
  217. *
  218. * @access public
  219. * @return void
  220. */
  221. function testTokenize() {
  222. $result = String::tokenize('A,(short,boring test)');
  223. $expected = array('A', '(short,boring test)');
  224. $this->assertEqual($result, $expected);
  225. $result = String::tokenize('A,(short,more interesting( test)');
  226. $expected = array('A', '(short,more interesting( test)');
  227. $this->assertEqual($result, $expected);
  228. $result = String::tokenize('A,(short,very interesting( test))');
  229. $expected = array('A', '(short,very interesting( test))');
  230. $this->assertEqual($result, $expected);
  231. $result = String::tokenize('"single tag"', ' ', '"', '"');
  232. $expected = array('"single tag"');
  233. $this->assertEqual($expected, $result);
  234. $result = String::tokenize('tagA "single tag" tagB', ' ', '"', '"');
  235. $expected = array('tagA', '"single tag"', 'tagB');
  236. $this->assertEqual($expected, $result);
  237. }
  238. function testReplaceWithQuestionMarkInString() {
  239. $string = ':a, :b and :c?';
  240. $expected = '2 and 3?';
  241. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  242. $this->assertEqual($expected, $result);
  243. }
  244. }
  245. ?>