ViewTaskTest.php 18 KB

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