ViewTaskTest.php 17 KB

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