ViewTaskTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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(
  76. 'core.article', 'core.post', 'core.comment',
  77. 'core.articles_tag', 'core.tag', 'core.test_plugin_comment');
  78. /**
  79. * setUp method
  80. *
  81. * Ensure that the default theme is used
  82. *
  83. * @return void
  84. */
  85. public function setUp() {
  86. parent::setUp();
  87. Configure::write('App.namespace', 'TestApp');
  88. $this->_setupTask(['in', 'err', 'createFile', '_stop']);
  89. TableRegistry::get('ViewTaskComments', [
  90. 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
  91. ]);
  92. }
  93. /**
  94. * Generate the mock objects used in tests.
  95. *
  96. * @return void
  97. */
  98. protected function _setupTask($methods) {
  99. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  100. $this->Task = $this->getMock('Cake\Console\Command\Task\ViewTask',
  101. $methods,
  102. [$io]
  103. );
  104. $this->Task->Template = new TemplateTask($io);
  105. $this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask', [], [$io]);
  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. }
  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(
  152. 'TestPlugin\Controller\TestsController',
  153. $this->Task->controllerClass
  154. );
  155. }
  156. /**
  157. * Test controller method with prefixes.
  158. *
  159. * @return void
  160. */
  161. public function testControllerPrefix() {
  162. $this->Task->params['prefix'] = 'Admin';
  163. $this->Task->controller('Posts');
  164. $this->assertEquals('Posts', $this->Task->controllerName);
  165. $this->assertEquals(
  166. 'TestApp\Controller\Admin\PostsController',
  167. $this->Task->controllerClass
  168. );
  169. $this->Task->params['plugin'] = 'TestPlugin';
  170. $this->Task->controller('Comments');
  171. $this->assertEquals('Comments', $this->Task->controllerName);
  172. $this->assertEquals(
  173. 'TestPlugin\Controller\Admin\CommentsController',
  174. $this->Task->controllerClass
  175. );
  176. }
  177. /**
  178. * test controller with a non-conventional controller name
  179. *
  180. * @return void
  181. */
  182. public function testControllerWithOverride() {
  183. $this->Task->controller('Comments', 'Posts');
  184. $this->assertEquals('Posts', $this->Task->controllerName);
  185. $this->assertEquals(
  186. 'TestApp\Controller\PostsController',
  187. $this->Task->controllerClass
  188. );
  189. }
  190. /**
  191. * Test the model() method.
  192. *
  193. * @return void
  194. */
  195. public function testModel() {
  196. $this->Task->model('Articles');
  197. $this->assertEquals('Articles', $this->Task->modelName);
  198. $this->Task->model('NotThere');
  199. $this->assertEquals('NotTheres', $this->Task->modelName);
  200. }
  201. /**
  202. * Test model() method with plugins.
  203. *
  204. * @return void
  205. */
  206. public function testModelPlugin() {
  207. $this->Task->params['plugin'] = 'TestPlugin';
  208. $this->Task->model('TestPluginComments');
  209. $this->assertEquals(
  210. 'TestPlugin.TestPluginComments',
  211. $this->Task->modelName
  212. );
  213. }
  214. /**
  215. * Test getPath()
  216. *
  217. * @return void
  218. */
  219. public function testGetPath() {
  220. $this->Task->controllerName = 'Posts';
  221. $result = $this->Task->getPath();
  222. $this->assertPathEquals(APP . 'Template/Posts/', $result);
  223. $this->Task->params['prefix'] = 'admin';
  224. $result = $this->Task->getPath();
  225. $this->assertPathEquals(APP . 'Template/Admin/Posts/', $result);
  226. }
  227. /**
  228. * Test getPath with plugins.
  229. *
  230. * @return void
  231. */
  232. public function testGetPathPlugin() {
  233. $this->Task->controllerName = 'Posts';
  234. $pluginPath = APP . 'Plugin/TestPlugin/';
  235. Plugin::load('TestPlugin', array('path' => $pluginPath));
  236. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  237. $result = $this->Task->getPath();
  238. $this->assertPathEquals($pluginPath . 'src/Template/Posts/', $result);
  239. $this->Task->params['prefix'] = 'admin';
  240. $result = $this->Task->getPath();
  241. $this->assertPathEquals($pluginPath . 'src/Template/Admin/Posts/', $result);
  242. Plugin::unload('TestPlugin');
  243. }
  244. /**
  245. * Test getContent and parsing of Templates.
  246. *
  247. * @return void
  248. */
  249. public function testGetContent() {
  250. $vars = array(
  251. 'modelClass' => 'TestViewModel',
  252. 'schema' => [],
  253. 'primaryKey' => ['id'],
  254. 'displayField' => 'name',
  255. 'singularVar' => 'testViewModel',
  256. 'pluralVar' => 'testViewModels',
  257. 'singularHumanName' => 'Test View Model',
  258. 'pluralHumanName' => 'Test View Models',
  259. 'fields' => ['id', 'name', 'body'],
  260. 'associations' => [],
  261. 'keyFields' => [],
  262. );
  263. $result = $this->Task->getContent('view', $vars);
  264. $this->assertContains('Delete Test View Model', $result);
  265. $this->assertContains('Edit Test View Model', $result);
  266. $this->assertContains('List Test View Models', $result);
  267. $this->assertContains('New Test View Model', $result);
  268. $this->assertContains('$testViewModel->id', $result);
  269. $this->assertContains('$testViewModel->name', $result);
  270. $this->assertContains('$testViewModel->body', $result);
  271. }
  272. /**
  273. * test getContent() using a routing prefix action.
  274. *
  275. * @return void
  276. */
  277. public function testGetContentWithRoutingPrefix() {
  278. $vars = array(
  279. 'modelClass' => 'TestViewModel',
  280. 'schema' => [],
  281. 'primaryKey' => ['id'],
  282. 'displayField' => 'name',
  283. 'singularVar' => 'testViewModel',
  284. 'pluralVar' => 'testViewModels',
  285. 'singularHumanName' => 'Test View Model',
  286. 'pluralHumanName' => 'Test View Models',
  287. 'fields' => ['id', 'name', 'body'],
  288. 'keyFields' => [],
  289. 'associations' => []
  290. );
  291. $this->Task->params['prefix'] = 'Admin';
  292. $result = $this->Task->getContent('view', $vars);
  293. $this->assertContains('Delete Test View Model', $result);
  294. $this->assertContains('Edit Test View Model', $result);
  295. $this->assertContains('List Test View Models', $result);
  296. $this->assertContains('New Test View Model', $result);
  297. $this->assertContains('$testViewModel->id', $result);
  298. $this->assertContains('$testViewModel->name', $result);
  299. $this->assertContains('$testViewModel->body', $result);
  300. $result = $this->Task->getContent('add', $vars);
  301. $this->assertContains("input('name')", $result);
  302. $this->assertContains("input('body')", $result);
  303. $this->assertContains('List Test View Models', $result);
  304. }
  305. /**
  306. * test Bake method
  307. *
  308. * @return void
  309. */
  310. public function testBakeView() {
  311. $this->Task->controllerName = 'ViewTaskComments';
  312. $this->Task->modelName = 'ViewTaskComments';
  313. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  314. $this->Task->expects($this->at(0))
  315. ->method('createFile')
  316. ->with(
  317. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  318. $this->stringContains('View Task Comments')
  319. );
  320. $this->Task->bake('view', true);
  321. }
  322. /**
  323. * test baking an edit file
  324. *
  325. * @return void
  326. */
  327. public function testBakeEdit() {
  328. $this->Task->controllerName = 'ViewTaskComments';
  329. $this->Task->modelName = 'ViewTaskComments';
  330. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  331. $this->Task->expects($this->at(0))->method('createFile')
  332. ->with(
  333. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  334. $this->anything()
  335. );
  336. $result = $this->Task->bake('edit', true);
  337. $this->assertContains("Form->input('id')", $result);
  338. $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
  339. }
  340. /**
  341. * test baking an index
  342. *
  343. * @return void
  344. */
  345. public function testBakeIndex() {
  346. $this->Task->controllerName = 'ViewTaskComments';
  347. $this->Task->modelName = 'ViewTaskComments';
  348. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  349. $this->Task->expects($this->at(0))->method('createFile')
  350. ->with(
  351. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  352. $this->stringContains("\$viewTaskComment->article->title")
  353. );
  354. $this->Task->bake('index', true);
  355. }
  356. /**
  357. * test Bake with plugins
  358. *
  359. * @return void
  360. */
  361. public function testBakeIndexPlugin() {
  362. $this->Task->controllerName = 'ViewTaskComments';
  363. $this->Task->modelName = 'TestPlugin.TestPluginComments';
  364. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  365. $table = TableRegistry::get('TestPlugin.TestPluginComments');
  366. $table->belongsTo('Articles');
  367. $this->Task->expects($this->at(0))
  368. ->method('createFile')
  369. ->with(
  370. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  371. $this->stringContains('$viewTaskComment->article->id')
  372. );
  373. $this->Task->bake('index', true);
  374. }
  375. /**
  376. * test that baking a view with no template doesn't make a file.
  377. *
  378. * @return void
  379. */
  380. public function testBakeWithNoTemplate() {
  381. $this->Task->controllerName = 'ViewTaskComments';
  382. $this->Task->modelName = 'ViewTaskComments';
  383. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  384. $this->Task->expects($this->never())->method('createFile');
  385. $this->Task->bake('delete', true);
  386. }
  387. /**
  388. * test bake actions baking multiple actions.
  389. *
  390. * @return void
  391. */
  392. public function testBakeActions() {
  393. $this->Task->controllerName = 'ViewTaskComments';
  394. $this->Task->modelName = 'ViewTaskComments';
  395. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  396. $this->Task->expects($this->at(0))
  397. ->method('createFile')
  398. ->with(
  399. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  400. $this->stringContains('View Task Comments')
  401. );
  402. $this->Task->expects($this->at(1))->method('createFile')
  403. ->with(
  404. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  405. $this->stringContains('Edit View Task Comment')
  406. );
  407. $this->Task->expects($this->at(2))->method('createFile')
  408. ->with(
  409. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  410. $this->stringContains('ViewTaskComment')
  411. );
  412. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  413. }
  414. /**
  415. * test baking a customAction (non crud)
  416. *
  417. * @return void
  418. */
  419. public function testCustomAction() {
  420. $this->Task->controllerName = 'ViewTaskComments';
  421. $this->Task->modelName = 'ViewTaskComments';
  422. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  423. $this->Task->expects($this->any())->method('in')
  424. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  425. $this->Task->expects($this->once())->method('createFile')
  426. ->with(
  427. $this->_normalizePath(APP . 'Template/ViewTaskComments/my_action.ctp'),
  428. $this->anything()
  429. );
  430. $this->Task->customAction();
  431. }
  432. /**
  433. * Test execute no args.
  434. *
  435. * @return void
  436. */
  437. public function testMainNoArgs() {
  438. $this->_setupTask(['in', 'err', 'bake', 'createFile', '_stop']);
  439. $this->Task->Model->expects($this->once())
  440. ->method('listAll')
  441. ->will($this->returnValue(['comments', 'articles']));
  442. $this->Task->expects($this->never())
  443. ->method('bake');
  444. $this->Task->main();
  445. }
  446. /**
  447. * Test all() calls execute
  448. *
  449. * @return void
  450. */
  451. public function testAllCallsMain() {
  452. $this->_setupTask(['in', 'err', 'createFile', 'main', '_stop']);
  453. $this->Task->Model->expects($this->once())
  454. ->method('listAll')
  455. ->will($this->returnValue(['comments', 'articles']));
  456. $this->Task->expects($this->exactly(2))
  457. ->method('main');
  458. $this->Task->expects($this->at(0))
  459. ->method('main')
  460. ->with('comments');
  461. $this->Task->expects($this->at(1))
  462. ->method('main')
  463. ->with('articles');
  464. $this->Task->all();
  465. }
  466. /**
  467. * test `cake bake view $controller view`
  468. *
  469. * @return void
  470. */
  471. public function testMainWithActionParam() {
  472. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  473. $this->Task->expects($this->once())
  474. ->method('bake')
  475. ->with('view', true);
  476. $this->Task->main('ViewTaskComments', 'view');
  477. }
  478. /**
  479. * test `cake bake view $controller`
  480. * Ensure that views are only baked for actions that exist in the controller.
  481. *
  482. * @return void
  483. */
  484. public function testMainWithController() {
  485. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  486. $this->Task->expects($this->exactly(4))
  487. ->method('bake');
  488. $this->Task->expects($this->at(0))
  489. ->method('bake')
  490. ->with('index', $this->anything());
  491. $this->Task->expects($this->at(1))
  492. ->method('bake')
  493. ->with('view', $this->anything());
  494. $this->Task->expects($this->at(2))
  495. ->method('bake')
  496. ->with('add', $this->anything());
  497. $this->Task->expects($this->at(3))
  498. ->method('bake')
  499. ->with('edit', $this->anything());
  500. $this->Task->main('ViewTaskComments');
  501. }
  502. /**
  503. * test that plugin.name works.
  504. *
  505. * @return void
  506. */
  507. public function testMainWithPluginName() {
  508. $this->_setupTask(['in', 'err', 'createFile']);
  509. $this->Task->connection = 'test';
  510. $filename = $this->_normalizePath(TEST_APP . 'Plugin/TestPlugin/src/Template/ViewTaskComments/index.ctp');
  511. Plugin::load('TestPlugin');
  512. $this->Task->expects($this->at(0))
  513. ->method('createFile')
  514. ->with($filename);
  515. $this->Task->main('TestPlugin.ViewTaskComments');
  516. }
  517. /**
  518. * static dataprovider for test cases
  519. *
  520. * @return void
  521. */
  522. public static function nameVariations() {
  523. return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
  524. }
  525. /**
  526. * test `cake bake view $table --controller Blog`
  527. *
  528. * @return void
  529. */
  530. public function testMainWithControllerFlag() {
  531. $this->Task->params['controller'] = 'Blog';
  532. $this->Task->expects($this->exactly(4))
  533. ->method('createFile');
  534. $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
  535. foreach ($views as $i => $view) {
  536. $this->Task->expects($this->at($i))->method('createFile')
  537. ->with(
  538. $this->_normalizePath(APP . 'Template/Blog/' . $view),
  539. $this->anything()
  540. );
  541. }
  542. $this->Task->main('Posts');
  543. }
  544. /**
  545. * test `cake bake view $controller --prefix Admin`
  546. *
  547. * @return void
  548. */
  549. public function testMainWithControllerAndAdminFlag() {
  550. $this->Task->params['prefix'] = 'Admin';
  551. $this->Task->expects($this->exactly(2))
  552. ->method('createFile');
  553. $views = array('index.ctp', 'add.ctp');
  554. foreach ($views as $i => $view) {
  555. $this->Task->expects($this->at($i))->method('createFile')
  556. ->with(
  557. $this->_normalizePath(APP . 'Template/Admin/Posts/' . $view),
  558. $this->anything()
  559. );
  560. }
  561. $this->Task->main('Posts');
  562. }
  563. /**
  564. * test `cake bake view posts index list`
  565. *
  566. * @return void
  567. */
  568. public function testMainWithAlternateTemplates() {
  569. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  570. $this->Task->connection = 'test';
  571. $this->Task->params = [];
  572. $this->Task->expects($this->once())
  573. ->method('bake')
  574. ->with('list', true);
  575. $this->Task->main('ViewTaskComments', 'index', 'list');
  576. }
  577. /**
  578. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  579. *
  580. * @return void
  581. */
  582. public function testGetTemplate() {
  583. $result = $this->Task->getTemplate('delete');
  584. $this->assertFalse($result);
  585. $result = $this->Task->getTemplate('add');
  586. $this->assertEquals('form', $result);
  587. $result = $this->Task->getTemplate('edit');
  588. $this->assertEquals('form', $result);
  589. $result = $this->Task->getTemplate('view');
  590. $this->assertEquals('view', $result);
  591. $result = $this->Task->getTemplate('index');
  592. $this->assertEquals('index', $result);
  593. }
  594. /**
  595. * Test getting prefixed views.
  596. *
  597. * @return void
  598. */
  599. public function testGetTemplatePrefixed() {
  600. $this->Task->params['prefix'] = 'Admin';
  601. $result = $this->Task->getTemplate('add');
  602. $this->assertEquals('form', $result);
  603. $this->Task->Template->templatePaths = array(
  604. 'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
  605. );
  606. $this->Task->Template->params['theme'] = 'test';
  607. $result = $this->Task->getTemplate('edit');
  608. $this->assertEquals('admin_edit', $result);
  609. $result = $this->Task->getTemplate('add');
  610. $this->assertEquals('admin_form', $result);
  611. $result = $this->Task->getTemplate('view');
  612. $this->assertEquals('view', $result);
  613. }
  614. }