ViewTaskTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. /**
  3. * ViewTask Test file
  4. *
  5. * Test Case for view generation shell task
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @package Cake.Test.Case.Console.Command.Task
  17. * @since CakePHP v 1.2.0.7726
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ShellDispatcher', 'Console');
  21. App::uses('ConsoleOutput', 'Console');
  22. App::uses('ConsoleInput', 'Console');
  23. App::uses('Shell', 'Console');
  24. App::uses('ViewTask', 'Console/Command/Task');
  25. App::uses('ControllerTask', 'Console/Command/Task');
  26. App::uses('TemplateTask', 'Console/Command/Task');
  27. App::uses('ProjectTask', 'Console/Command/Task');
  28. App::uses('DbConfigTask', 'Console/Command/Task');
  29. App::uses('Model', 'Model');
  30. App::uses('Controller', 'Controller');
  31. App::uses('AppController', 'Controller');
  32. /**
  33. * Test View Task Comment Model
  34. *
  35. * @package Cake.Test.Case.Console.Command.Task
  36. */
  37. class ViewTaskComment extends Model {
  38. /**
  39. * Table name
  40. *
  41. * @var string
  42. */
  43. public $useTable = 'comments';
  44. /**
  45. * Belongs To Associations
  46. *
  47. * @var array
  48. */
  49. public $belongsTo = array(
  50. 'Article' => array(
  51. 'className' => 'TestTest.ViewTaskArticle',
  52. 'foreignKey' => 'article_id'
  53. )
  54. );
  55. }
  56. /**
  57. * Test View Task Article Model
  58. *
  59. * @package Cake.Test.Case.Console.Command.Task
  60. */
  61. class ViewTaskArticle extends Model {
  62. /**
  63. * Table name
  64. *
  65. * @var string
  66. */
  67. public $useTable = 'articles';
  68. }
  69. /**
  70. * Test View Task Comments Controller
  71. *
  72. * @package Cake.Test.Case.Console.Command.Task
  73. */
  74. class ViewTaskCommentsController extends Controller {
  75. /**
  76. * Testing public controller action
  77. *
  78. * @return void
  79. */
  80. public function index() {
  81. }
  82. /**
  83. * Testing public controller action
  84. *
  85. * @return void
  86. */
  87. public function add() {
  88. }
  89. }
  90. /**
  91. * Test View Task Articles Controller
  92. *
  93. * @package Cake.Test.Case.Console.Command.Task
  94. */
  95. class ViewTaskArticlesController extends Controller {
  96. /**
  97. * Test public controller action
  98. *
  99. * @return void
  100. */
  101. public function index() {
  102. }
  103. /**
  104. * Test public controller action
  105. *
  106. * @return void
  107. */
  108. public function add() {
  109. }
  110. /**
  111. * Test admin prefixed controller action
  112. *
  113. * @return void
  114. */
  115. public function admin_index() {
  116. }
  117. /**
  118. * Test admin prefixed controller action
  119. *
  120. * @return void
  121. */
  122. public function admin_add() {
  123. }
  124. /**
  125. * Test admin prefixed controller action
  126. *
  127. * @return void
  128. */
  129. public function admin_view() {
  130. }
  131. /**
  132. * Test admin prefixed controller action
  133. *
  134. * @return void
  135. */
  136. public function admin_edit() {
  137. }
  138. /**
  139. * Test admin prefixed controller action
  140. *
  141. * @return void
  142. */
  143. public function admin_delete() {
  144. }
  145. }
  146. /**
  147. * ViewTaskTest class
  148. *
  149. * @package Cake.Test.Case.Console.Command.Task
  150. */
  151. class ViewTaskTest extends CakeTestCase {
  152. /**
  153. * Fixtures
  154. *
  155. * @var array
  156. */
  157. public $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
  158. /**
  159. * setUp method
  160. *
  161. * Ensure that the default theme is used
  162. *
  163. * @return void
  164. */
  165. public function setUp() {
  166. parent::setUp();
  167. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  168. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  169. $this->Task = $this->getMock('ViewTask',
  170. array('in', 'err', 'createFile', '_stop'),
  171. array($out, $out, $in)
  172. );
  173. $this->Task->Template = new TemplateTask($out, $out, $in);
  174. $this->Task->Controller = $this->getMock('ControllerTask', array(), array($out, $out, $in));
  175. $this->Task->Project = $this->getMock('ProjectTask', array(), array($out, $out, $in));
  176. $this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
  177. $this->Task->path = TMP;
  178. $this->Task->Template->params['theme'] = 'default';
  179. $this->Task->Template->templatePaths = array('default' => CAKE . 'Console' . DS . 'Templates' . DS . 'default' . DS);
  180. }
  181. /**
  182. * tearDown method
  183. *
  184. * @return void
  185. */
  186. public function tearDown() {
  187. parent::tearDown();
  188. unset($this->Task, $this->Dispatch);
  189. }
  190. /**
  191. * Test getContent and parsing of Templates.
  192. *
  193. * @return void
  194. */
  195. public function testGetContent() {
  196. $vars = array(
  197. 'modelClass' => 'TestViewModel',
  198. 'schema' => array(),
  199. 'primaryKey' => 'id',
  200. 'displayField' => 'name',
  201. 'singularVar' => 'testViewModel',
  202. 'pluralVar' => 'testViewModels',
  203. 'singularHumanName' => 'Test View Model',
  204. 'pluralHumanName' => 'Test View Models',
  205. 'fields' => array('id', 'name', 'body'),
  206. 'associations' => array()
  207. );
  208. $result = $this->Task->getContent('view', $vars);
  209. $this->assertRegExp('/Delete Test View Model/', $result);
  210. $this->assertRegExp('/Edit Test View Model/', $result);
  211. $this->assertRegExp('/List Test View Models/', $result);
  212. $this->assertRegExp('/New Test View Model/', $result);
  213. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  214. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  215. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  216. }
  217. /**
  218. * test getContent() using an admin_prefixed action.
  219. *
  220. * @return void
  221. */
  222. public function testGetContentWithAdminAction() {
  223. $_back = Configure::read('Routing');
  224. Configure::write('Routing.prefixes', array('admin'));
  225. $vars = array(
  226. 'modelClass' => 'TestViewModel',
  227. 'schema' => array(),
  228. 'primaryKey' => 'id',
  229. 'displayField' => 'name',
  230. 'singularVar' => 'testViewModel',
  231. 'pluralVar' => 'testViewModels',
  232. 'singularHumanName' => 'Test View Model',
  233. 'pluralHumanName' => 'Test View Models',
  234. 'fields' => array('id', 'name', 'body'),
  235. 'associations' => array()
  236. );
  237. $result = $this->Task->getContent('admin_view', $vars);
  238. $this->assertRegExp('/Delete Test View Model/', $result);
  239. $this->assertRegExp('/Edit Test View Model/', $result);
  240. $this->assertRegExp('/List Test View Models/', $result);
  241. $this->assertRegExp('/New Test View Model/', $result);
  242. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  243. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  244. $this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  245. $result = $this->Task->getContent('admin_add', $vars);
  246. $this->assertRegExp("/input\('name'\)/", $result);
  247. $this->assertRegExp("/input\('body'\)/", $result);
  248. $this->assertRegExp('/List Test View Models/', $result);
  249. Configure::write('Routing', $_back);
  250. }
  251. /**
  252. * test Bake method
  253. *
  254. * @return void
  255. */
  256. public function testBakeView() {
  257. $this->Task->controllerName = 'ViewTaskComments';
  258. $this->Task->expects($this->at(0))->method('createFile')
  259. ->with(
  260. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  261. $this->stringContains('View Task Articles')
  262. );
  263. $this->Task->bake('view', true);
  264. }
  265. /**
  266. * test baking an edit file
  267. *
  268. * @return void
  269. */
  270. public function testBakeEdit() {
  271. $this->Task->controllerName = 'ViewTaskComments';
  272. $this->Task->expects($this->at(0))->method('createFile')
  273. ->with(
  274. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  275. new PHPUnit_Framework_Constraint_IsAnything()
  276. );
  277. $this->Task->bake('edit', true);
  278. }
  279. /**
  280. * test baking an index
  281. *
  282. * @return void
  283. */
  284. public function testBakeIndex() {
  285. $this->Task->controllerName = 'ViewTaskComments';
  286. $this->Task->expects($this->at(0))->method('createFile')
  287. ->with(
  288. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  289. $this->stringContains("\$viewTaskComment['Article']['title']")
  290. );
  291. $this->Task->bake('index', true);
  292. }
  293. /**
  294. * test that baking a view with no template doesn't make a file.
  295. *
  296. * @return void
  297. */
  298. public function testBakeWithNoTemplate() {
  299. $this->Task->controllerName = 'ViewTaskComments';
  300. $this->Task->expects($this->never())->method('createFile');
  301. $this->Task->bake('delete', true);
  302. }
  303. /**
  304. * test bake() with a -plugin param
  305. *
  306. * @return void
  307. */
  308. public function testBakeWithPlugin() {
  309. $this->Task->controllerName = 'ViewTaskComments';
  310. $this->Task->plugin = 'TestTest';
  311. $this->Task->name = 'View';
  312. //fake plugin path
  313. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
  314. $path = APP . 'Plugin' . DS . 'TestTest' . DS . 'View' . DS . 'ViewTaskComments' . DS . 'view.ctp';
  315. $result = $this->Task->getContent('index');
  316. $this->assertNotContains('List Test Test.view Task Articles', $result);
  317. $this->Task->expects($this->once())
  318. ->method('createFile')
  319. ->with($path, $this->anything());
  320. $this->Task->bake('view', true);
  321. CakePlugin::unload();
  322. }
  323. /**
  324. * test bake actions baking multiple actions.
  325. *
  326. * @return void
  327. */
  328. public function testBakeActions() {
  329. $this->Task->controllerName = 'ViewTaskComments';
  330. $this->Task->expects($this->at(0))->method('createFile')
  331. ->with(
  332. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  333. $this->stringContains('View Task Comments')
  334. );
  335. $this->Task->expects($this->at(1))->method('createFile')
  336. ->with(
  337. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  338. $this->stringContains('Edit View Task Comment')
  339. );
  340. $this->Task->expects($this->at(2))->method('createFile')
  341. ->with(
  342. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  343. $this->stringContains('ViewTaskComment')
  344. );
  345. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  346. }
  347. /**
  348. * test baking a customAction (non crud)
  349. *
  350. * @return void
  351. */
  352. public function testCustomAction() {
  353. $this->Task->controllerName = 'ViewTaskComments';
  354. $this->Task->expects($this->any())->method('in')
  355. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  356. $this->Task->expects($this->once())->method('createFile')
  357. ->with(
  358. TMP . 'ViewTaskComments' . DS . 'my_action.ctp',
  359. $this->anything()
  360. );
  361. $this->Task->customAction();
  362. }
  363. /**
  364. * Test all()
  365. *
  366. * @return void
  367. */
  368. public function testExecuteIntoAll() {
  369. $this->Task->args[0] = 'all';
  370. $this->Task->Controller->expects($this->once())->method('listAll')
  371. ->will($this->returnValue(array('view_task_comments')));
  372. $this->Task->expects($this->at(0))->method('createFile')
  373. ->with(
  374. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  375. $this->anything()
  376. );
  377. $this->Task->expects($this->at(1))->method('createFile')
  378. ->with(
  379. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  380. $this->anything()
  381. );
  382. $this->Task->expects($this->exactly(2))->method('createFile');
  383. $this->Task->execute();
  384. }
  385. /**
  386. * Test all() with action parameter
  387. *
  388. * @return void
  389. */
  390. public function testExecuteIntoAllWithActionName() {
  391. $this->Task->args = array('all', 'index');
  392. $this->Task->Controller->expects($this->once())->method('listAll')
  393. ->will($this->returnValue(array('view_task_comments')));
  394. $this->Task->expects($this->once())->method('createFile')
  395. ->with(
  396. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  397. $this->anything()
  398. );
  399. $this->Task->execute();
  400. }
  401. /**
  402. * test `cake bake view $controller view`
  403. *
  404. * @return void
  405. */
  406. public function testExecuteWithActionParam() {
  407. $this->Task->args[0] = 'ViewTaskComments';
  408. $this->Task->args[1] = 'view';
  409. $this->Task->expects($this->once())->method('createFile')
  410. ->with(
  411. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  412. $this->anything()
  413. );
  414. $this->Task->execute();
  415. }
  416. /**
  417. * test `cake bake view $controller`
  418. * Ensure that views are only baked for actions that exist in the controller.
  419. *
  420. * @return void
  421. */
  422. public function testExecuteWithController() {
  423. $this->Task->args[0] = 'ViewTaskComments';
  424. $this->Task->expects($this->at(0))->method('createFile')
  425. ->with(
  426. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  427. $this->anything()
  428. );
  429. $this->Task->expects($this->at(1))->method('createFile')
  430. ->with(
  431. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  432. $this->anything()
  433. );
  434. $this->Task->expects($this->exactly(2))->method('createFile');
  435. $this->Task->execute();
  436. }
  437. /**
  438. * static dataprovider for test cases
  439. *
  440. * @return void
  441. */
  442. public static function nameVariations() {
  443. return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
  444. }
  445. /**
  446. * test that both plural and singular forms can be used for baking views.
  447. *
  448. * @dataProvider nameVariations
  449. * @return void
  450. */
  451. public function testExecuteWithControllerVariations($name) {
  452. $this->Task->args = array($name);
  453. $this->Task->expects($this->at(0))->method('createFile')
  454. ->with(
  455. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  456. $this->anything()
  457. );
  458. $this->Task->expects($this->at(1))->method('createFile')
  459. ->with(
  460. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  461. $this->anything()
  462. );
  463. $this->Task->execute();
  464. }
  465. /**
  466. * test `cake bake view $controller --admin`
  467. * Which only bakes admin methods, not non-admin methods.
  468. *
  469. * @return void
  470. */
  471. public function testExecuteWithControllerAndAdminFlag() {
  472. $_back = Configure::read('Routing');
  473. Configure::write('Routing.prefixes', array('admin'));
  474. $this->Task->args[0] = 'ViewTaskArticles';
  475. $this->Task->params['admin'] = 1;
  476. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  477. $this->Task->expects($this->exactly(4))->method('createFile');
  478. $views = array('admin_index.ctp', 'admin_add.ctp', 'admin_view.ctp', 'admin_edit.ctp');
  479. foreach ($views as $i => $view) {
  480. $this->Task->expects($this->at($i))->method('createFile')
  481. ->with(
  482. TMP . 'ViewTaskArticles' . DS . $view,
  483. $this->anything()
  484. );
  485. }
  486. $this->Task->execute();
  487. Configure::write('Routing', $_back);
  488. }
  489. /**
  490. * test execute into interactive.
  491. *
  492. * @return void
  493. */
  494. public function testExecuteInteractive() {
  495. $this->Task->connection = 'test';
  496. $this->Task->args = array();
  497. $this->Task->params = array();
  498. $this->Task->Controller->expects($this->once())->method('getName')
  499. ->will($this->returnValue('ViewTaskComments'));
  500. $this->Task->expects($this->any())->method('in')
  501. ->will($this->onConsecutiveCalls('y', 'y', 'n'));
  502. $this->Task->expects($this->at(3))->method('createFile')
  503. ->with(
  504. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  505. $this->stringContains('ViewTaskComment')
  506. );
  507. $this->Task->expects($this->at(4))->method('createFile')
  508. ->with(
  509. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  510. $this->stringContains('ViewTaskComment')
  511. );
  512. $this->Task->expects($this->at(5))->method('createFile')
  513. ->with(
  514. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  515. $this->stringContains('Add View Task Comment')
  516. );
  517. $this->Task->expects($this->at(6))->method('createFile')
  518. ->with(
  519. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  520. $this->stringContains('Edit View Task Comment')
  521. );
  522. $this->Task->expects($this->exactly(4))->method('createFile');
  523. $this->Task->execute();
  524. }
  525. /**
  526. * test `cake bake view posts index list`
  527. *
  528. * @return void
  529. */
  530. public function testExecuteWithAlternateTemplates() {
  531. $this->Task->connection = 'test';
  532. $this->Task->args = array('ViewTaskComments', 'index', 'list');
  533. $this->Task->params = array();
  534. $this->Task->expects($this->once())->method('createFile')
  535. ->with(
  536. TMP . 'ViewTaskComments' . DS . 'list.ctp',
  537. $this->stringContains('ViewTaskComment')
  538. );
  539. $this->Task->execute();
  540. }
  541. /**
  542. * test execute into interactive() with admin methods.
  543. *
  544. * @return void
  545. */
  546. public function testExecuteInteractiveWithAdmin() {
  547. Configure::write('Routing.prefixes', array('admin'));
  548. $this->Task->connection = 'test';
  549. $this->Task->args = array();
  550. $this->Task->Controller->expects($this->once())->method('getName')
  551. ->will($this->returnValue('ViewTaskComments'));
  552. $this->Task->Project->expects($this->once())->method('getPrefix')
  553. ->will($this->returnValue('admin_'));
  554. $this->Task->expects($this->any())->method('in')
  555. ->will($this->onConsecutiveCalls('y', 'n', 'y'));
  556. $this->Task->expects($this->at(3))->method('createFile')
  557. ->with(
  558. TMP . 'ViewTaskComments' . DS . 'admin_index.ctp',
  559. $this->stringContains('ViewTaskComment')
  560. );
  561. $this->Task->expects($this->at(4))->method('createFile')
  562. ->with(
  563. TMP . 'ViewTaskComments' . DS . 'admin_view.ctp',
  564. $this->stringContains('ViewTaskComment')
  565. );
  566. $this->Task->expects($this->at(5))->method('createFile')
  567. ->with(
  568. TMP . 'ViewTaskComments' . DS . 'admin_add.ctp',
  569. $this->stringContains('Add View Task Comment')
  570. );
  571. $this->Task->expects($this->at(6))->method('createFile')
  572. ->with(
  573. TMP . 'ViewTaskComments' . DS . 'admin_edit.ctp',
  574. $this->stringContains('Edit View Task Comment')
  575. );
  576. $this->Task->expects($this->exactly(4))->method('createFile');
  577. $this->Task->execute();
  578. }
  579. /**
  580. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  581. *
  582. * @return void
  583. */
  584. public function testGetTemplate() {
  585. $result = $this->Task->getTemplate('delete');
  586. $this->assertFalse($result);
  587. $result = $this->Task->getTemplate('add');
  588. $this->assertEquals('form', $result);
  589. Configure::write('Routing.prefixes', array('admin'));
  590. $result = $this->Task->getTemplate('admin_add');
  591. $this->assertEquals('form', $result);
  592. $this->Task->Template->templatePaths = array(
  593. 'test' => CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS . 'Templates' . DS . 'test' . DS
  594. );
  595. $this->Task->Template->params['theme'] = 'test';
  596. $result = $this->Task->getTemplate('admin_edit');
  597. $this->assertEquals('admin_edit', $result);
  598. }
  599. }