ViewTaskTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test View Task Comment Model
  26. *
  27. */
  28. class ViewTaskCommentsTable extends Table {
  29. public function initialize(array $config) {
  30. $this->table('comments');
  31. $this->belongsTo('Articles', [
  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\Task\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.post', '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. Configure::write('App.namespace', 'TestApp');
  86. $this->_setupTask(['in', 'err', 'createFile', '_stop']);
  87. TableRegistry::get('ViewTaskComments', [
  88. 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
  89. ]);
  90. }
  91. /**
  92. * Generate the mock objects used in tests.
  93. *
  94. * @return void
  95. */
  96. protected function _setupTask($methods) {
  97. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  98. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  99. $this->Task = $this->getMock('Cake\Console\Command\Task\ViewTask',
  100. $methods,
  101. [$out, $out, $in]
  102. );
  103. $this->Task->Template = new TemplateTask($out, $out, $in);
  104. $this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask', [], [$out, $out, $in]);
  105. $this->Task->path = TMP;
  106. $this->Task->Template->params['theme'] = 'default';
  107. $this->Task->Template->templatePaths = ['default' => CAKE . 'Console/Templates/default/'];
  108. }
  109. /**
  110. * tearDown method
  111. *
  112. * @return void
  113. */
  114. public function tearDown() {
  115. parent::tearDown();
  116. TableRegistry::clear();
  117. unset($this->Task);
  118. }
  119. /**
  120. * Test the controller() method.
  121. *
  122. * @return void
  123. */
  124. public function testController() {
  125. $this->Task->controller('Comments');
  126. $this->assertEquals('Comments', $this->Task->controllerName);
  127. $this->assertEquals(
  128. 'TestApp\Controller\CommentsController',
  129. $this->Task->controllerClass
  130. );
  131. }
  132. /**
  133. * Test the controller() method.
  134. *
  135. * @dataProvider nameVariations
  136. * @return void
  137. */
  138. public function testControllerVariations($name) {
  139. $this->Task->controller($name);
  140. $this->assertEquals('ViewTaskComments', $this->Task->controllerName);
  141. $this->assertEquals('ViewTaskComments', $this->Task->tableName);
  142. }
  143. /**
  144. * Test controller method with plugins.
  145. *
  146. * @return void
  147. */
  148. public function testControllerPlugin() {
  149. $this->Task->params['plugin'] = 'TestPlugin';
  150. $this->Task->controller('Tests');
  151. $this->assertEquals('Tests', $this->Task->controllerName);
  152. $this->assertEquals('Tests', $this->Task->tableName);
  153. $this->assertEquals(
  154. 'TestPlugin\Controller\TestsController',
  155. $this->Task->controllerClass
  156. );
  157. }
  158. /**
  159. * Test controller method with prefixes.
  160. *
  161. * @return void
  162. */
  163. public function testControllerPrefix() {
  164. $this->Task->params['prefix'] = 'Admin';
  165. $this->Task->controller('Posts');
  166. $this->assertEquals('Posts', $this->Task->controllerName);
  167. $this->assertEquals('Posts', $this->Task->tableName);
  168. $this->assertEquals(
  169. 'TestApp\Controller\Admin\PostsController',
  170. $this->Task->controllerClass
  171. );
  172. $this->Task->params['plugin'] = 'TestPlugin';
  173. $this->Task->controller('Comments');
  174. $this->assertEquals('Comments', $this->Task->controllerName);
  175. $this->assertEquals('Comments', $this->Task->tableName);
  176. $this->assertEquals(
  177. 'TestPlugin\Controller\Admin\CommentsController',
  178. $this->Task->controllerClass
  179. );
  180. }
  181. /**
  182. * test controller with a non-conventional controller name
  183. *
  184. * @return void
  185. */
  186. public function testControllerWithOverride() {
  187. $this->Task->controller('Comments', 'Posts');
  188. $this->assertEquals('Posts', $this->Task->controllerName);
  189. $this->assertEquals('Comments', $this->Task->tableName);
  190. $this->assertEquals(
  191. 'TestApp\Controller\PostsController',
  192. $this->Task->controllerClass
  193. );
  194. }
  195. /**
  196. * Test getPath()
  197. *
  198. * @return void
  199. */
  200. public function testGetPath() {
  201. $this->Task->controllerName = 'Posts';
  202. $this->Task->path = '/path/Template/';
  203. $result = $this->Task->getPath();
  204. $this->assertPathEquals('/path/Template/Posts/', $result);
  205. $this->Task->params['prefix'] = 'admin';
  206. $result = $this->Task->getPath();
  207. $this->assertPathEquals('/path/Template/Admin/Posts/', $result);
  208. }
  209. /**
  210. * Test getPath with plugins.
  211. *
  212. * @return void
  213. */
  214. public function testGetPathPlugin() {
  215. $this->Task->controllerName = 'Posts';
  216. $this->Task->path = '/path/Template/';
  217. $pluginPath = APP . 'Plugin/TestPlugin/';
  218. Plugin::load('TestPlugin', array('path' => $pluginPath));
  219. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  220. $result = $this->Task->getPath();
  221. $this->assertPathEquals($pluginPath . 'Template/Posts/', $result);
  222. $this->Task->params['prefix'] = 'admin';
  223. $result = $this->Task->getPath();
  224. $this->assertPathEquals($pluginPath . 'Template/Admin/Posts/', $result);
  225. Plugin::unload('TestPlugin');
  226. }
  227. /**
  228. * Test getContent and parsing of Templates.
  229. *
  230. * @return void
  231. */
  232. public function testGetContent() {
  233. $vars = array(
  234. 'modelClass' => 'TestViewModel',
  235. 'schema' => [],
  236. 'primaryKey' => ['id'],
  237. 'displayField' => 'name',
  238. 'singularVar' => 'testViewModel',
  239. 'pluralVar' => 'testViewModels',
  240. 'singularHumanName' => 'Test View Model',
  241. 'pluralHumanName' => 'Test View Models',
  242. 'fields' => ['id', 'name', 'body'],
  243. 'associations' => [],
  244. 'keyFields' => [],
  245. );
  246. $result = $this->Task->getContent('view', $vars);
  247. $this->assertContains('Delete Test View Model', $result);
  248. $this->assertContains('Edit Test View Model', $result);
  249. $this->assertContains('List Test View Models', $result);
  250. $this->assertContains('New Test View Model', $result);
  251. $this->assertContains('$testViewModel->id', $result);
  252. $this->assertContains('$testViewModel->name', $result);
  253. $this->assertContains('$testViewModel->body', $result);
  254. }
  255. /**
  256. * test getContent() using a routing prefix action.
  257. *
  258. * @return void
  259. */
  260. public function testGetContentWithRoutingPrefix() {
  261. $vars = array(
  262. 'modelClass' => 'TestViewModel',
  263. 'schema' => [],
  264. 'primaryKey' => ['id'],
  265. 'displayField' => 'name',
  266. 'singularVar' => 'testViewModel',
  267. 'pluralVar' => 'testViewModels',
  268. 'singularHumanName' => 'Test View Model',
  269. 'pluralHumanName' => 'Test View Models',
  270. 'fields' => ['id', 'name', 'body'],
  271. 'keyFields' => [],
  272. 'associations' => []
  273. );
  274. $this->Task->params['prefix'] = 'Admin';
  275. $result = $this->Task->getContent('view', $vars);
  276. $this->assertContains('Delete Test View Model', $result);
  277. $this->assertContains('Edit Test View Model', $result);
  278. $this->assertContains('List Test View Models', $result);
  279. $this->assertContains('New Test View Model', $result);
  280. $this->assertContains('$testViewModel->id', $result);
  281. $this->assertContains('$testViewModel->name', $result);
  282. $this->assertContains('$testViewModel->body', $result);
  283. $result = $this->Task->getContent('add', $vars);
  284. $this->assertContains("input('name')", $result);
  285. $this->assertContains("input('body')", $result);
  286. $this->assertContains('List Test View Models', $result);
  287. }
  288. /**
  289. * test Bake method
  290. *
  291. * @return void
  292. */
  293. public function testBakeView() {
  294. $this->Task->controllerName = 'ViewTaskComments';
  295. $this->Task->tableName = 'ViewTaskComments';
  296. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  297. $this->Task->expects($this->at(0))
  298. ->method('createFile')
  299. ->with(
  300. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  301. $this->stringContains('View Task Comments')
  302. );
  303. $this->Task->bake('view', true);
  304. }
  305. /**
  306. * test baking an edit file
  307. *
  308. * @return void
  309. */
  310. public function testBakeEdit() {
  311. $this->Task->controllerName = 'ViewTaskComments';
  312. $this->Task->tableName = 'ViewTaskComments';
  313. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  314. $this->Task->expects($this->at(0))->method('createFile')
  315. ->with(
  316. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  317. $this->anything()
  318. );
  319. $result = $this->Task->bake('edit', true);
  320. $this->assertContains("Form->input('id')", $result);
  321. $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
  322. }
  323. /**
  324. * test baking an index
  325. *
  326. * @return void
  327. */
  328. public function testBakeIndex() {
  329. $this->Task->controllerName = 'ViewTaskComments';
  330. $this->Task->tableName = 'ViewTaskComments';
  331. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  332. $this->Task->expects($this->at(0))->method('createFile')
  333. ->with(
  334. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  335. $this->stringContains("\$viewTaskComment->article->title")
  336. );
  337. $this->Task->bake('index', true);
  338. }
  339. /**
  340. * test that baking a view with no template doesn't make a file.
  341. *
  342. * @return void
  343. */
  344. public function testBakeWithNoTemplate() {
  345. $this->Task->controllerName = 'ViewTaskComments';
  346. $this->Task->tableName = 'ViewTaskComments';
  347. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  348. $this->Task->expects($this->never())->method('createFile');
  349. $this->Task->bake('delete', true);
  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->tableName = 'ViewTaskComments';
  359. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  360. $this->Task->expects($this->at(0))
  361. ->method('createFile')
  362. ->with(
  363. TMP . 'ViewTaskComments' . DS . 'view.ctp',
  364. $this->stringContains('View Task Comments')
  365. );
  366. $this->Task->expects($this->at(1))->method('createFile')
  367. ->with(
  368. TMP . 'ViewTaskComments' . DS . 'edit.ctp',
  369. $this->stringContains('Edit View Task Comment')
  370. );
  371. $this->Task->expects($this->at(2))->method('createFile')
  372. ->with(
  373. TMP . 'ViewTaskComments' . DS . 'index.ctp',
  374. $this->stringContains('ViewTaskComment')
  375. );
  376. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  377. }
  378. /**
  379. * test baking a customAction (non crud)
  380. *
  381. * @return void
  382. */
  383. public function testCustomAction() {
  384. $this->Task->controllerName = 'ViewTaskComments';
  385. $this->Task->tableName = 'ViewTaskComments';
  386. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  387. $this->Task->expects($this->any())->method('in')
  388. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  389. $this->Task->expects($this->once())->method('createFile')
  390. ->with(
  391. TMP . 'ViewTaskComments' . DS . 'my_action.ctp',
  392. $this->anything()
  393. );
  394. $this->Task->customAction();
  395. }
  396. /**
  397. * Test execute no args.
  398. *
  399. * @return void
  400. */
  401. public function testExecuteNoArgs() {
  402. $this->_setupTask(['in', 'err', 'bake', 'createFile', '_stop']);
  403. $this->Task->Model->expects($this->once())
  404. ->method('listAll')
  405. ->will($this->returnValue(['comments', 'articles']));
  406. $this->Task->expects($this->never())
  407. ->method('bake');
  408. $this->Task->execute();
  409. }
  410. /**
  411. * Test all()
  412. *
  413. * @return void
  414. */
  415. public function testExecuteIntoAll() {
  416. $this->_setupTask(['in', 'err', 'createFile', 'all', '_stop']);
  417. $this->Task->args[0] = 'all';
  418. $this->Task->expects($this->once())
  419. ->method('all');
  420. $this->Task->execute();
  421. }
  422. /**
  423. * Test all() calls execute
  424. *
  425. * @return void
  426. */
  427. public function testAllCallsExecute() {
  428. $this->_setupTask(['in', 'err', 'createFile', 'execute', '_stop']);
  429. $this->Task->Model->expects($this->once())
  430. ->method('listAll')
  431. ->will($this->returnValue(['comments', 'articles']));
  432. $this->Task->expects($this->exactly(2))
  433. ->method('execute');
  434. $this->Task->all();
  435. $this->assertEquals('articles', $this->Task->args[0]);
  436. }
  437. /**
  438. * test `cake bake view $controller view`
  439. *
  440. * @return void
  441. */
  442. public function testExecuteWithActionParam() {
  443. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  444. $this->Task->args[0] = 'ViewTaskComments';
  445. $this->Task->args[1] = 'view';
  446. $this->Task->expects($this->once())
  447. ->method('bake')
  448. ->with('view', true);
  449. $this->Task->execute();
  450. }
  451. /**
  452. * test `cake bake view $controller`
  453. * Ensure that views are only baked for actions that exist in the controller.
  454. *
  455. * @return void
  456. */
  457. public function testExecuteWithController() {
  458. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  459. $this->Task->args[0] = 'ViewTaskComments';
  460. $this->Task->expects($this->exactly(4))
  461. ->method('bake');
  462. $this->Task->expects($this->at(0))
  463. ->method('bake')
  464. ->with('index', $this->anything());
  465. $this->Task->expects($this->at(1))
  466. ->method('bake')
  467. ->with('view', $this->anything());
  468. $this->Task->expects($this->at(2))
  469. ->method('bake')
  470. ->with('add', $this->anything());
  471. $this->Task->expects($this->at(3))
  472. ->method('bake')
  473. ->with('edit', $this->anything());
  474. $this->Task->execute();
  475. }
  476. /**
  477. * static dataprovider for test cases
  478. *
  479. * @return void
  480. */
  481. public static function nameVariations() {
  482. return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
  483. }
  484. /**
  485. * test `cake bake view $table --controller Blog`
  486. *
  487. * @return void
  488. */
  489. public function testExecuteWithControllerFlag() {
  490. $this->Task->args[0] = 'Posts';
  491. $this->Task->params['controller'] = 'Blog';
  492. $this->Task->expects($this->exactly(4))
  493. ->method('createFile');
  494. $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
  495. foreach ($views as $i => $view) {
  496. $this->Task->expects($this->at($i))->method('createFile')
  497. ->with(
  498. TMP . 'Blog' . DS . $view,
  499. $this->anything()
  500. );
  501. }
  502. $this->Task->execute();
  503. }
  504. /**
  505. * test `cake bake view $controller --prefix Admin`
  506. *
  507. * @return void
  508. */
  509. public function testExecuteWithControllerAndAdminFlag() {
  510. $this->Task->args[0] = 'Posts';
  511. $this->Task->params['prefix'] = 'Admin';
  512. $this->Task->expects($this->exactly(2))
  513. ->method('createFile');
  514. $views = array('index.ctp', 'add.ctp');
  515. foreach ($views as $i => $view) {
  516. $this->Task->expects($this->at($i))->method('createFile')
  517. ->with(
  518. TMP . 'Admin' . DS . 'Posts' . DS . $view,
  519. $this->anything()
  520. );
  521. }
  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->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  531. $this->Task->connection = 'test';
  532. $this->Task->args = ['ViewTaskComments', 'index', 'list'];
  533. $this->Task->params = [];
  534. $this->Task->expects($this->once())
  535. ->method('bake')
  536. ->with('list', true);
  537. $this->Task->execute();
  538. }
  539. /**
  540. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  541. *
  542. * @return void
  543. */
  544. public function testGetTemplate() {
  545. $result = $this->Task->getTemplate('delete');
  546. $this->assertFalse($result);
  547. $result = $this->Task->getTemplate('add');
  548. $this->assertEquals('form', $result);
  549. $result = $this->Task->getTemplate('edit');
  550. $this->assertEquals('form', $result);
  551. $result = $this->Task->getTemplate('view');
  552. $this->assertEquals('view', $result);
  553. $result = $this->Task->getTemplate('index');
  554. $this->assertEquals('index', $result);
  555. }
  556. /**
  557. * Test getting prefixed views.
  558. *
  559. * @return void
  560. */
  561. public function testGetTemplatePrefixed() {
  562. $this->Task->params['prefix'] = 'Admin';
  563. $result = $this->Task->getTemplate('add');
  564. $this->assertEquals('form', $result);
  565. $this->Task->Template->templatePaths = array(
  566. 'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
  567. );
  568. $this->Task->Template->params['theme'] = 'test';
  569. $result = $this->Task->getTemplate('edit');
  570. $this->assertEquals('admin_edit', $result);
  571. $result = $this->Task->getTemplate('add');
  572. $this->assertEquals('admin_form', $result);
  573. $result = $this->Task->getTemplate('view');
  574. $this->assertEquals('view', $result);
  575. }
  576. }