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

/cake/tests/cases/console/libs/tasks/view.test.php

https://github.com/msadouni/cakephp2x
PHP | 604 lines | 287 code | 71 blank | 246 comment | 2 complexity | 49c9ed7571b02e138b9dd2fbeeff1f5d MD5 | raw file
  1. <?php
  2. /**
  3. * ViewTask Test file
  4. *
  5. * Test Case for view generation shell task
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2006-2009, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake
  18. * @subpackage cake.tests.cases.console.libs.tasks
  19. * @since CakePHP v 1.2.0.7726
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::import('Shell', 'Shell', false);
  23. if (!defined('DISABLE_AUTO_DISPATCH')) {
  24. define('DISABLE_AUTO_DISPATCH', true);
  25. }
  26. if (!class_exists('ShellDispatcher')) {
  27. ob_start();
  28. $argv = false;
  29. require CAKE . 'console' . DS . 'cake.php';
  30. ob_end_clean();
  31. }
  32. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'view.php';
  33. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'controller.php';
  34. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
  35. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'project.php';
  36. Mock::generatePartial(
  37. 'ShellDispatcher', 'TestViewTaskMockShellDispatcher',
  38. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  39. );
  40. Mock::generatePartial(
  41. 'ViewTask', 'MockViewTask',
  42. array('in', '_stop', 'err', 'out', 'createFile')
  43. );
  44. Mock::generate('ControllerTask', 'ViewTaskMockControllerTask');
  45. Mock::generate('ProjectTask', 'ViewTaskMockProjectTask');
  46. /**
  47. * Test View Task Comment Model
  48. *
  49. * @package cake
  50. * @subpackage cake.tests.cases.console.libs.tasks
  51. */
  52. class ViewTaskComment extends Model {
  53. /**
  54. * Model name
  55. *
  56. * @var string
  57. * @access public
  58. */
  59. var $name = 'ViewTaskComment';
  60. /**
  61. * Table name
  62. *
  63. * @var string
  64. * @access public
  65. */
  66. var $useTable = 'comments';
  67. /**
  68. * Belongs To Associations
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. var $belongsTo = array(
  74. 'Article' => array(
  75. 'className' => 'ViewTaskArticle',
  76. 'foreignKey' => 'article_id'
  77. )
  78. );
  79. }
  80. /**
  81. * Test View Task Article Model
  82. *
  83. * @package cake
  84. * @subpackage cake.tests.cases.console.libs.tasks
  85. */
  86. class ViewTaskArticle extends Model {
  87. /**
  88. * Model name
  89. *
  90. * @var string
  91. * @access public
  92. */
  93. var $name = 'ViewTaskArticle';
  94. /**
  95. * Table name
  96. *
  97. * @var string
  98. * @access public
  99. */
  100. var $useTable = 'articles';
  101. }
  102. /**
  103. * Test View Task Comments Controller
  104. *
  105. * @package cake
  106. * @subpackage cake.tests.cases.console.libs.tasks
  107. */
  108. class ViewTaskCommentsController extends Controller {
  109. /**
  110. * Controller name
  111. *
  112. * @var string
  113. * @access public
  114. */
  115. var $name = 'ViewTaskComments';
  116. /**
  117. * Testing public controller action
  118. *
  119. * @return void
  120. * @access public
  121. */
  122. function index() {
  123. }
  124. /**
  125. * Testing public controller action
  126. *
  127. * @return void
  128. * @access public
  129. */
  130. function add() {
  131. }
  132. }
  133. /**
  134. * Test View Task Articles Controller
  135. *
  136. * @package cake
  137. * @subpackage cake.tests.cases.console.libs.tasks
  138. */
  139. class ViewTaskArticlesController extends Controller {
  140. /**
  141. * Controller name
  142. *
  143. * @var string
  144. * @access public
  145. */
  146. var $name = 'ViewTaskArticles';
  147. /**
  148. * Test public controller action
  149. *
  150. * @return void
  151. * @access public
  152. */
  153. function index() {
  154. }
  155. /**
  156. * Test public controller action
  157. *
  158. * @return void
  159. * @access public
  160. */
  161. function add() {
  162. }
  163. /**
  164. * Test admin prefixed controller action
  165. *
  166. * @return void
  167. * @access public
  168. */
  169. function admin_index() {
  170. }
  171. /**
  172. * Test admin prefixed controller action
  173. *
  174. * @return void
  175. * @access public
  176. */
  177. function admin_add() {
  178. }
  179. /**
  180. * Test admin prefixed controller action
  181. *
  182. * @return void
  183. * @access public
  184. */
  185. function admin_view() {
  186. }
  187. /**
  188. * Test admin prefixed controller action
  189. *
  190. * @return void
  191. * @access public
  192. */
  193. function admin_edit() {
  194. }
  195. /**
  196. * Test admin prefixed controller action
  197. *
  198. * @return void
  199. * @access public
  200. */
  201. function admin_delete() {
  202. }
  203. }
  204. /**
  205. * ViewTaskTest class
  206. *
  207. * @package cake
  208. * @subpackage cake.tests.cases.console.libs.tasks
  209. */
  210. class ViewTaskTest extends CakeTestCase {
  211. /**
  212. * Fixtures
  213. *
  214. * @var array
  215. * @access public
  216. */
  217. var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
  218. /**
  219. * startTest method
  220. *
  221. * Ensure that the default theme is used
  222. *
  223. * @return void
  224. * @access public
  225. */
  226. function startTest() {
  227. $this->Dispatcher =& new TestViewTaskMockShellDispatcher();
  228. $this->Dispatcher->shellPaths = App::path('shells');
  229. $this->Task =& new MockViewTask($this->Dispatcher);
  230. $this->Task->Dispatch =& $this->Dispatcher;
  231. $this->Task->Template =& new TemplateTask($this->Dispatcher);
  232. $this->Task->Controller =& new ViewTaskMockControllerTask();
  233. $this->Task->Project =& new ViewTaskMockProjectTask();
  234. $this->Task->path = TMP;
  235. $this->Task->Template->params['theme'] = 'default';
  236. }
  237. /**
  238. * endTest method
  239. *
  240. * @return void
  241. * @access public
  242. */
  243. function endTest() {
  244. ClassRegistry::flush();
  245. }
  246. /**
  247. * Test getContent and parsing of Templates.
  248. *
  249. * @return void
  250. * @access public
  251. */
  252. function testGetContent() {
  253. $vars = array(
  254. 'modelClass' => 'TestViewModel',
  255. 'schema' => array(),
  256. 'primaryKey' => 'id',
  257. 'displayField' => 'name',
  258. 'singularVar' => 'testViewModel',
  259. 'pluralVar' => 'testViewModels',
  260. 'singularHumanName' => 'Test View Model',
  261. 'pluralHumanName' => 'Test View Models',
  262. 'fields' => array('id', 'name', 'body'),
  263. 'associations' => array()
  264. );
  265. $result = $this->Task->getContent('view', $vars);
  266. $this->assertPattern('/Delete .+Test View Model/', $result);
  267. $this->assertPattern('/Edit .+Test View Model/', $result);
  268. $this->assertPattern('/List .+Test View Models/', $result);
  269. $this->assertPattern('/New .+Test View Model/', $result);
  270. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  271. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  272. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  273. }
  274. /**
  275. * test getContent() using an admin_prefixed action.
  276. *
  277. * @return void
  278. * @access public
  279. */
  280. function testGetContentWithAdminAction() {
  281. $_back = Configure::read('Routing');
  282. Configure::write('Routing.prefixes', array('admin'));
  283. $vars = array(
  284. 'modelClass' => 'TestViewModel',
  285. 'schema' => array(),
  286. 'primaryKey' => 'id',
  287. 'displayField' => 'name',
  288. 'singularVar' => 'testViewModel',
  289. 'pluralVar' => 'testViewModels',
  290. 'singularHumanName' => 'Test View Model',
  291. 'pluralHumanName' => 'Test View Models',
  292. 'fields' => array('id', 'name', 'body'),
  293. 'associations' => array()
  294. );
  295. $result = $this->Task->getContent('admin_view', $vars);
  296. $this->assertPattern('/Delete .+Test View Model/', $result);
  297. $this->assertPattern('/Edit .+Test View Model/', $result);
  298. $this->assertPattern('/List .+Test View Models/', $result);
  299. $this->assertPattern('/New .+Test View Model/', $result);
  300. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  301. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  302. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  303. $result = $this->Task->getContent('admin_add', $vars);
  304. $this->assertPattern("/input\('name'\)/", $result);
  305. $this->assertPattern("/input\('body'\)/", $result);
  306. $this->assertPattern('/List .+Test View Models/', $result);
  307. Configure::write('Routing', $_back);
  308. }
  309. /**
  310. * test Bake method
  311. *
  312. * @return void
  313. * @access public
  314. */
  315. function testBake() {
  316. $this->Task->controllerName = 'ViewTaskComments';
  317. $this->Task->controllerPath = 'view_task_comments';
  318. $this->Task->expectAt(0, 'createFile', array(
  319. TMP . 'view_task_comments' . DS . 'view.ctp',
  320. new PatternExpectation('/View Task Articles/')
  321. ));
  322. $this->Task->bake('view', true);
  323. $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'edit.ctp', '*'));
  324. $this->Task->bake('edit', true);
  325. $this->Task->expectAt(2, 'createFile', array(
  326. TMP . 'view_task_comments' . DS . 'index.ctp',
  327. new PatternExpectation('/\$viewTaskComment\[\'Article\'\]\[\'title\'\]/')
  328. ));
  329. $this->Task->bake('index', true);
  330. }
  331. /**
  332. * test bake() with a -plugin param
  333. *
  334. * @return void
  335. * @access public
  336. */
  337. function testBakeWithPlugin() {
  338. $this->Task->controllerName = 'ViewTaskComments';
  339. $this->Task->controllerPath = 'view_task_comments';
  340. $this->Task->plugin = 'TestTest';
  341. $path = APP . 'plugins' . DS . 'test_test' . DS . 'views' . DS . 'view_task_comments' . DS . 'view.ctp';
  342. $this->Task->expectAt(0, 'createFile', array($path, '*'));
  343. $this->Task->bake('view', true);
  344. }
  345. /**
  346. * test bake actions baking multiple actions.
  347. *
  348. * @return void
  349. * @access public
  350. */
  351. function testBakeActions() {
  352. $this->Task->controllerName = 'ViewTaskComments';
  353. $this->Task->controllerPath = 'view_task_comments';
  354. $this->Task->expectAt(0, 'createFile', array(
  355. TMP . 'view_task_comments' . DS . 'view.ctp',
  356. new PatternExpectation('/View Task Comments/')
  357. ));
  358. $this->Task->expectAt(1, 'createFile', array(
  359. TMP . 'view_task_comments' . DS . 'edit.ctp',
  360. new PatternExpectation('/Edit .+View Task Comment/')
  361. ));
  362. $this->Task->expectAt(2, 'createFile', array(
  363. TMP . 'view_task_comments' . DS . 'index.ctp',
  364. new PatternExpectation('/ViewTaskComment/')
  365. ));
  366. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  367. }
  368. /**
  369. * test baking a customAction (non crud)
  370. *
  371. * @return void
  372. * @access public
  373. */
  374. function testCustomAction() {
  375. $this->Task->controllerName = 'ViewTaskComments';
  376. $this->Task->controllerPath = 'view_task_comments';
  377. $this->Task->params['app'] = APP;
  378. $this->Task->setReturnValueAt(0, 'in', '');
  379. $this->Task->setReturnValueAt(1, 'in', 'my_action');
  380. $this->Task->setReturnValueAt(2, 'in', 'y');
  381. $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'my_action.ctp', '*'));
  382. $this->Task->customAction();
  383. }
  384. /**
  385. * Test all()
  386. *
  387. * @return void
  388. * @access public
  389. */
  390. function testExecuteIntoAll() {
  391. $this->Task->args[0] = 'all';
  392. $this->Task->Controller->setReturnValue('listAll', array('view_task_comments'));
  393. $this->Task->Controller->expectOnce('listAll');
  394. $this->Task->expectCallCount('createFile', 2);
  395. $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
  396. $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
  397. $this->Task->execute();
  398. }
  399. /**
  400. * test `cake bake view $controller view`
  401. *
  402. * @return void
  403. * @access public
  404. */
  405. function testExecuteWithActionParam() {
  406. $this->Task->args[0] = 'ViewTaskComments';
  407. $this->Task->args[1] = 'view';
  408. $this->Task->expectCallCount('createFile', 1);
  409. $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*'));
  410. $this->Task->execute();
  411. }
  412. /**
  413. * test `cake bake view $controller`
  414. * Ensure that views are only baked for actions that exist in the controller.
  415. *
  416. * @return void
  417. * @access public
  418. */
  419. function testExecuteWithController() {
  420. $this->Task->args[0] = 'ViewTaskComments';
  421. $this->Task->expectCallCount('createFile', 2);
  422. $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
  423. $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
  424. $this->Task->execute();
  425. }
  426. /**
  427. * test `cake bake view $controller -admin`
  428. * Which only bakes admin methods, not non-admin methods.
  429. *
  430. * @return void
  431. * @access public
  432. */
  433. function testExecuteWithControllerAndAdminFlag() {
  434. $_back = Configure::read('Routing');
  435. Configure::write('Routing.prefixes', array('admin'));
  436. $this->Task->args[0] = 'ViewTaskArticles';
  437. $this->Task->params['admin'] = 1;
  438. $this->Task->Project->setReturnValue('getPrefix', 'admin_');
  439. $this->Task->expectCallCount('createFile', 4);
  440. $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_index.ctp', '*'));
  441. $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_add.ctp', '*'));
  442. $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_view.ctp', '*'));
  443. $this->Task->expectAt(3, 'createFile', array(TMP . 'view_task_articles' . DS . 'admin_edit.ctp', '*'));
  444. $this->Task->execute();
  445. Configure::write('Routing', $_back);
  446. }
  447. /**
  448. * test execute into interactive.
  449. *
  450. * @return void
  451. * @access public
  452. */
  453. function testExecuteInteractive() {
  454. $this->Task->connection = 'test_suite';
  455. $this->Task->args = array();
  456. $this->Task->params = array();
  457. $this->Task->Controller->setReturnValue('getName', 'ViewTaskComments');
  458. $this->Task->setReturnValue('in', 'y');
  459. $this->Task->setReturnValueAt(0, 'in', 'y');
  460. $this->Task->setReturnValueAt(1, 'in', 'y');
  461. $this->Task->setReturnValueAt(2, 'in', 'n');
  462. $this->Task->expectCallCount('createFile', 4);
  463. $this->Task->expectAt(0, 'createFile', array(
  464. TMP . 'view_task_comments' . DS . 'index.ctp',
  465. new PatternExpectation('/ViewTaskComment/')
  466. ));
  467. $this->Task->expectAt(1, 'createFile', array(
  468. TMP . 'view_task_comments' . DS . 'view.ctp',
  469. new PatternExpectation('/ViewTaskComment/')
  470. ));
  471. $this->Task->expectAt(2, 'createFile', array(
  472. TMP . 'view_task_comments' . DS . 'add.ctp',
  473. new PatternExpectation('/Add .+View Task Comment/')
  474. ));
  475. $this->Task->expectAt(3, 'createFile', array(
  476. TMP . 'view_task_comments' . DS . 'edit.ctp',
  477. new PatternExpectation('/Edit .+View Task Comment/')
  478. ));
  479. $this->Task->execute();
  480. }
  481. /**
  482. * test `cake bake view posts index list`
  483. *
  484. * @return void
  485. * @access public
  486. */
  487. function testExecuteWithAlternateTemplates() {
  488. $this->Task->connection = 'test_suite';
  489. $this->Task->args = array('ViewTaskComments', 'index', 'list');
  490. $this->Task->params = array();
  491. $this->Task->expectCallCount('createFile', 1);
  492. $this->Task->expectAt(0, 'createFile', array(
  493. TMP . 'view_task_comments' . DS . 'list.ctp',
  494. new PatternExpectation('/ViewTaskComment/')
  495. ));
  496. $this->Task->execute();
  497. }
  498. /**
  499. * test execute into interactive() with admin methods.
  500. *
  501. * @return void
  502. * @access public
  503. */
  504. function testExecuteInteractiveWithAdmin() {
  505. Configure::write('Routing.prefixes', array('admin'));
  506. $this->Task->connection = 'test_suite';
  507. $this->Task->args = array();
  508. $this->Task->Controller->setReturnValue('getName', 'ViewTaskComments');
  509. $this->Task->Project->setReturnValue('getPrefix', 'admin_');
  510. $this->Task->setReturnValueAt(0, 'in', 'y');
  511. $this->Task->setReturnValueAt(1, 'in', 'n');
  512. $this->Task->setReturnValueAt(2, 'in', 'y');
  513. $this->Task->expectCallCount('createFile', 4);
  514. $this->Task->expectAt(0, 'createFile', array(
  515. TMP . 'view_task_comments' . DS . 'admin_index.ctp',
  516. new PatternExpectation('/ViewTaskComment/')
  517. ));
  518. $this->Task->expectAt(1, 'createFile', array(
  519. TMP . 'view_task_comments' . DS . 'admin_view.ctp',
  520. new PatternExpectation('/ViewTaskComment/')
  521. ));
  522. $this->Task->expectAt(2, 'createFile', array(
  523. TMP . 'view_task_comments' . DS . 'admin_add.ctp',
  524. new PatternExpectation('/Add .+View Task Comment/')
  525. ));
  526. $this->Task->expectAt(3, 'createFile', array(
  527. TMP . 'view_task_comments' . DS . 'admin_edit.ctp',
  528. new PatternExpectation('/Edit .+View Task Comment/')
  529. ));
  530. $this->Task->execute();
  531. }
  532. }
  533. ?>