ViewTaskTest.php 17 KB

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