ProjectTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * ProjectTask Test file
  4. *
  5. * Test Case for project generation shell task
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP Project
  18. * @package Cake.Test.Case.Console.Command.Task
  19. * @since CakePHP v 1.3.0
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('ShellDispatcher', 'Console');
  23. App::uses('ConsoleOutput', 'Console');
  24. App::uses('ConsoleInput', 'Console');
  25. App::uses('Shell', 'Console');
  26. App::uses('ProjectTask', 'Console/Command/Task');
  27. App::uses('Folder', 'Utility');
  28. App::uses('File', 'Utility');
  29. /**
  30. * ProjectTask Test class
  31. *
  32. * @package Cake.Test.Case.Console.Command.Task
  33. */
  34. class ProjectTaskTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @return void
  39. */
  40. public function setUp() {
  41. parent::setUp();
  42. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  43. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  44. $this->Task = $this->getMock('ProjectTask',
  45. array('in', 'err', 'createFile', '_stop'),
  46. array($out, $out, $in)
  47. );
  48. $this->Task->path = TMP . 'tests' . DS;
  49. }
  50. /**
  51. * tearDown method
  52. *
  53. * @return void
  54. */
  55. public function tearDown() {
  56. parent::tearDown();
  57. $Folder = new Folder($this->Task->path . 'bake_test_app');
  58. $Folder->delete();
  59. unset($this->Task);
  60. }
  61. /**
  62. * creates a test project that is used for testing project task.
  63. *
  64. * @return void
  65. */
  66. protected function _setupTestProject() {
  67. $skel = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  68. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  69. $this->Task->bake($this->Task->path . 'bake_test_app', $skel);
  70. }
  71. /**
  72. * test bake() method and directory creation.
  73. *
  74. * @return void
  75. */
  76. public function testBake() {
  77. $this->_setupTestProject();
  78. $path = $this->Task->path . 'bake_test_app';
  79. $this->assertTrue(is_dir($path), 'No project dir %s');
  80. $dirs = array(
  81. 'Config',
  82. 'Config' . DS . 'Schema',
  83. 'Console',
  84. 'Console' . DS . 'Command',
  85. 'Console' . DS . 'Templates',
  86. 'Console' . DS . 'Command' . DS . 'Task',
  87. 'Controller',
  88. 'Controller' . DS . 'Component',
  89. 'Locale',
  90. 'Model',
  91. 'Model' . DS . 'Behavior',
  92. 'Model' . DS . 'Datasource',
  93. 'Plugin',
  94. 'Test',
  95. 'Test' . DS . 'Case',
  96. 'Test' . DS . 'Case' . DS . 'Controller',
  97. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  98. 'Test' . DS . 'Case' . DS . 'Model',
  99. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  100. 'Test' . DS . 'Fixture',
  101. 'Vendor',
  102. 'View',
  103. 'View' . DS . 'Helper',
  104. 'tmp',
  105. 'tmp' . DS . 'cache',
  106. 'tmp' . DS . 'cache' . DS . 'models',
  107. 'tmp' . DS . 'cache' . DS . 'persistent',
  108. 'tmp' . DS . 'cache' . DS . 'views',
  109. 'tmp' . DS . 'logs',
  110. 'tmp' . DS . 'sessions',
  111. 'tmp' . DS . 'tests',
  112. 'webroot',
  113. 'webroot' . DS . 'css',
  114. 'webroot' . DS . 'files',
  115. 'webroot' . DS . 'img',
  116. 'webroot' . DS . 'js',
  117. );
  118. foreach ($dirs as $dir) {
  119. $this->assertTrue(is_dir($path . DS . $dir), 'Missing ' . $dir);
  120. }
  121. }
  122. /**
  123. * test bake with an absolute path.
  124. *
  125. * @return void
  126. */
  127. public function testExecuteWithAbsolutePath() {
  128. $path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  129. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  130. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  131. $this->Task->execute();
  132. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  133. $File = new File($path . DS . 'webroot' . DS . 'index.php');
  134. $contents = $File->read();
  135. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  136. $File = new File($path . DS . 'webroot' . DS . 'test.php');
  137. $contents = $File->read();
  138. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  139. }
  140. /**
  141. * test bake with CakePHP on the include path. The constants should remain commented out.
  142. *
  143. * @return void
  144. */
  145. public function testExecuteWithCakeOnIncludePath() {
  146. if (!function_exists('ini_set')) {
  147. $this->markTestAsSkipped('Not access to ini_set, cannot proceed.');
  148. }
  149. $restore = ini_get('include_path');
  150. ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . $restore);
  151. $path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  152. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  153. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  154. $this->Task->execute();
  155. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  156. $contents = file_get_contents($path . DS . 'webroot' . DS . 'index.php');
  157. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  158. $contents = file_get_contents($path . DS . 'webroot' . DS . 'test.php');
  159. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  160. ini_set('include_path', $restore);
  161. }
  162. /**
  163. * test bake() method with -empty flag, directory creation and empty files.
  164. *
  165. * @return void
  166. */
  167. public function testBakeEmptyFlag() {
  168. $this->Task->params['empty'] = true;
  169. $this->_setupTestProject();
  170. $path = $this->Task->path . 'bake_test_app';
  171. $empty = array(
  172. 'Console' . DS . 'Command' . DS . 'Task' => 'empty',
  173. 'Controller' . DS . 'Component' => 'empty',
  174. 'Lib' => 'empty',
  175. 'Model' . DS . 'Behavior' => 'empty',
  176. 'Model' . DS . 'Datasource' => 'empty',
  177. 'Plugin' => 'empty',
  178. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior' => 'empty',
  179. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component' => 'empty',
  180. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' => 'empty',
  181. 'Test' . DS . 'Fixture' => 'empty',
  182. 'Vendor' => 'empty',
  183. 'View' . DS . 'Elements' => 'empty',
  184. 'View' . DS . 'Scaffolds' => 'empty',
  185. 'tmp' . DS . 'cache' . DS . 'models' => 'empty',
  186. 'tmp' . DS . 'cache' . DS . 'persistent' => 'empty',
  187. 'tmp' . DS . 'cache' . DS . 'views' => 'empty',
  188. 'tmp' . DS . 'logs' => 'empty',
  189. 'tmp' . DS . 'sessions' => 'empty',
  190. 'tmp' . DS . 'tests' => 'empty',
  191. 'webroot' . DS . 'js' => 'empty',
  192. 'webroot' . DS . 'files' => 'empty'
  193. );
  194. foreach ($empty as $dir => $file) {
  195. $this->assertTrue(is_file($path . DS . $dir . DS . $file), sprintf('Missing %s file in %s', $file, $dir));
  196. }
  197. }
  198. /**
  199. * test generation of Security.salt
  200. *
  201. * @return void
  202. */
  203. public function testSecuritySaltGeneration() {
  204. $this->_setupTestProject();
  205. $path = $this->Task->path . 'bake_test_app' . DS;
  206. $result = $this->Task->securitySalt($path);
  207. $this->assertTrue($result);
  208. $File = new File($path . 'Config' . DS . 'core.php');
  209. $contents = $File->read();
  210. $this->assertNotRegExp('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  211. }
  212. /**
  213. * test generation of Security.cipherSeed
  214. *
  215. * @return void
  216. */
  217. public function testSecurityCipherSeedGeneration() {
  218. $this->_setupTestProject();
  219. $path = $this->Task->path . 'bake_test_app' . DS;
  220. $result = $this->Task->securityCipherSeed($path);
  221. $this->assertTrue($result);
  222. $File = new File($path . 'Config' . DS . 'core.php');
  223. $contents = $File->read();
  224. $this->assertNotRegExp('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
  225. }
  226. /**
  227. * test generation of cache prefix
  228. *
  229. * @return void
  230. */
  231. public function testCachePrefixGeneration() {
  232. $this->_setupTestProject();
  233. $path = $this->Task->path . 'bake_test_app' . DS;
  234. $result = $this->Task->cachePrefix($path);
  235. $this->assertTrue($result);
  236. $File = new File($path . 'Config' . DS . 'core.php');
  237. $contents = $File->read();
  238. $this->assertRegExp('/\$prefix = \'.+\';/', $contents, '$prefix is not defined');
  239. $this->assertNotRegExp('/\$prefix = \'myapp_\';/', $contents, 'Default cache prefix left behind. %s');
  240. }
  241. /**
  242. * Test that index.php is generated correctly.
  243. *
  244. * @return void
  245. */
  246. public function testIndexPhpGeneration() {
  247. $this->_setupTestProject();
  248. $path = $this->Task->path . 'bake_test_app' . DS;
  249. $this->Task->corePath($path);
  250. $File = new File($path . 'webroot' . DS . 'index.php');
  251. $contents = $File->read();
  252. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  253. $File = new File($path . 'webroot' . DS . 'test.php');
  254. $contents = $File->read();
  255. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  256. }
  257. /**
  258. * test getPrefix method, and that it returns Routing.prefix or writes to config file.
  259. *
  260. * @return void
  261. */
  262. public function testGetPrefix() {
  263. Configure::write('Routing.prefixes', array('admin'));
  264. $result = $this->Task->getPrefix();
  265. $this->assertEquals('admin_', $result);
  266. Configure::write('Routing.prefixes', null);
  267. $this->_setupTestProject();
  268. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  269. $this->Task->expects($this->once())->method('in')->will($this->returnValue('super_duper_admin'));
  270. $result = $this->Task->getPrefix();
  271. $this->assertEquals('super_duper_admin_', $result);
  272. $File = new File($this->Task->configPath . 'core.php');
  273. $File->delete();
  274. }
  275. /**
  276. * test cakeAdmin() writing core.php
  277. *
  278. * @return void
  279. */
  280. public function testCakeAdmin() {
  281. $File = new File(APP . 'Config' . DS . 'core.php');
  282. $contents = $File->read();
  283. $File = new File(TMP . 'tests' . DS . 'core.php');
  284. $File->write($contents);
  285. Configure::write('Routing.prefixes', null);
  286. $this->Task->configPath = TMP . 'tests' . DS;
  287. $result = $this->Task->cakeAdmin('my_prefix');
  288. $this->assertTrue($result);
  289. $this->assertEquals(Configure::read('Routing.prefixes'), array('my_prefix'));
  290. $File->delete();
  291. }
  292. /**
  293. * test getting the prefix with more than one prefix setup
  294. *
  295. * @return void
  296. */
  297. public function testGetPrefixWithMultiplePrefixes() {
  298. Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
  299. $this->_setupTestProject();
  300. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  301. $this->Task->expects($this->once())->method('in')->will($this->returnValue(2));
  302. $result = $this->Task->getPrefix();
  303. $this->assertEquals('ninja_', $result);
  304. }
  305. /**
  306. * Test execute method with one param to destination folder.
  307. *
  308. * @return void
  309. */
  310. public function testExecute() {
  311. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  312. $this->Task->params['working'] = TMP . 'tests' . DS;
  313. $path = $this->Task->path . 'bake_test_app';
  314. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($path));
  315. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  316. $this->Task->execute();
  317. $this->assertTrue(is_dir($path), 'No project dir');
  318. $this->assertTrue(is_dir($path . DS . 'Controller'), 'No controllers dir ');
  319. $this->assertTrue(is_dir($path . DS . 'Controller' . DS . 'Component'), 'No components dir ');
  320. $this->assertTrue(is_dir($path . DS . 'Model'), 'No models dir');
  321. $this->assertTrue(is_dir($path . DS . 'View'), 'No views dir');
  322. $this->assertTrue(is_dir($path . DS . 'View' . DS . 'Helper'), 'No helpers dir');
  323. $this->assertTrue(is_dir($path . DS . 'Test'), 'No tests dir');
  324. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Case'), 'No cases dir');
  325. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Fixture'), 'No fixtures dir');
  326. }
  327. /**
  328. * test console path
  329. *
  330. * @return void
  331. */
  332. public function testConsolePath() {
  333. $this->_setupTestProject();
  334. $path = $this->Task->path . 'bake_test_app' . DS;
  335. $result = $this->Task->consolePath($path);
  336. $this->assertTrue($result);
  337. $File = new File($path . 'Console' . DS . 'cake.php');
  338. $contents = $File->read();
  339. $this->assertNotRegExp('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.');
  340. }
  341. }