ViewTaskTest.php 18 KB

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