PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/tests/cases/basics.test.php

http://skygames.googlecode.com/
PHP | 433 lines | 239 code | 54 blank | 140 comment | 0 complexity | ef6c1058aa380e7f53d116dfd2179846 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: basics.test.php 7805 2008-10-30 17:30:26Z AD7six $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake.tests
  20. * @subpackage cake.tests.cases
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision: 7805 $
  23. * @modifiedby $LastChangedBy: AD7six $
  24. * @lastmodified $Date: 2008-10-30 12:30:26 -0500 (Thu, 30 Oct 2008) $
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. require_once CAKE.'basics.php';
  28. /**
  29. * BasicsTest class
  30. *
  31. * @package cake.tests
  32. * @subpackage cake.tests.cases
  33. */
  34. class BasicsTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function setUp() {
  42. Configure::write('localePaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale'));
  43. }
  44. /**
  45. * testHttpBase method
  46. *
  47. * @return void
  48. * @access public
  49. */
  50. function testHttpBase() {
  51. $__SERVER = $_SERVER;
  52. $_SERVER['HTTP_HOST'] = 'localhost';
  53. $this->assertEqual(env('HTTP_BASE'), '');
  54. $_SERVER['HTTP_HOST'] = 'example.com';
  55. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  56. $_SERVER['HTTP_HOST'] = 'www.example.com';
  57. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  58. $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
  59. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  60. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
  61. $this->assertEqual(env('HTTP_BASE'), '.subdomain.example.com');
  62. $_SERVER = $__SERVER;
  63. }
  64. /**
  65. * test uses()
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. function testUses() {
  71. $this->assertFalse(class_exists('Security'));
  72. $this->assertFalse(class_exists('Sanitize'));
  73. uses('Security', 'Sanitize');
  74. $this->assertTrue(class_exists('Security'));
  75. $this->assertTrue(class_exists('Sanitize'));
  76. }
  77. /**
  78. * Test h()
  79. *
  80. * @access public
  81. * @return void
  82. */
  83. function testH() {
  84. $string = '<foo>';
  85. $result = h($string);
  86. $this->assertEqual('&lt;foo&gt;', $result);
  87. $in = array('this & that', '<p>Which one</p>');
  88. $result = h($in);
  89. $expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
  90. $this->assertEqual($expected, $result);
  91. }
  92. /**
  93. * Test a()
  94. *
  95. * @access public
  96. * @return void
  97. */
  98. function testA() {
  99. $result = a('this', 'that', 'bar');
  100. $this->assertEqual(array('this', 'that', 'bar'), $result);
  101. }
  102. /**
  103. * Test aa()
  104. *
  105. * @access public
  106. * @return void
  107. */
  108. function testAa() {
  109. $result = aa('a', 'b', 'c', 'd');
  110. $expected = array('a' => 'b', 'c' => 'd');
  111. $this->assertEqual($expected, $result);
  112. $result = aa('a', 'b', 'c', 'd', 'e');
  113. $expected = array('a' => 'b', 'c' => 'd', 'e' => null);
  114. $this->assertEqual($result, $expected);
  115. }
  116. /**
  117. * Test am()
  118. *
  119. * @access public
  120. * @return void
  121. */
  122. function testAm() {
  123. $result = am(array('one', 'two'), 2, 3, 4);
  124. $expected = array('one', 'two', 2, 3, 4);
  125. $this->assertEqual($result, $expected);
  126. $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
  127. $expected = array('one' => array(4, 5),'two' => array('foo'));
  128. $this->assertEqual($result, $expected);
  129. }
  130. /**
  131. * test cache()
  132. *
  133. * @access public
  134. * @return void
  135. */
  136. function testCache() {
  137. Configure::write('Cache.disable', true);
  138. $result = cache('basics_test', 'simple cache write');
  139. $this->assertNull($result);
  140. $result = cache('basics_test');
  141. $this->assertNull($result);
  142. Configure::write('Cache.disable', false);
  143. $result = cache('basics_test', 'simple cache write');
  144. $this->assertTrue($result);
  145. $this->assertTrue(file_exists(CACHE . 'basics_test'));
  146. $result = cache('basics_test');
  147. $this->assertEqual($result, 'simple cache write');
  148. @unlink(CACHE . 'basics_test');
  149. cache('basics_test', 'expired', '+1 second');
  150. sleep(2);
  151. $result = cache('basics_test', null, '+1 second');
  152. $this->assertNull($result);
  153. }
  154. /**
  155. * test clearCache()
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function testClearCache() {
  161. cache('views' . DS . 'basics_test.cache', 'simple cache write');
  162. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  163. cache('views' . DS . 'basics_test_2.cache', 'simple cache write 2');
  164. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_2.cache'));
  165. cache('views' . DS . 'basics_test_3.cache', 'simple cache write 3');
  166. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  167. $result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
  168. $this->assertTrue($result);
  169. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  170. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  171. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  172. $result = clearCache(null, 'views', '.cache');
  173. $this->assertTrue($result);
  174. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  175. // Different path from views and with prefix
  176. cache('models' . DS . 'basics_test.cache', 'simple cache write');
  177. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  178. cache('models' . DS . 'basics_test_2.cache', 'simple cache write 2');
  179. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  180. cache('models' . DS . 'basics_test_3.cache', 'simple cache write 3');
  181. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  182. $result = clearCache('basics', 'models', '.cache');
  183. $this->assertTrue($result);
  184. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  185. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  186. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  187. }
  188. /**
  189. * test __()
  190. *
  191. * @access public
  192. * @return void
  193. */
  194. function test__() {
  195. Configure::write('Config.language', 'rule_1_po');
  196. $result = __('Plural Rule 1', true);
  197. $expected = 'Plural Rule 1 (translated)';
  198. $this->assertEqual($result, $expected);
  199. $result = __('Plural Rule 1 (from core)', true);
  200. $expected = 'Plural Rule 1 (from core translated)';
  201. $this->assertEqual($result, $expected);
  202. ob_start();
  203. __('Plural Rule 1 (from core)');
  204. $result = ob_get_clean();
  205. $expected = 'Plural Rule 1 (from core translated)';
  206. $this->assertEqual($result, $expected);
  207. }
  208. /**
  209. * test __n()
  210. *
  211. * @access public
  212. * @return void
  213. */
  214. function test__n() {
  215. Configure::write('Config.language', 'rule_1_po');
  216. $result = __n('%d = 1', '%d = 0 or > 1', 0, true);
  217. $expected = '%d = 0 or > 1 (translated)';
  218. $this->assertEqual($result, $expected);
  219. $result = __n('%d = 1', '%d = 0 or > 1', 1, true);
  220. $expected = '%d = 1 (translated)';
  221. $this->assertEqual($result, $expected);
  222. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2, true);
  223. $expected = '%d = 0 or > 1 (from core translated)';
  224. $this->assertEqual($result, $expected);
  225. ob_start();
  226. __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  227. $result = ob_get_clean();
  228. $expected = '%d = 0 or > 1 (from core translated)';
  229. $this->assertEqual($result, $expected);
  230. }
  231. /**
  232. * test __d()
  233. *
  234. * @access public
  235. * @return void
  236. */
  237. function test__d() {
  238. Configure::write('Config.language', 'rule_1_po');
  239. $result = __d('default', 'Plural Rule 1', true);
  240. $expected = 'Plural Rule 1 (translated)';
  241. $this->assertEqual($result, $expected);
  242. $result = __d('core', 'Plural Rule 1', true);
  243. $expected = 'Plural Rule 1';
  244. $this->assertEqual($result, $expected);
  245. $result = __d('core', 'Plural Rule 1 (from core)', true);
  246. $expected = 'Plural Rule 1 (from core translated)';
  247. $this->assertEqual($result, $expected);
  248. ob_start();
  249. __d('core', 'Plural Rule 1 (from core)');
  250. $result = ob_get_clean();
  251. $expected = 'Plural Rule 1 (from core translated)';
  252. $this->assertEqual($result, $expected);
  253. }
  254. /**
  255. * test __dn()
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. function test__dn() {
  261. Configure::write('Config.language', 'rule_1_po');
  262. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0, true);
  263. $expected = '%d = 0 or > 1 (translated)';
  264. $this->assertEqual($result, $expected);
  265. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0, true);
  266. $expected = '%d = 0 or > 1';
  267. $this->assertEqual($result, $expected);
  268. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0, true);
  269. $expected = '%d = 0 or > 1 (from core translated)';
  270. $this->assertEqual($result, $expected);
  271. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1, true);
  272. $expected = '%d = 1 (translated)';
  273. $this->assertEqual($result, $expected);
  274. ob_start();
  275. __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  276. $result = ob_get_clean();
  277. $expected = '%d = 0 or > 1 (from core translated)';
  278. $this->assertEqual($result, $expected);
  279. }
  280. /**
  281. * test __c()
  282. *
  283. * @access public
  284. * @return void
  285. */
  286. function test__c() {
  287. Configure::write('Config.language', 'rule_1_po');
  288. $result = __c('Plural Rule 1', 5, true);
  289. $expected = 'Plural Rule 1 (translated)';
  290. $this->assertEqual($result, $expected);
  291. $result = __c('Plural Rule 1 (from core)', 5, true);
  292. $expected = 'Plural Rule 1 (from core translated)';
  293. $this->assertEqual($result, $expected);
  294. ob_start();
  295. __c('Plural Rule 1 (from core)', 5);
  296. $result = ob_get_clean();
  297. $expected = 'Plural Rule 1 (from core translated)';
  298. $this->assertEqual($result, $expected);
  299. }
  300. /**
  301. * test __dc()
  302. *
  303. * @access public
  304. * @return void
  305. */
  306. function test__dc() {
  307. Configure::write('Config.language', 'rule_1_po');
  308. $result = __dc('default', 'Plural Rule 1', 5, true);
  309. $expected = 'Plural Rule 1 (translated)';
  310. $this->assertEqual($result, $expected);
  311. $result = __dc('default', 'Plural Rule 1 (from core)', 5, true);
  312. $expected = 'Plural Rule 1 (from core translated)';
  313. $this->assertEqual($result, $expected);
  314. $result = __dc('core', 'Plural Rule 1', 5, true);
  315. $expected = 'Plural Rule 1';
  316. $this->assertEqual($result, $expected);
  317. $result = __dc('core', 'Plural Rule 1 (from core)', 5, true);
  318. $expected = 'Plural Rule 1 (from core translated)';
  319. $this->assertEqual($result, $expected);
  320. ob_start();
  321. __dc('default', 'Plural Rule 1 (from core)', 5);
  322. $result = ob_get_clean();
  323. $expected = 'Plural Rule 1 (from core translated)';
  324. $this->assertEqual($result, $expected);
  325. }
  326. /**
  327. * test __dcn()
  328. *
  329. * @access public
  330. * @return void
  331. */
  332. function test__dcn() {
  333. Configure::write('Config.language', 'rule_1_po');
  334. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 5, true);
  335. $expected = '%d = 0 or > 1 (translated)';
  336. $this->assertEqual($result, $expected);
  337. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 5, true);
  338. $expected = '%d = 1 (from core translated)';
  339. $this->assertEqual($result, $expected);
  340. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 5, true);
  341. $expected = '%d = 0 or > 1';
  342. $this->assertEqual($result, $expected);
  343. ob_start();
  344. __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 5);
  345. $result = ob_get_clean();
  346. $expected = '%d = 1 (from core translated)';
  347. $this->assertEqual($result, $expected);
  348. }
  349. /**
  350. * test LogError()
  351. *
  352. * @access public
  353. * @return void
  354. */
  355. function testLogError() {
  356. @unlink(LOGS . 'error.log');
  357. LogError('Testing LogError() basic function');
  358. LogError("Testing with\nmulti-line\nstring");
  359. $result = file_get_contents(LOGS . 'error.log');
  360. $this->assertPattern('/Error: Testing LogError\(\) basic function/', $result);
  361. $this->assertNoPattern("/Error: Testing with\nmulti-line\nstring/", $result);
  362. $this->assertPattern('/Error: Testing with multi-line string/', $result);
  363. }
  364. /**
  365. * test convertSlash()
  366. *
  367. * @access public
  368. * @return void
  369. */
  370. function testConvertSlash() {
  371. $result = convertSlash('\path\to\location\\');
  372. $expected = '\path\to\location\\';
  373. $this->assertEqual($result, $expected);
  374. $result = convertSlash('/path/to/location/');
  375. $expected = 'path_to_location';
  376. $this->assertEqual($result, $expected);
  377. }
  378. }
  379. ?>