PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/cakeusermanagement/cake/tests/cases/libs/configure.test.php

https://github.com/miamiruby/cakestuff
PHP | 565 lines | 304 code | 76 blank | 185 comment | 12 complexity | bcb9c223f619b81dfeee5fa15d7bd3ee MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /* SVN FILE: $Id: configure.test.php 7690 2008-10-02 04:56:53Z nate $ */
  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.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The Open Group Test Suite License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  20. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  21. * @package cake.tests
  22. * @subpackage cake.tests.cases.libs
  23. * @since CakePHP(tm) v 1.2.0.5432
  24. * @version $Revision: 7690 $
  25. * @modifiedby $LastChangedBy: nate $
  26. * @lastmodified $Date: 2008-10-02 00:56:53 -0400 (Thu, 02 Oct 2008) $
  27. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  28. */
  29. App::import('Core', 'Configure');
  30. /**
  31. * TestConfigure class
  32. *
  33. * @package cake
  34. * @subpackage cake.tests.cases.libs
  35. */
  36. class TestConfigure extends Configure {
  37. /**
  38. * &getInstance method
  39. *
  40. * @param bool $boot
  41. * @access public
  42. * @return void
  43. */
  44. function &getInstance($boot = true) {
  45. /**
  46. * instance property
  47. *
  48. * @var array
  49. * @access public
  50. */
  51. static $instance = array();
  52. if (!$instance) {
  53. $instance[0] =& Configure::getInstance();
  54. $instance[0]->__loadBootstrap(false);
  55. }
  56. return $instance[0];
  57. }
  58. }
  59. /**
  60. * Short description for class.
  61. *
  62. * @package cake.tests
  63. * @subpackage cake.tests.cases.libs
  64. */
  65. class ConfigureTest extends CakeTestCase {
  66. /**
  67. * setUp method
  68. *
  69. * @access public
  70. * @return void
  71. */
  72. function setUp() {
  73. parent::setUp();
  74. $this->Configure =& TestConfigure::getInstance();
  75. $this->Configure->write('Cache.disable', true);
  76. }
  77. /**
  78. * tearDown method
  79. *
  80. * @access public
  81. * @return void
  82. */
  83. function tearDown() {
  84. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
  85. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
  86. }
  87. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
  88. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
  89. }
  90. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
  91. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
  92. }
  93. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
  94. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
  95. }
  96. parent::tearDown();
  97. unset($this->Configure);
  98. }
  99. /**
  100. * testListObjects method
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. function testListObjects() {
  106. $result = $this->Configure->listObjects('class', TEST_CAKE_CORE_INCLUDE_PATH . 'libs');
  107. $this->assertTrue(in_array('Xml', $result));
  108. $this->assertTrue(in_array('Cache', $result));
  109. $this->assertTrue(in_array('HttpSocket', $result));
  110. $result = $this->Configure->listObjects('behavior');
  111. $this->assertTrue(in_array('Tree', $result));
  112. $result = $this->Configure->listObjects('controller');
  113. $this->assertTrue(in_array('Pages', $result));
  114. $result = $this->Configure->listObjects('component');
  115. $this->assertTrue(in_array('Auth', $result));
  116. $result = $this->Configure->listObjects('view');
  117. $this->assertTrue(in_array('Media', $result));
  118. $result = $this->Configure->listObjects('helper');
  119. $this->assertTrue(in_array('Html', $result));
  120. $result = $this->Configure->listObjects('model');
  121. $notExpected = array('AppModel', 'Behavior', 'ConnectionManager', 'DbAcl', 'Model', 'Schema');
  122. foreach ($notExpected as $class) {
  123. $this->assertFalse(in_array($class, $result));
  124. }
  125. $result = $this->Configure->listObjects('file');
  126. $this->assertFalse($result);
  127. $result = $this->Configure->listObjects('file', 'non_existing_configure');
  128. $expected = array();
  129. $this->assertEqual($result, $expected);
  130. $result = $this->Configure->listObjects('NonExistingType');
  131. $this->assertFalse($result);
  132. }
  133. /**
  134. * testRead method
  135. *
  136. * @access public
  137. * @return void
  138. */
  139. function testRead() {
  140. $expected = 'ok';
  141. $this->Configure->write('level1.level2.level3_1', $expected);
  142. $this->Configure->write('level1.level2.level3_2', 'something_else');
  143. $result = $this->Configure->read('level1.level2.level3_1');
  144. $this->assertEqual($expected, $result);
  145. $result = $this->Configure->read('level1.level2.level3_2');
  146. $this->assertEqual($result, 'something_else');
  147. $result = $this->Configure->read('debug');
  148. $this->assertTrue($result >= 0);
  149. unset($this->Configure->debug);
  150. $result = $this->Configure->read('debug');
  151. $this->assertTrue($result >= 0);
  152. }
  153. /**
  154. * testWrite method
  155. *
  156. * @access public
  157. * @return void
  158. */
  159. function testWrite() {
  160. $this->Configure->write('SomeName.someKey', 'myvalue');
  161. $result = $this->Configure->read('SomeName.someKey');
  162. $this->assertEqual($result, 'myvalue');
  163. $this->Configure->write('SomeName.someKey', null);
  164. $result = $this->Configure->read('SomeName.someKey');
  165. $this->assertEqual($result, null);
  166. }
  167. /**
  168. * testSetErrorReporting Level
  169. *
  170. * @return void
  171. **/
  172. function testSetErrorReportingLevel() {
  173. $this->Configure->write('debug', 0);
  174. $result = ini_get('error_reporting');
  175. $this->assertEqual($result, 0);
  176. $this->Configure->write('debug', 2);
  177. $result = ini_get('error_reporting');
  178. $this->assertEqual($result, E_ALL);
  179. $result = ini_get('display_errors');
  180. $this->assertEqual($result, 1);
  181. $this->Configure->write('debug', 0);
  182. $result = ini_get('error_reporting');
  183. $this->assertEqual($result, 0);
  184. }
  185. /**
  186. * testDelete method
  187. *
  188. * @access public
  189. * @return void
  190. */
  191. function testDelete() {
  192. $this->Configure->write('SomeName.someKey', 'myvalue');
  193. $result = $this->Configure->read('SomeName.someKey');
  194. $this->assertEqual($result, 'myvalue');
  195. $this->Configure->delete('SomeName.someKey');
  196. $result = $this->Configure->read('SomeName.someKey');
  197. $this->assertTrue($result === null);
  198. $this->Configure->write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
  199. $result = $this->Configure->read('SomeName.someKey');
  200. $this->assertEqual($result, 'myvalue');
  201. $result = $this->Configure->read('SomeName.otherKey');
  202. $this->assertEqual($result, 'otherValue');
  203. $this->Configure->delete('SomeName');
  204. $result = $this->Configure->read('SomeName.someKey');
  205. $this->assertTrue($result === null);
  206. $result = $this->Configure->read('SomeName.otherKey');
  207. $this->assertTrue($result === null);
  208. }
  209. /**
  210. * testLoad method
  211. *
  212. * @access public
  213. * @return void
  214. */
  215. function testLoad() {
  216. $result = $this->Configure->load('non_existing_configuration_file');
  217. $this->assertFalse($result);
  218. $result = $this->Configure->load('config');
  219. $this->assertTrue($result === null);
  220. }
  221. /**
  222. * testStore method
  223. *
  224. * @access public
  225. * @return void
  226. */
  227. function testStore() {
  228. $this->Configure->store(null, 'test', array('data' => 'value'));
  229. $this->Configure->store(null, 'test', array('data' => array('first' => 'value', 'second' => 'value2')));
  230. }
  231. /**
  232. * testVersion method
  233. *
  234. * @access public
  235. * @return void
  236. */
  237. function testVersion() {
  238. $result = $this->Configure->version();
  239. $this->assertTrue(version_compare($result, '1.2', '>='));
  240. unset($this->Configure->Cake['version']);
  241. $result = $this->Configure->version();
  242. $this->assertTrue(version_compare($result, '1.2', '>='));
  243. }
  244. /**
  245. * testBuildPaths method
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. function testBuildPaths() {
  251. $this->Configure->buildPaths(array());
  252. $this->assertTrue(!empty($this->Configure->modelPaths));
  253. $this->Configure->buildPaths(array('model' => 'dummy'));
  254. }
  255. }
  256. /**
  257. * AppImportTest class
  258. *
  259. * @package cake
  260. * @subpackage cake.tests.cases.libs
  261. */
  262. class AppImportTest extends UnitTestCase {
  263. /**
  264. * testClassLoading method
  265. *
  266. * @access public
  267. * @return void
  268. */
  269. function testClassLoading() {
  270. $file = App::import();
  271. $this->assertTrue($file);
  272. $file = App::import('Core', 'Model', false);
  273. $this->assertTrue($file);
  274. $file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
  275. $this->assertFalse($file);
  276. $file = App::import('Model', 'AppModel', false);
  277. $this->assertTrue($file);
  278. $file = App::import('WrongType', null, true, array(), '');
  279. $this->assertTrue($file);
  280. $file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
  281. $this->assertFalse($file);
  282. $file = App::import('Core', 'NonExistingPlugin.NonExistingModel', false);
  283. $this->assertFalse($file);
  284. $file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
  285. $this->assertFalse($file);
  286. $file = App::import('Core', array('NonExistingPlugin.NonExistingModel'), false);
  287. $this->assertFalse($file);
  288. $file = App::import('Core', array('NonExistingPlugin.NonExistingModel.AnotherChild'), false);
  289. $this->assertFalse($file);
  290. if (!class_exists('AppController')) {
  291. $classes = array_flip(get_declared_classes());
  292. if (PHP5) {
  293. $this->assertFalse(isset($classes['PagesController']));
  294. $this->assertFalse(isset($classes['AppController']));
  295. } else {
  296. $this->assertFalse(isset($classes['pagescontroller']));
  297. $this->assertFalse(isset($classes['appcontroller']));
  298. }
  299. $file = App::import('Controller', 'Pages');
  300. $this->assertTrue($file);
  301. $classes = array_flip(get_declared_classes());
  302. if (PHP5) {
  303. $this->assertTrue(isset($classes['PagesController']));
  304. $this->assertTrue(isset($classes['AppController']));
  305. } else {
  306. $this->assertTrue(isset($classes['pagescontroller']));
  307. $this->assertTrue(isset($classes['appcontroller']));
  308. }
  309. $file = App::import('Behavior', 'Containable');
  310. $this->assertTrue($file);
  311. $file = App::import('Component', 'RequestHandler');
  312. $this->assertTrue($file);
  313. $file = App::import('Helper', 'Form');
  314. $this->assertTrue($file);
  315. $file = App::import('Model', 'NonExistingModel');
  316. $this->assertFalse($file);
  317. }
  318. $_back = Configure::read('pluginPaths');
  319. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  320. $result = App::import('Controller', 'TestPlugin.Tests');
  321. $this->assertTrue($result);
  322. $this->assertTrue(class_exists('TestPluginAppController'));
  323. $this->assertTrue(class_exists('TestsController'));
  324. $result = App::import('Helper', 'TestPlugin.OtherHelper');
  325. $this->assertTrue($result);
  326. $this->assertTrue(class_exists('OtherHelperHelper'));
  327. Configure::write('pluginPaths', $_back);
  328. }
  329. /**
  330. * testFileLoading method
  331. *
  332. * @access public
  333. * @return void
  334. */
  335. function testFileLoading () {
  336. $file = App::import('File', 'RealFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php');
  337. $this->assertTrue($file);
  338. $file = App::import('File', 'NoFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
  339. $this->assertFalse($file);
  340. }
  341. // import($type = null, $name = null, $parent = true, $file = null, $search = array(), $return = false) {
  342. /**
  343. * testFileLoadingWithArray method
  344. *
  345. * @access public
  346. * @return void
  347. */
  348. function testFileLoadingWithArray() {
  349. $type = array('type' => 'File', 'name' => 'SomeName', 'parent' => false,
  350. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . DS . 'config' . DS . 'config.php');
  351. $file = App::import($type);
  352. $this->assertTrue($file);
  353. $type = array('type' => 'File', 'name' => 'NoFile', 'parent' => false,
  354. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
  355. $file = App::import($type);
  356. $this->assertFalse($file);
  357. }
  358. /**
  359. * testFileLoadingReturnValue method
  360. *
  361. * @access public
  362. * @return void
  363. */
  364. function testFileLoadingReturnValue () {
  365. $file = App::import('File', 'Name', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', true);
  366. $this->assertTrue($file);
  367. $this->assertTrue(isset($file['Cake.version']));
  368. $type = array('type' => 'File', 'name' => 'OtherName', 'parent' => false,
  369. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', 'return' => true);
  370. $file = App::import($type);
  371. $this->assertTrue($file);
  372. $this->assertTrue(isset($file['Cake.version']));
  373. }
  374. /**
  375. * testLoadingWithSearch method
  376. *
  377. * @access public
  378. * @return void
  379. */
  380. function testLoadingWithSearch () {
  381. $file = App::import('File', 'NewName', false, array(TEST_CAKE_CORE_INCLUDE_PATH ), 'config.php');
  382. $this->assertTrue($file);
  383. $file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php');
  384. $this->assertFalse($file);
  385. }
  386. /**
  387. * testLoadingWithSearchArray method
  388. *
  389. * @access public
  390. * @return void
  391. */
  392. function testLoadingWithSearchArray () {
  393. $type = array('type' => 'File', 'name' => 'RandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(TEST_CAKE_CORE_INCLUDE_PATH ));
  394. $file = App::import($type);
  395. $this->assertTrue($file);
  396. $type = array('type' => 'File', 'name' => 'AnotherRandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS));
  397. $file = App::import($type);
  398. $this->assertFalse($file);
  399. }
  400. /**
  401. * testMultipleLoading method
  402. *
  403. * @access public
  404. * @return void
  405. */
  406. function testMultipleLoading() {
  407. $toLoad = array('I18n', 'Socket');
  408. $classes = array_flip(get_declared_classes());
  409. $this->assertFalse(isset($classes['i18n']));
  410. $this->assertFalse(isset($classes['Socket']));
  411. $load = App::import($toLoad);
  412. $this->assertTrue($load);
  413. $classes = array_flip(get_declared_classes());
  414. if (PHP5) {
  415. $this->assertTrue(isset($classes['I18n']));
  416. } else {
  417. $this->assertTrue(isset($classes['i18n']));
  418. }
  419. $load = App::import(array('I18n', 'SomeNotFoundClass', 'Socket'));
  420. $this->assertFalse($load);
  421. $load = App::import($toLoad);
  422. $this->assertTrue($load);
  423. }
  424. /**
  425. * This test only works if you have plugins/my_plugin set up.
  426. * plugins/my_plugin/models/my_plugin.php and other_model.php
  427. */
  428. /*
  429. function testMultipleLoadingByType() {
  430. $classes = array_flip(get_declared_classes());
  431. $this->assertFalse(isset($classes['OtherPlugin']));
  432. $this->assertFalse(isset($classes['MyPlugin']));
  433. $load = App::import('Model', array('MyPlugin.OtherPlugin', 'MyPlugin.MyPlugin'));
  434. $this->assertTrue($load);
  435. $classes = array_flip(get_declared_classes());
  436. $this->assertTrue(isset($classes['OtherPlugin']));
  437. $this->assertTrue(isset($classes['MyPlugin']));
  438. }
  439. */
  440. function testLoadingVendor() {
  441. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  442. Configure::write('vendorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS));
  443. ob_start();
  444. $result = App::import('Vendor', 'TestPlugin.TestPluginAsset', array('ext' => 'css'));
  445. $text = ob_get_clean();
  446. $this->assertTrue($result);
  447. $this->assertEqual($text, 'this is the test plugin asset css file');
  448. ob_start();
  449. $result = App::import('Vendor', 'TestAsset', array('ext' => 'css'));
  450. $text = ob_get_clean();
  451. $this->assertTrue($result);
  452. $this->assertEqual($text, 'this is the test asset css file');
  453. $result = App::import('Vendor', 'TestPlugin.SamplePlugin');
  454. $this->assertTrue($result);
  455. $this->assertTrue(class_exists('SamplePluginClassTestName'));
  456. $result = App::import('Vendor', 'Sample');
  457. $this->assertTrue($result);
  458. $this->assertTrue(class_exists('SampleClassTestName'));
  459. ob_start();
  460. $result = App::import('Vendor', 'SomeName', array('file' => 'some.name.php'));
  461. $text = ob_get_clean();
  462. $this->assertTrue($result);
  463. $this->assertEqual($text, 'This is a file with dot in file name');
  464. ob_start();
  465. $result = App::import('Vendor', 'TestHello', array('file' => 'Test'.DS.'hello.php'));
  466. $text = ob_get_clean();
  467. $this->assertTrue($result);
  468. $this->assertEqual($text, 'This is the hello.php file in Test directoy');
  469. ob_start();
  470. $result = App::import('Vendor', 'MyTest', array('file' => 'Test'.DS.'MyTest.php'));
  471. $text = ob_get_clean();
  472. $this->assertTrue($result);
  473. $this->assertEqual($text, 'This is the MyTest.php file');
  474. ob_start();
  475. $result = App::import('Vendor', 'Welcome');
  476. $text = ob_get_clean();
  477. $this->assertTrue($result);
  478. $this->assertEqual($text, 'This is the welcome.php file in vendors directoy');
  479. ob_start();
  480. $result = App::import('Vendor', 'TestPlugin.Welcome');
  481. $text = ob_get_clean();
  482. $this->assertTrue($result);
  483. $this->assertEqual($text, 'This is the welcome.php file in test_plugin/vendors directoy');
  484. }
  485. }
  486. ?>