AppTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. /**
  3. * AppTest file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Core
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * AppTest class
  21. *
  22. * @package Cake.Test.Case.Core
  23. */
  24. class AppTest extends CakeTestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. CakePlugin::unload();
  32. }
  33. /**
  34. * testBuild method
  35. *
  36. * @return void
  37. */
  38. public function testBuild() {
  39. $old = App::path('Model');
  40. $expected = array(
  41. APP . 'Model' . DS,
  42. APP . 'models' . DS
  43. );
  44. $this->assertEqual($expected, $old);
  45. App::build(array('Model' => array('/path/to/models/')));
  46. $new = App::path('Model');
  47. $expected = array(
  48. '/path/to/models/',
  49. APP . 'Model' . DS,
  50. APP . 'models' . DS
  51. );
  52. $this->assertEqual($expected, $new);
  53. App::build();
  54. App::build(array('Model' => array('/path/to/models/')), App::PREPEND);
  55. $new = App::path('Model');
  56. $expected = array(
  57. '/path/to/models/',
  58. APP . 'Model' . DS,
  59. APP . 'models' . DS
  60. );
  61. $this->assertEqual($expected, $new);
  62. App::build();
  63. App::build(array('Model' => array('/path/to/models/')), App::APPEND);
  64. $new = App::path('Model');
  65. $expected = array(
  66. APP . 'Model' . DS,
  67. APP . 'models' . DS,
  68. '/path/to/models/'
  69. );
  70. $this->assertEqual($expected, $new);
  71. App::build();
  72. App::build(array(
  73. 'Model' => array('/path/to/models/'),
  74. 'Controller' => array('/path/to/controllers/'),
  75. ), App::APPEND);
  76. $new = App::path('Model');
  77. $expected = array(
  78. APP . 'Model' . DS,
  79. APP . 'models' . DS,
  80. '/path/to/models/'
  81. );
  82. $this->assertEqual($expected, $new);
  83. $new = App::path('Controller');
  84. $expected = array(
  85. APP . 'Controller' . DS,
  86. APP . 'controllers' . DS,
  87. '/path/to/controllers/'
  88. );
  89. $this->assertEqual($expected, $new);
  90. App::build(); //reset defaults
  91. $defaults = App::path('Model');
  92. $this->assertEqual($old, $defaults);
  93. }
  94. /**
  95. * tests that it is possible to set up paths using the cake 1.3 notation for them (models, behaviors, controllers...)
  96. *
  97. * @return void
  98. */
  99. public function testCompatibleBuild() {
  100. $old = App::path('models');
  101. $expected = array(
  102. APP . 'Model' . DS,
  103. APP . 'models' . DS
  104. );
  105. $this->assertEqual($expected, $old);
  106. App::build(array('models' => array('/path/to/models/')));
  107. $new = App::path('models');
  108. $expected = array(
  109. '/path/to/models/',
  110. APP . 'Model' . DS,
  111. APP . 'models' . DS
  112. );
  113. $this->assertEqual($expected, $new);
  114. $this->assertEqual($expected, App::path('Model'));
  115. App::build(array('datasources' => array('/path/to/datasources/')));
  116. $expected = array(
  117. '/path/to/datasources/',
  118. APP . 'Model' . DS . 'Datasource' . DS,
  119. APP . 'models' . DS . 'datasources' . DS
  120. );
  121. $result = App::path('datasources');
  122. $this->assertEqual($expected, $result);
  123. $this->assertEqual($expected, App::path('Model/Datasource'));
  124. App::build(array('behaviors' => array('/path/to/behaviors/')));
  125. $expected = array(
  126. '/path/to/behaviors/',
  127. APP . 'Model' . DS . 'Behavior' . DS,
  128. APP . 'models' . DS . 'behaviors' . DS
  129. );
  130. $result = App::path('behaviors');
  131. $this->assertEqual($expected, $result);
  132. $this->assertEqual($expected, App::path('Model/Behavior'));
  133. App::build(array('controllers' => array('/path/to/controllers/')));
  134. $expected = array(
  135. '/path/to/controllers/',
  136. APP . 'Controller' . DS,
  137. APP . 'controllers' . DS
  138. );
  139. $result = App::path('controllers');
  140. $this->assertEqual($expected, $result);
  141. $this->assertEqual($expected, App::path('Controller'));
  142. App::build(array('components' => array('/path/to/components/')));
  143. $expected = array(
  144. '/path/to/components/',
  145. APP . 'Controller' . DS . 'Component' . DS,
  146. APP . 'controllers' . DS . 'components' . DS
  147. );
  148. $result = App::path('components');
  149. $this->assertEqual($expected, $result);
  150. $this->assertEqual($expected, App::path('Controller/Component'));
  151. App::build(array('views' => array('/path/to/views/')));
  152. $expected = array(
  153. '/path/to/views/',
  154. APP . 'View' . DS,
  155. APP . 'views' . DS
  156. );
  157. $result = App::path('views');
  158. $this->assertEqual($expected, $result);
  159. $this->assertEqual($expected, App::path('View'));
  160. App::build(array('helpers' => array('/path/to/helpers/')));
  161. $expected = array(
  162. '/path/to/helpers/',
  163. APP . 'View' . DS . 'Helper' .DS,
  164. APP . 'views' . DS . 'helpers' . DS
  165. );
  166. $result = App::path('helpers');
  167. $this->assertEqual($expected, $result);
  168. $this->assertEqual($expected, App::path('View/Helper'));
  169. App::build(array('shells' => array('/path/to/shells/')));
  170. $expected = array(
  171. '/path/to/shells/',
  172. APP . 'Console' . DS . 'Command' . DS,
  173. APP . 'console' . DS . 'shells' . DS,
  174. );
  175. $result = App::path('shells');
  176. $this->assertEqual($expected, $result);
  177. $this->assertEqual($expected, App::path('Console/Command'));
  178. App::build(); //reset defaults
  179. $defaults = App::path('Model');
  180. $this->assertEqual($old, $defaults);
  181. }
  182. /**
  183. * test path() with a plugin.
  184. *
  185. * @return void
  186. */
  187. public function testPathWithPlugins() {
  188. $basepath = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  189. App::build(array(
  190. 'Plugin' => array($basepath),
  191. ));
  192. CakePlugin::load('TestPlugin');
  193. $result = App::path('Vendor', 'TestPlugin');
  194. $this->assertEquals($basepath . 'TestPlugin' . DS . 'Vendor' . DS, $result[0]);
  195. }
  196. /**
  197. * testBuildWithReset method
  198. *
  199. * @return void
  200. */
  201. public function testBuildWithReset() {
  202. $old = App::path('Model');
  203. $expected = array(
  204. APP . 'Model' . DS,
  205. APP . 'models' . DS
  206. );
  207. $this->assertEqual($expected, $old);
  208. App::build(array('Model' => array('/path/to/models/')), App::RESET);
  209. $new = App::path('Model');
  210. $expected = array(
  211. '/path/to/models/'
  212. );
  213. $this->assertEqual($expected, $new);
  214. App::build(); //reset defaults
  215. $defaults = App::path('Model');
  216. $this->assertEqual($old, $defaults);
  217. }
  218. /**
  219. * testCore method
  220. *
  221. * @return void
  222. */
  223. public function testCore() {
  224. $model = App::core('Model');
  225. $this->assertEqual(array(CAKE . 'Model' . DS), $model);
  226. $view = App::core('View');
  227. $this->assertEqual(array(CAKE . 'View' . DS), $view);
  228. $controller = App::core('Controller');
  229. $this->assertEqual(array(CAKE . 'Controller' . DS), $controller);
  230. $component = App::core('Controller/Component');
  231. $this->assertEqual(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  232. $auth = App::core('Controller/Component/Auth');
  233. $this->assertEqual(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  234. $datasource = App::core('Model/Datasource');
  235. $this->assertEqual(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  236. }
  237. /**
  238. * testListObjects method
  239. *
  240. * @return void
  241. */
  242. public function testListObjects() {
  243. $result = App::objects('class', CAKE . 'Routing', false);
  244. $this->assertTrue(in_array('Dispatcher', $result));
  245. $this->assertTrue(in_array('Router', $result));
  246. App::build(array(
  247. 'Model/Behavior' => App::core('Model/Behavior'),
  248. 'Controller' => App::core('Controller'),
  249. 'Controller/Component' => App::core('Controller/Component'),
  250. 'View' => App::core('View'),
  251. 'Model' => App::core('Model'),
  252. 'View/Helper' => App::core('View/Helper'),
  253. ), App::RESET);
  254. $result = App::objects('behavior', null, false);
  255. $this->assertTrue(in_array('TreeBehavior', $result));
  256. $result = App::objects('Model/Behavior', null, false);
  257. $this->assertTrue(in_array('TreeBehavior', $result));
  258. $result = App::objects('controller', null, false);
  259. $this->assertTrue(in_array('PagesController', $result));
  260. $result = App::objects('Controller', null, false);
  261. $this->assertTrue(in_array('PagesController', $result));
  262. $result = App::objects('component', null, false);
  263. $this->assertTrue(in_array('AuthComponent', $result));
  264. $result = App::objects('Controller/Component', null, false);
  265. $this->assertTrue(in_array('AuthComponent', $result));
  266. $result = App::objects('view', null, false);
  267. $this->assertTrue(in_array('MediaView', $result));
  268. $result = App::objects('View', null, false);
  269. $this->assertTrue(in_array('MediaView', $result));
  270. $result = App::objects('helper', null, false);
  271. $this->assertTrue(in_array('HtmlHelper', $result));
  272. $result = App::objects('View/Helper', null, false);
  273. $this->assertTrue(in_array('HtmlHelper', $result));
  274. $result = App::objects('model', null, false);
  275. $this->assertTrue(in_array('AcoAction', $result));
  276. $result = App::objects('Model', null, false);
  277. $this->assertTrue(in_array('AcoAction', $result));
  278. $result = App::objects('file');
  279. $this->assertFalse($result);
  280. $result = App::objects('file', 'non_existing_configure');
  281. $expected = array();
  282. $this->assertEqual($expected, $result);
  283. $result = App::objects('NonExistingType');
  284. $this->assertEqual($result, array());
  285. App::build(array(
  286. 'plugins' => array(
  287. CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS
  288. )
  289. ));
  290. $result = App::objects('plugin', null, false);
  291. $this->assertTrue(in_array('Cache', $result));
  292. $this->assertTrue(in_array('Log', $result));
  293. App::build();
  294. }
  295. /**
  296. * Make sure that .svn and friends are excluded from App::objects('plugin')
  297. */
  298. public function testListObjectsIgnoreDotDirectories() {
  299. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  300. App::build(array(
  301. 'plugins' => array($path)
  302. ), App::RESET);
  303. mkdir($path . '.svn');
  304. $result = App::objects('plugin', null, false);
  305. rmdir($path . '.svn');
  306. $this->assertNotContains('.svn', $result);
  307. }
  308. /**
  309. * Tests listing objects within a plugin
  310. *
  311. * @return void
  312. */
  313. public function testListObjectsInPlugin() {
  314. App::build(array(
  315. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  316. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  317. ), App::RESET);
  318. CakePlugin::loadAll();
  319. $result = App::objects('TestPlugin.model');
  320. $this->assertTrue(in_array('TestPluginPost', $result));
  321. $result = App::objects('TestPlugin.Model');
  322. $this->assertTrue(in_array('TestPluginPost', $result));
  323. $result = App::objects('TestPlugin.behavior');
  324. $this->assertTrue(in_array('TestPluginPersisterOne', $result));
  325. $result = App::objects('TestPlugin.Model/Behavior');
  326. $this->assertTrue(in_array('TestPluginPersisterOne', $result));
  327. $result = App::objects('TestPlugin.helper');
  328. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  329. $this->assertEquals($expected, $result);
  330. $result = App::objects('TestPlugin.View/Helper');
  331. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  332. $this->assertEquals($expected, $result);
  333. $result = App::objects('TestPlugin.component');
  334. $this->assertTrue(in_array('OtherComponent', $result));
  335. $result = App::objects('TestPlugin.Controller/Component');
  336. $this->assertTrue(in_array('OtherComponent', $result));
  337. $result = App::objects('TestPluginTwo.behavior');
  338. $this->assertEquals($result, array());
  339. $result = App::objects('TestPluginTwo.Model/Behavior');
  340. $this->assertEquals($result, array());
  341. $result = App::objects('model', null, false);
  342. $this->assertTrue(in_array('Comment', $result));
  343. $this->assertTrue(in_array('Post', $result));
  344. $result = App::objects('Model', null, false);
  345. $this->assertTrue(in_array('Comment', $result));
  346. $this->assertTrue(in_array('Post', $result));
  347. App::build();
  348. }
  349. /**
  350. * test that pluginPath can find paths for plugins.
  351. *
  352. * @return void
  353. */
  354. public function testPluginPath() {
  355. App::build(array(
  356. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  357. ));
  358. CakePlugin::loadAll();
  359. $path = App::pluginPath('TestPlugin');
  360. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  361. $this->assertEqual($path, $expected);
  362. $path = App::pluginPath('TestPluginTwo');
  363. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
  364. $this->assertEqual($path, $expected);
  365. App::build();
  366. }
  367. /**
  368. * test that pluginPath can find paths for plugins.
  369. *
  370. * @return void
  371. */
  372. public function testThemePath() {
  373. App::build(array(
  374. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  375. ));
  376. $path = App::themePath('test_theme');
  377. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  378. $this->assertEqual($path, $expected);
  379. $path = App::themePath('TestTheme');
  380. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  381. $this->assertEqual($path, $expected);
  382. App::build();
  383. }
  384. /**
  385. * testClassLoading method
  386. *
  387. * @return void
  388. */
  389. public function testClassLoading() {
  390. $file = App::import('Model', 'Model', false);
  391. $this->assertTrue($file);
  392. $this->assertTrue(class_exists('Model'));
  393. $file = App::import('Controller', 'Controller', false);
  394. $this->assertTrue($file);
  395. $this->assertTrue(class_exists('Controller'));
  396. $file = App::import('Component', 'Auth', false);
  397. $this->assertTrue($file);
  398. $this->assertTrue(class_exists('AuthComponent'));
  399. $file = App::import('Shell', 'Shell', false);
  400. $this->assertTrue($file);
  401. $this->assertTrue(class_exists('Shell'));
  402. $file = App::import('Configure', 'PhpReader');
  403. $this->assertTrue($file);
  404. $this->assertTrue(class_exists('PhpReader'));
  405. $file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
  406. $this->assertFalse($file);
  407. $file = App::import('Model', 'AppModel', false);
  408. $this->assertTrue($file);
  409. $this->assertTrue(class_exists('AppModel'));
  410. $file = App::import('WrongType', null, true, array(), '');
  411. $this->assertFalse($file);
  412. $file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
  413. $this->assertFalse($file);
  414. $file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
  415. $this->assertFalse($file);
  416. if (!class_exists('AppController', false)) {
  417. $classes = array_flip(get_declared_classes());
  418. $this->assertFalse(isset($classes['PagesController']));
  419. $this->assertFalse(isset($classes['AppController']));
  420. $file = App::import('Controller', 'Pages');
  421. $this->assertTrue($file);
  422. $this->assertTrue(class_exists('PagesController'));
  423. $classes = array_flip(get_declared_classes());
  424. $this->assertTrue(isset($classes['PagesController']));
  425. $this->assertTrue(isset($classes['AppController']));
  426. $file = App::import('Behavior', 'Containable');
  427. $this->assertTrue($file);
  428. $this->assertTrue(class_exists('ContainableBehavior'));
  429. $file = App::import('Component', 'RequestHandler');
  430. $this->assertTrue($file);
  431. $this->assertTrue(class_exists('RequestHandlerComponent'));
  432. $file = App::import('Helper', 'Form');
  433. $this->assertTrue($file);
  434. $this->assertTrue(class_exists('FormHelper'));
  435. $file = App::import('Model', 'NonExistingModel');
  436. $this->assertFalse($file);
  437. $file = App::import('Datasource', 'DboSource');
  438. $this->assertTrue($file);
  439. $this->assertTrue(class_exists('DboSource'));
  440. }
  441. App::build();
  442. }
  443. /**
  444. * test import() with plugins
  445. *
  446. * @return void
  447. */
  448. public function testPluginImporting() {
  449. App::build(array(
  450. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  451. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  452. ));
  453. CakePlugin::loadAll();
  454. $result = App::import('Controller', 'TestPlugin.Tests');
  455. $this->assertTrue($result);
  456. $this->assertTrue(class_exists('TestPluginAppController'));
  457. $this->assertTrue(class_exists('TestsController'));
  458. $result = App::import('Lib', 'TestPlugin.TestPluginLibrary');
  459. $this->assertTrue($result);
  460. $this->assertTrue(class_exists('TestPluginLibrary'));
  461. $result = App::import('Lib', 'Library');
  462. $this->assertTrue($result);
  463. $this->assertTrue(class_exists('Library'));
  464. $result = App::import('Helper', 'TestPlugin.OtherHelper');
  465. $this->assertTrue($result);
  466. $this->assertTrue(class_exists('OtherHelperHelper'));
  467. $result = App::import('Helper', 'TestPlugin.TestPluginApp');
  468. $this->assertTrue($result);
  469. $this->assertTrue(class_exists('TestPluginAppHelper'));
  470. $result = App::import('Datasource', 'TestPlugin.TestSource');
  471. $this->assertTrue($result);
  472. $this->assertTrue(class_exists('TestSource'));
  473. App::build();
  474. }
  475. /**
  476. * test that building helper paths actually works.
  477. *
  478. * @return void
  479. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/410
  480. */
  481. public function testImportingHelpersFromAlternatePaths() {
  482. $this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
  483. App::build(array(
  484. 'View/Helper' => array(
  485. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Helper' . DS
  486. )
  487. ));
  488. $this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
  489. App::import('Helper', 'Banana');
  490. $this->assertTrue(class_exists('BananaHelper', false), 'BananaHelper was not loaded.');
  491. App::build();
  492. }
  493. /**
  494. * testFileLoading method
  495. *
  496. * @return void
  497. */
  498. public function testFileLoading () {
  499. $file = App::import('File', 'RealFile', false, array(), CAKE . 'Config' . DS . 'config.php');
  500. $this->assertTrue($file);
  501. $file = App::import('File', 'NoFile', false, array(), CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
  502. $this->assertFalse($file);
  503. }
  504. /**
  505. * testFileLoadingWithArray method
  506. *
  507. * @return void
  508. */
  509. public function testFileLoadingWithArray() {
  510. $type = array('type' => 'File', 'name' => 'SomeName', 'parent' => false,
  511. 'file' => CAKE . DS . 'Config' . DS . 'config.php');
  512. $file = App::import($type);
  513. $this->assertTrue($file);
  514. $type = array('type' => 'File', 'name' => 'NoFile', 'parent' => false,
  515. 'file' => CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
  516. $file = App::import($type);
  517. $this->assertFalse($file);
  518. }
  519. /**
  520. * testFileLoadingReturnValue method
  521. *
  522. * @return void
  523. */
  524. public function testFileLoadingReturnValue () {
  525. $file = App::import('File', 'Name', false, array(), CAKE . 'Config' . DS . 'config.php', true);
  526. $this->assertTrue(!empty($file));
  527. $this->assertTrue(isset($file['Cake.version']));
  528. $type = array('type' => 'File', 'name' => 'OtherName', 'parent' => false,
  529. 'file' => CAKE . 'Config' . DS . 'config.php', 'return' => true);
  530. $file = App::import($type);
  531. $this->assertTrue(!empty($file));
  532. $this->assertTrue(isset($file['Cake.version']));
  533. }
  534. /**
  535. * testLoadingWithSearch method
  536. *
  537. * @return void
  538. */
  539. public function testLoadingWithSearch () {
  540. $file = App::import('File', 'NewName', false, array(CAKE . 'Config' . DS), 'config.php');
  541. $this->assertTrue($file);
  542. $file = App::import('File', 'AnotherNewName', false, array(CAKE), 'config.php');
  543. $this->assertFalse($file);
  544. }
  545. /**
  546. * testLoadingWithSearchArray method
  547. *
  548. * @return void
  549. */
  550. public function testLoadingWithSearchArray() {
  551. $type = array(
  552. 'type' => 'File',
  553. 'name' => 'RandomName',
  554. 'parent' => false,
  555. 'file' => 'config.php',
  556. 'search' => array(CAKE . 'Config' . DS)
  557. );
  558. $file = App::import($type);
  559. $this->assertTrue($file);
  560. $type = array(
  561. 'type' => 'File',
  562. 'name' => 'AnotherRandomName',
  563. 'parent' => false,
  564. 'file' => 'config.php',
  565. 'search' => array(CAKE)
  566. );
  567. $file = App::import($type);
  568. $this->assertFalse($file);
  569. }
  570. /**
  571. * testMultipleLoading method
  572. *
  573. * @return void
  574. */
  575. public function testMultipleLoading() {
  576. if (class_exists('PersisterOne', false) || class_exists('PersisterTwo', false)) {
  577. $this->markTestSkipped('Cannot test loading of classes that exist.');
  578. }
  579. App::build(array(
  580. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
  581. ));
  582. $toLoad = array('PersisterOne', 'PersisterTwo');
  583. $load = App::import('Model', $toLoad);
  584. $this->assertTrue($load);
  585. $classes = array_flip(get_declared_classes());
  586. $this->assertTrue(isset($classes['PersisterOne']));
  587. $this->assertTrue(isset($classes['PersisterTwo']));
  588. $load = App::import('Model', array('PersisterOne', 'SomeNotFoundClass', 'PersisterTwo'));
  589. $this->assertFalse($load);
  590. }
  591. public function testLoadingVendor() {
  592. App::build(array(
  593. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  594. 'vendors' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor'. DS),
  595. ), App::RESET);
  596. CakePlugin::loadAll();
  597. ob_start();
  598. $result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
  599. $text = ob_get_clean();
  600. $this->assertTrue($result);
  601. $this->assertEqual($text, 'this is the test asset css file');
  602. $result = App::import('Vendor', 'TestPlugin.sample/SamplePlugin');
  603. $this->assertTrue($result);
  604. $this->assertTrue(class_exists('SamplePluginClassTestName'));
  605. $result = App::import('Vendor', 'sample/ConfigureTestVendorSample');
  606. $this->assertTrue($result);
  607. $this->assertTrue(class_exists('ConfigureTestVendorSample'));
  608. ob_start();
  609. $result = App::import('Vendor', 'SomeNameInSubfolder', array('file' => 'somename/some.name.php'));
  610. $text = ob_get_clean();
  611. $this->assertTrue($result);
  612. $this->assertEqual($text, 'This is a file with dot in file name');
  613. ob_start();
  614. $result = App::import('Vendor', 'TestHello', array('file' => 'Test'.DS.'hello.php'));
  615. $text = ob_get_clean();
  616. $this->assertTrue($result);
  617. $this->assertEqual($text, 'This is the hello.php file in Test directory');
  618. ob_start();
  619. $result = App::import('Vendor', 'MyTest', array('file' => 'Test'.DS.'MyTest.php'));
  620. $text = ob_get_clean();
  621. $this->assertTrue($result);
  622. $this->assertEqual($text, 'This is the MyTest.php file');
  623. ob_start();
  624. $result = App::import('Vendor', 'Welcome');
  625. $text = ob_get_clean();
  626. $this->assertTrue($result);
  627. $this->assertEqual($text, 'This is the welcome.php file in vendors directory');
  628. ob_start();
  629. $result = App::import('Vendor', 'TestPlugin.Welcome');
  630. $text = ob_get_clean();
  631. $this->assertTrue($result);
  632. $this->assertEqual($text, 'This is the welcome.php file in test_plugin/vendors directory');
  633. }
  634. /**
  635. * Tests that the automatic class loader will also find in "libs" folder for both
  636. * app and plugins if it does not find the class in other configured paths
  637. *
  638. */
  639. public function testLoadClassInLibs() {
  640. App::build(array(
  641. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  642. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  643. ), App::RESET);
  644. CakePlugin::loadAll();
  645. $this->assertFalse(class_exists('CustomLibClass', false));
  646. App::uses('CustomLibClass', 'TestPlugin.Custom/Package');
  647. $this->assertTrue(class_exists('CustomLibClass'));
  648. $this->assertFalse(class_exists('TestUtilityClass', false));
  649. App::uses('TestUtilityClass', 'Utility');
  650. $this->assertTrue(class_exists('CustomLibClass'));
  651. }
  652. /**
  653. * Tests that App::location() returns the defined path for a class
  654. *
  655. * @return void
  656. */
  657. public function testClassLocation() {
  658. App::uses('MyCustomClass', 'MyPackage/Name');
  659. $this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
  660. }
  661. /**
  662. * Test that paths() works.
  663. *
  664. * @return void
  665. */
  666. public function testPaths() {
  667. $result = App::paths();
  668. $this->assertArrayHasKey('Plugin', $result);
  669. $this->assertArrayHasKey('Controller', $result);
  670. $this->assertArrayHasKey('Controller/Component', $result);
  671. }
  672. }