ViewTaskTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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-2011, 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-2011, 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' => '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. }
  208. /**
  209. * tearDown method
  210. *
  211. * @return void
  212. */
  213. public function tearDown() {
  214. parent::tearDown();
  215. unset($this->Task, $this->Dispatch);
  216. }
  217. /**
  218. * Test getContent and parsing of Templates.
  219. *
  220. * @return void
  221. */
  222. public function testGetContent() {
  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('view', $vars);
  236. $this->assertPattern('/Delete Test View Model/', $result);
  237. $this->assertPattern('/Edit Test View Model/', $result);
  238. $this->assertPattern('/List Test View Models/', $result);
  239. $this->assertPattern('/New Test View Model/', $result);
  240. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  241. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  242. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  243. }
  244. /**
  245. * test getContent() using an admin_prefixed action.
  246. *
  247. * @return void
  248. */
  249. public function testGetContentWithAdminAction() {
  250. $_back = Configure::read('Routing');
  251. Configure::write('Routing.prefixes', array('admin'));
  252. $vars = array(
  253. 'modelClass' => 'TestViewModel',
  254. 'schema' => array(),
  255. 'primaryKey' => 'id',
  256. 'displayField' => 'name',
  257. 'singularVar' => 'testViewModel',
  258. 'pluralVar' => 'testViewModels',
  259. 'singularHumanName' => 'Test View Model',
  260. 'pluralHumanName' => 'Test View Models',
  261. 'fields' => array('id', 'name', 'body'),
  262. 'associations' => array()
  263. );
  264. $result = $this->Task->getContent('admin_view', $vars);
  265. $this->assertPattern('/Delete Test View Model/', $result);
  266. $this->assertPattern('/Edit Test View Model/', $result);
  267. $this->assertPattern('/List Test View Models/', $result);
  268. $this->assertPattern('/New Test View Model/', $result);
  269. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
  270. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
  271. $this->assertPattern('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
  272. $result = $this->Task->getContent('admin_add', $vars);
  273. $this->assertPattern("/input\('name'\)/", $result);
  274. $this->assertPattern("/input\('body'\)/", $result);
  275. $this->assertPattern('/List Test View Models/', $result);
  276. Configure::write('Routing', $_back);
  277. }
  278. /**
  279. * test Bake method
  280. *
  281. * @return void
  282. */
  283. public function testBakeView() {
  284. $this->Task->controllerName = 'ViewTaskComments';
  285. $this->Task->expects($this->at(0))->method('createFile')
  286. ->with(
  287. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  288. $this->stringContains('View Task Articles')
  289. );
  290. $this->Task->bake('view', true);
  291. }
  292. /**
  293. * test baking an edit file
  294. *
  295. * @return void
  296. */
  297. public function testBakeEdit() {
  298. $this->Task->controllerName = 'ViewTaskComments';
  299. $this->Task->expects($this->at(0))->method('createFile')
  300. ->with(
  301. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  302. new PHPUnit_Framework_Constraint_IsAnything()
  303. );
  304. $this->Task->bake('edit', true);
  305. }
  306. /**
  307. * test baking an index
  308. *
  309. * @return void
  310. */
  311. public function testBakeIndex() {
  312. $this->Task->controllerName = 'ViewTaskComments';
  313. $this->Task->expects($this->at(0))->method('createFile')
  314. ->with(
  315. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  316. $this->stringContains("\$viewTaskComment['Article']['title']")
  317. );
  318. $this->Task->bake('index', true);
  319. }
  320. /**
  321. * test that baking a view with no template doesn't make a file.
  322. *
  323. * @return void
  324. */
  325. public function testBakeWithNoTemplate() {
  326. $this->Task->controllerName = 'ViewTaskComments';
  327. $this->Task->expects($this->never())->method('createFile');
  328. $this->Task->bake('delete', true);
  329. }
  330. /**
  331. * test bake() with a -plugin param
  332. *
  333. * @return void
  334. */
  335. public function testBakeWithPlugin() {
  336. $this->Task->controllerName = 'ViewTaskComments';
  337. $this->Task->plugin = 'TestTest';
  338. $this->Task->name = 'View';
  339. //fake plugin path
  340. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
  341. $path = APP . 'Plugin' . DS . 'TestTest' . DS . 'View' . DS . 'ViewTaskComments' . DS . 'view.ctp';
  342. $this->Task->expects($this->once())->method('createFile')
  343. ->with($path, new PHPUnit_Framework_Constraint_IsAnything());
  344. $this->Task->bake('view', true);
  345. CakePlugin::unload();
  346. }
  347. /**
  348. * test bake actions baking multiple actions.
  349. *
  350. * @return void
  351. */
  352. public function testBakeActions() {
  353. $this->Task->controllerName = 'ViewTaskComments';
  354. $this->Task->expects($this->at(0))->method('createFile')
  355. ->with(
  356. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  357. $this->stringContains('View Task Comments')
  358. );
  359. $this->Task->expects($this->at(1))->method('createFile')
  360. ->with(
  361. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  362. $this->stringContains('Edit View Task Comment')
  363. );
  364. $this->Task->expects($this->at(2))->method('createFile')
  365. ->with(
  366. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  367. $this->stringContains('ViewTaskComment')
  368. );
  369. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  370. }
  371. /**
  372. * test baking a customAction (non crud)
  373. *
  374. * @return void
  375. */
  376. public function testCustomAction() {
  377. $this->Task->controllerName = 'ViewTaskComments';
  378. $this->Task->params['app'] = APP;
  379. $this->Task->expects($this->any())->method('in')
  380. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  381. $this->Task->expects($this->once())->method('createFile')
  382. ->with(
  383. TMP . 'ViewTaskComments' . DS . 'my_action.ctp',
  384. new PHPUnit_Framework_Constraint_IsAnything()
  385. );
  386. $this->Task->customAction();
  387. }
  388. /**
  389. * Test all()
  390. *
  391. * @return void
  392. */
  393. public function testExecuteIntoAll() {
  394. $this->Task->args[0] = 'all';
  395. $this->Task->Controller->expects($this->once())->method('listAll')
  396. ->will($this->returnValue(array('view_task_comments')));
  397. $this->Task->expects($this->at(0))->method('createFile')
  398. ->with(
  399. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  400. new PHPUnit_Framework_Constraint_IsAnything()
  401. );
  402. $this->Task->expects($this->at(1))->method('createFile')
  403. ->with(
  404. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  405. new PHPUnit_Framework_Constraint_IsAnything()
  406. );
  407. $this->Task->expects($this->exactly(2))->method('createFile');
  408. $this->Task->execute();
  409. }
  410. /**
  411. * Test all() with action parameter
  412. *
  413. * @return void
  414. */
  415. public function testExecuteIntoAllWithActionName() {
  416. $this->Task->args = array('all', 'index');
  417. $this->Task->Controller->expects($this->once())->method('listAll')
  418. ->will($this->returnValue(array('view_task_comments')));
  419. $this->Task->expects($this->once())->method('createFile')
  420. ->with(
  421. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  422. new PHPUnit_Framework_Constraint_IsAnything()
  423. );
  424. $this->Task->execute();
  425. }
  426. /**
  427. * test `cake bake view $controller view`
  428. *
  429. * @return void
  430. */
  431. public function testExecuteWithActionParam() {
  432. $this->Task->args[0] = 'ViewTaskComments';
  433. $this->Task->args[1] = 'view';
  434. $this->Task->expects($this->once())->method('createFile')
  435. ->with(
  436. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  437. new PHPUnit_Framework_Constraint_IsAnything()
  438. );
  439. $this->Task->execute();
  440. }
  441. /**
  442. * test `cake bake view $controller`
  443. * Ensure that views are only baked for actions that exist in the controller.
  444. *
  445. * @return void
  446. */
  447. public function testExecuteWithController() {
  448. $this->Task->args[0] = 'ViewTaskComments';
  449. $this->Task->expects($this->at(0))->method('createFile')
  450. ->with(
  451. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  452. new PHPUnit_Framework_Constraint_IsAnything()
  453. );
  454. $this->Task->expects($this->at(1))->method('createFile')
  455. ->with(
  456. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  457. new PHPUnit_Framework_Constraint_IsAnything()
  458. );
  459. $this->Task->expects($this->exactly(2))->method('createFile');
  460. $this->Task->execute();
  461. }
  462. /**
  463. * static dataprovider for test cases
  464. *
  465. * @return void
  466. */
  467. public static function nameVariations() {
  468. return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
  469. }
  470. /**
  471. * test that both plural and singular forms can be used for baking views.
  472. *
  473. * @dataProvider nameVariations
  474. * @return void
  475. */
  476. public function testExecuteWithControllerVariations($name) {
  477. $this->Task->args = array($name);
  478. $this->Task->expects($this->at(0))->method('createFile')
  479. ->with(
  480. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  481. new PHPUnit_Framework_Constraint_IsAnything()
  482. );
  483. $this->Task->expects($this->at(1))->method('createFile')
  484. ->with(
  485. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  486. new PHPUnit_Framework_Constraint_IsAnything()
  487. );
  488. $this->Task->execute();
  489. }
  490. /**
  491. * test `cake bake view $controller -admin`
  492. * Which only bakes admin methods, not non-admin methods.
  493. *
  494. * @return void
  495. */
  496. public function testExecuteWithControllerAndAdminFlag() {
  497. $_back = Configure::read('Routing');
  498. Configure::write('Routing.prefixes', array('admin'));
  499. $this->Task->args[0] = 'ViewTaskArticles';
  500. $this->Task->params['admin'] = 1;
  501. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  502. $this->Task->expects($this->exactly(4))->method('createFile');
  503. $views = array('admin_index.ctp', 'admin_add.ctp', 'admin_view.ctp', 'admin_edit.ctp');
  504. foreach ($views as $i => $view) {
  505. $this->Task->expects($this->at($i))->method('createFile')
  506. ->with(
  507. TMP . 'ViewTaskArticles' . DS . $view,
  508. new PHPUnit_Framework_Constraint_IsAnything()
  509. );
  510. }
  511. $this->Task->execute();
  512. Configure::write('Routing', $_back);
  513. }
  514. /**
  515. * test execute into interactive.
  516. *
  517. * @return void
  518. */
  519. public function testExecuteInteractive() {
  520. $this->Task->connection = 'test';
  521. $this->Task->args = array();
  522. $this->Task->params = array();
  523. $this->Task->Controller->expects($this->once())->method('getName')
  524. ->will($this->returnValue('ViewTaskComments'));
  525. $this->Task->expects($this->any())->method('in')
  526. ->will($this->onConsecutiveCalls('y', 'y', 'n'));
  527. $this->Task->expects($this->at(3))->method('createFile')
  528. ->with(
  529. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  530. $this->stringContains('ViewTaskComment')
  531. );
  532. $this->Task->expects($this->at(4))->method('createFile')
  533. ->with(
  534. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  535. $this->stringContains('ViewTaskComment')
  536. );
  537. $this->Task->expects($this->at(5))->method('createFile')
  538. ->with(
  539. TMP . 'ViewTaskComments' . DS . 'add.ctp',
  540. $this->stringContains('Add View Task Comment')
  541. );
  542. $this->Task->expects($this->at(6))->method('createFile')
  543. ->with(
  544. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  545. $this->stringContains('Edit View Task Comment')
  546. );
  547. $this->Task->expects($this->exactly(4))->method('createFile');
  548. $this->Task->execute();
  549. }
  550. /**
  551. * test `cake bake view posts index list`
  552. *
  553. * @return void
  554. */
  555. public function testExecuteWithAlternateTemplates() {
  556. $this->Task->connection = 'test';
  557. $this->Task->args = array('ViewTaskComments', 'index', 'list');
  558. $this->Task->params = array();
  559. $this->Task->expects($this->once())->method('createFile')
  560. ->with(
  561. TMP . 'ViewTaskComments' . DS . 'list.ctp',
  562. $this->stringContains('ViewTaskComment')
  563. );
  564. $this->Task->execute();
  565. }
  566. /**
  567. * test execute into interactive() with admin methods.
  568. *
  569. * @return void
  570. */
  571. public function testExecuteInteractiveWithAdmin() {
  572. Configure::write('Routing.prefixes', array('admin'));
  573. $this->Task->connection = 'test';
  574. $this->Task->args = array();
  575. $this->Task->Controller->expects($this->once())->method('getName')
  576. ->will($this->returnValue('ViewTaskComments'));
  577. $this->Task->Project->expects($this->once())->method('getPrefix')
  578. ->will($this->returnValue('admin_'));
  579. $this->Task->expects($this->any())->method('in')
  580. ->will($this->onConsecutiveCalls('y', 'n', 'y'));
  581. $this->Task->expects($this->at(3))->method('createFile')
  582. ->with(
  583. TMP . 'ViewTaskComments' . DS . 'admin_index.ctp',
  584. $this->stringContains('ViewTaskComment')
  585. );
  586. $this->Task->expects($this->at(4))->method('createFile')
  587. ->with(
  588. TMP . 'ViewTaskComments' . DS . 'admin_view.ctp',
  589. $this->stringContains('ViewTaskComment')
  590. );
  591. $this->Task->expects($this->at(5))->method('createFile')
  592. ->with(
  593. TMP . 'ViewTaskComments' . DS . 'admin_add.ctp',
  594. $this->stringContains('Add View Task Comment')
  595. );
  596. $this->Task->expects($this->at(6))->method('createFile')
  597. ->with(
  598. TMP . 'ViewTaskComments' . DS . 'admin_edit.ctp',
  599. $this->stringContains('Edit View Task Comment')
  600. );
  601. $this->Task->expects($this->exactly(4))->method('createFile');
  602. $this->Task->execute();
  603. }
  604. /**
  605. * test getting templates, make sure noTemplateActions works
  606. *
  607. * @return void
  608. */
  609. public function testGetTemplate() {
  610. $result = $this->Task->getTemplate('delete');
  611. $this->assertFalse($result);
  612. $result = $this->Task->getTemplate('add');
  613. $this->assertEqual($result, 'form');
  614. Configure::write('Routing.prefixes', array('admin'));
  615. $result = $this->Task->getTemplate('admin_add');
  616. $this->assertEqual($result, 'form');
  617. }
  618. }