ViewTaskTest.php 17 KB

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