| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- <?php
- /**
- * CakePHP : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP Project
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Shell\Task;
- use Cake\Controller\Controller;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- use Cake\Shell\Task\TemplateTask;
- use Cake\Shell\Task\ViewTask;
- use Cake\TestSuite\TestCase;
- /**
- * Test View Task Comment Model
- */
- class ViewTaskCommentsTable extends Table {
- public function initialize(array $config) {
- $this->table('comments');
- $this->belongsTo('Articles', [
- 'foreignKey' => 'article_id'
- ]);
- }
- }
- /**
- * Test View Task Article Model
- */
- class ViewTaskArticlesTable extends Table {
- public function intialize(array $config) {
- $this->table('articles');
- }
- }
- /**
- * Test View Task Comments Controller
- */
- class ViewTaskCommentsController extends Controller {
- public $modelClass = 'Cake\Test\TestCase\Shell\Task\ViewTaskCommentsTable';
- /**
- * Testing public controller action
- *
- * @return void
- */
- public function index() {
- }
- /**
- * Testing public controller action
- *
- * @return void
- */
- public function add() {
- }
- }
- /**
- * ViewTaskTest class
- */
- class ViewTaskTest extends TestCase {
- /**
- * Fixtures
- *
- * @var array
- */
- public $fixtures = array(
- 'core.articles', 'core.posts', 'core.comments',
- 'core.articles_tags',
- 'core.tags',
- 'core.test_plugin_comments',
- 'core.category_threads',
- );
- /**
- * setUp method
- *
- * Ensure that the default template is used
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('App.namespace', 'TestApp');
- $this->_setupTask(['in', 'err', 'error', 'createFile', '_stop']);
- TableRegistry::get('ViewTaskComments', [
- 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
- ]);
- }
- /**
- * Generate the mock objects used in tests.
- *
- * @return void
- */
- protected function _setupTask($methods) {
- $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
- $this->Task = $this->getMock('Cake\Shell\Task\ViewTask',
- $methods,
- [$io]
- );
- $this->Task->Template = new TemplateTask($io);
- $this->Task->Model = $this->getMock('Cake\Shell\Task\ModelTask', [], [$io]);
- $this->Task->Template->params['template'] = 'default';
- $this->Task->Template->templatePaths = ['default' => CAKE . 'Template/Bake/default/'];
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- TableRegistry::clear();
- unset($this->Task);
- }
- /**
- * Test the controller() method.
- *
- * @return void
- */
- public function testController() {
- $this->Task->controller('Comments');
- $this->assertEquals('Comments', $this->Task->controllerName);
- $this->assertEquals(
- 'TestApp\Controller\CommentsController',
- $this->Task->controllerClass
- );
- }
- /**
- * Test the controller() method.
- *
- * @dataProvider nameVariations
- * @return void
- */
- public function testControllerVariations($name) {
- $this->Task->controller($name);
- $this->assertEquals('ViewTaskComments', $this->Task->controllerName);
- }
- /**
- * Test controller method with plugins.
- *
- * @return void
- */
- public function testControllerPlugin() {
- $this->Task->params['plugin'] = 'TestPlugin';
- $this->Task->controller('Tests');
- $this->assertEquals('Tests', $this->Task->controllerName);
- $this->assertEquals(
- 'TestPlugin\Controller\TestsController',
- $this->Task->controllerClass
- );
- }
- /**
- * Test controller method with prefixes.
- *
- * @return void
- */
- public function testControllerPrefix() {
- $this->Task->params['prefix'] = 'Admin';
- $this->Task->controller('Posts');
- $this->assertEquals('Posts', $this->Task->controllerName);
- $this->assertEquals(
- 'TestApp\Controller\Admin\PostsController',
- $this->Task->controllerClass
- );
- $this->Task->params['plugin'] = 'TestPlugin';
- $this->Task->controller('Comments');
- $this->assertEquals('Comments', $this->Task->controllerName);
- $this->assertEquals(
- 'TestPlugin\Controller\Admin\CommentsController',
- $this->Task->controllerClass
- );
- }
- /**
- * test controller with a non-conventional controller name
- *
- * @return void
- */
- public function testControllerWithOverride() {
- $this->Task->controller('Comments', 'Posts');
- $this->assertEquals('Posts', $this->Task->controllerName);
- $this->assertEquals(
- 'TestApp\Controller\PostsController',
- $this->Task->controllerClass
- );
- }
- /**
- * Test the model() method.
- *
- * @return void
- */
- public function testModel() {
- $this->Task->model('Articles');
- $this->assertEquals('Articles', $this->Task->modelName);
- $this->Task->model('NotThere');
- $this->assertEquals('NotThere', $this->Task->modelName);
- }
- /**
- * Test model() method with plugins.
- *
- * @return void
- */
- public function testModelPlugin() {
- $this->Task->params['plugin'] = 'TestPlugin';
- $this->Task->model('TestPluginComments');
- $this->assertEquals(
- 'TestPlugin.TestPluginComments',
- $this->Task->modelName
- );
- }
- /**
- * Test getPath()
- *
- * @return void
- */
- public function testGetPath() {
- $this->Task->controllerName = 'Posts';
- $result = $this->Task->getPath();
- $this->assertPathEquals(APP . 'Template/Posts/', $result);
- $this->Task->params['prefix'] = 'admin';
- $result = $this->Task->getPath();
- $this->assertPathEquals(APP . 'Template/Admin/Posts/', $result);
- }
- /**
- * Test getPath with plugins.
- *
- * @return void
- */
- public function testGetPathPlugin() {
- $this->Task->controllerName = 'Posts';
- $pluginPath = APP . 'Plugin/TestPlugin/';
- Plugin::load('TestPlugin', array('path' => $pluginPath));
- $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
- $result = $this->Task->getPath();
- $this->assertPathEquals($pluginPath . 'src/Template/Posts/', $result);
- $this->Task->params['prefix'] = 'admin';
- $result = $this->Task->getPath();
- $this->assertPathEquals($pluginPath . 'src/Template/Admin/Posts/', $result);
- Plugin::unload('TestPlugin');
- }
- /**
- * Test getContent and parsing of Templates.
- *
- * @return void
- */
- public function testGetContent() {
- $vars = array(
- 'modelClass' => 'TestViewModel',
- 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
- 'primaryKey' => ['id'],
- 'displayField' => 'name',
- 'singularVar' => 'testViewModel',
- 'pluralVar' => 'testViewModels',
- 'singularHumanName' => 'Test View Model',
- 'pluralHumanName' => 'Test View Models',
- 'fields' => ['id', 'name', 'body'],
- 'associations' => [],
- 'keyFields' => [],
- );
- $result = $this->Task->getContent('view', $vars);
- $this->assertContains('Delete Test View Model', $result);
- $this->assertContains('Edit Test View Model', $result);
- $this->assertContains('List Test View Models', $result);
- $this->assertContains('New Test View Model', $result);
- $this->assertContains('$testViewModel->id', $result);
- $this->assertContains('$testViewModel->name', $result);
- $this->assertContains('$testViewModel->body', $result);
- }
- /**
- * Test getContent with no pk
- *
- * @return void
- */
- public function testGetContentWithNoPrimaryKey() {
- $vars = array(
- 'modelClass' => 'TestViewModel',
- 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
- 'primaryKey' => [],
- 'displayField' => 'name',
- 'singularVar' => 'testViewModel',
- 'pluralVar' => 'testViewModels',
- 'singularHumanName' => 'Test View Model',
- 'pluralHumanName' => 'Test View Models',
- 'fields' => ['id', 'name', 'body'],
- 'associations' => [],
- 'keyFields' => [],
- );
- $this->Task->expects($this->once())
- ->method('error')
- ->with($this->stringContains('Cannot generate views for models'));
- $result = $this->Task->getContent('view', $vars);
- $this->assertFalse($result);
- }
- /**
- * test getContent() using a routing prefix action.
- *
- * @return void
- */
- public function testGetContentWithRoutingPrefix() {
- $vars = array(
- 'modelClass' => 'TestViewModel',
- 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
- 'primaryKey' => ['id'],
- 'displayField' => 'name',
- 'singularVar' => 'testViewModel',
- 'pluralVar' => 'testViewModels',
- 'singularHumanName' => 'Test View Model',
- 'pluralHumanName' => 'Test View Models',
- 'fields' => ['id', 'name', 'body'],
- 'keyFields' => [],
- 'associations' => []
- );
- $this->Task->params['prefix'] = 'Admin';
- $result = $this->Task->getContent('view', $vars);
- $this->assertContains('Delete Test View Model', $result);
- $this->assertContains('Edit Test View Model', $result);
- $this->assertContains('List Test View Models', $result);
- $this->assertContains('New Test View Model', $result);
- $this->assertContains('$testViewModel->id', $result);
- $this->assertContains('$testViewModel->name', $result);
- $this->assertContains('$testViewModel->body', $result);
- $result = $this->Task->getContent('add', $vars);
- $this->assertContains("input('name')", $result);
- $this->assertContains("input('body')", $result);
- $this->assertContains('List Test View Models', $result);
- }
- /**
- * test Bake method
- *
- * @return void
- */
- public function testBakeView() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
- $this->stringContains('View Task Comments')
- );
- $this->Task->bake('view', true);
- }
- /**
- * test baking an edit file
- *
- * @return void
- */
- public function testBakeEdit() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->at(0))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
- $this->anything()
- );
- $result = $this->Task->bake('edit', true);
- $this->assertNotContains("Form->input('id')", $result);
- $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
- }
- /**
- * test baking an index
- *
- * @return void
- */
- public function testBakeIndex() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->at(0))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
- $this->stringContains("\$viewTaskComment->article->title")
- );
- $this->Task->bake('index', true);
- }
- /**
- * test Bake with plugins
- *
- * @return void
- */
- public function testBakeIndexPlugin() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'TestPlugin.TestPluginComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $table = TableRegistry::get('TestPlugin.TestPluginComments');
- $table->belongsTo('Articles');
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
- $this->stringContains('$viewTaskComment->article->id')
- );
- $this->Task->bake('index', true);
- }
- /**
- * Ensure that models associated with themselves do not have action
- * links generated.
- *
- * @return void
- */
- public function testBakeSelfAssociations() {
- $this->Task->controllerName = 'CategoryThreads';
- $this->Task->modelName = 'TestApp\Model\Table\CategoryThreadsTable';
- $this->Task->expects($this->once())
- ->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/CategoryThreads/index.ctp'),
- $this->logicalNot($this->stringContains('ParentCategoryThread'))
- );
- $this->Task->bake('index', true);
- }
- /**
- * test that baking a view with no template doesn't make a file.
- *
- * @return void
- */
- public function testBakeWithNoTemplate() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->never())->method('createFile');
- $this->Task->bake('delete', true);
- }
- /**
- * test bake actions baking multiple actions.
- *
- * @return void
- */
- public function testBakeActions() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
- $this->stringContains('View Task Comments')
- );
- $this->Task->expects($this->at(1))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
- $this->stringContains('Edit View Task Comment')
- );
- $this->Task->expects($this->at(2))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
- $this->stringContains('ViewTaskComment')
- );
- $this->Task->bakeActions(array('view', 'edit', 'index'), array());
- }
- /**
- * test baking a customAction (non crud)
- *
- * @return void
- */
- public function testCustomAction() {
- $this->Task->controllerName = 'ViewTaskComments';
- $this->Task->modelName = 'ViewTaskComments';
- $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
- $this->Task->expects($this->any())->method('in')
- ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
- $this->Task->expects($this->once())->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/ViewTaskComments/my_action.ctp'),
- $this->anything()
- );
- $this->Task->customAction();
- }
- /**
- * Test execute no args.
- *
- * @return void
- */
- public function testMainNoArgs() {
- $this->_setupTask(['in', 'err', 'bake', 'createFile', '_stop']);
- $this->Task->Model->expects($this->once())
- ->method('listAll')
- ->will($this->returnValue(['comments', 'articles']));
- $this->Task->expects($this->never())
- ->method('bake');
- $this->Task->main();
- }
- /**
- * Test all() calls execute
- *
- * @return void
- */
- public function testAllCallsMain() {
- $this->_setupTask(['in', 'err', 'createFile', 'main', '_stop']);
- $this->Task->Model->expects($this->once())
- ->method('listAll')
- ->will($this->returnValue(['comments', 'articles']));
- $this->Task->expects($this->exactly(2))
- ->method('main');
- $this->Task->expects($this->at(0))
- ->method('main')
- ->with('comments');
- $this->Task->expects($this->at(1))
- ->method('main')
- ->with('articles');
- $this->Task->all();
- }
- /**
- * test `cake bake view $controller view`
- *
- * @return void
- */
- public function testMainWithActionParam() {
- $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
- $this->Task->expects($this->once())
- ->method('bake')
- ->with('view', true);
- $this->Task->main('ViewTaskComments', 'view');
- }
- /**
- * test `cake bake view $controller`
- * Ensure that views are only baked for actions that exist in the controller.
- *
- * @return void
- */
- public function testMainWithController() {
- $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
- $this->Task->expects($this->exactly(4))
- ->method('bake');
- $this->Task->expects($this->at(0))
- ->method('bake')
- ->with('index', $this->anything());
- $this->Task->expects($this->at(1))
- ->method('bake')
- ->with('view', $this->anything());
- $this->Task->expects($this->at(2))
- ->method('bake')
- ->with('add', $this->anything());
- $this->Task->expects($this->at(3))
- ->method('bake')
- ->with('edit', $this->anything());
- $this->Task->main('ViewTaskComments');
- }
- /**
- * test that plugin.name works.
- *
- * @return void
- */
- public function testMainWithPluginName() {
- $this->_setupTask(['in', 'err', 'createFile']);
- $this->Task->connection = 'test';
- $filename = $this->_normalizePath(TEST_APP . 'Plugin/TestPlugin/src/Template/ViewTaskComments/index.ctp');
- Plugin::load('TestPlugin');
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with($filename);
- $this->Task->main('TestPlugin.ViewTaskComments');
- }
- /**
- * static dataprovider for test cases
- *
- * @return void
- */
- public static function nameVariations() {
- return [['ViewTaskComments'], ['view_task_comments']];
- }
- /**
- * test `cake bake view $table --controller Blog`
- *
- * @return void
- */
- public function testMainWithControllerFlag() {
- $this->Task->params['controller'] = 'Blog';
- $this->Task->expects($this->exactly(4))
- ->method('createFile');
- $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
- foreach ($views as $i => $view) {
- $this->Task->expects($this->at($i))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/Blog/' . $view),
- $this->anything()
- );
- }
- $this->Task->main('Posts');
- }
- /**
- * test `cake bake view $controller --prefix Admin`
- *
- * @return void
- */
- public function testMainWithControllerAndAdminFlag() {
- $this->Task->params['prefix'] = 'Admin';
- $this->Task->expects($this->exactly(2))
- ->method('createFile');
- $views = array('index.ctp', 'add.ctp');
- foreach ($views as $i => $view) {
- $this->Task->expects($this->at($i))->method('createFile')
- ->with(
- $this->_normalizePath(APP . 'Template/Admin/Posts/' . $view),
- $this->anything()
- );
- }
- $this->Task->main('Posts');
- }
- /**
- * test `cake bake view posts index list`
- *
- * @return void
- */
- public function testMainWithAlternateTemplates() {
- $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
- $this->Task->connection = 'test';
- $this->Task->params = [];
- $this->Task->expects($this->once())
- ->method('bake')
- ->with('list', true);
- $this->Task->main('ViewTaskComments', 'index', 'list');
- }
- /**
- * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
- *
- * @return void
- */
- public function testGetTemplate() {
- $result = $this->Task->getTemplate('delete');
- $this->assertFalse($result);
- $result = $this->Task->getTemplate('add');
- $this->assertEquals('form', $result);
- $result = $this->Task->getTemplate('edit');
- $this->assertEquals('form', $result);
- $result = $this->Task->getTemplate('view');
- $this->assertEquals('view', $result);
- $result = $this->Task->getTemplate('index');
- $this->assertEquals('index', $result);
- }
- /**
- * Test getting prefixed views.
- *
- * @return void
- */
- public function testGetTemplatePrefixed() {
- $this->Task->params['prefix'] = 'Admin';
- $result = $this->Task->getTemplate('add');
- $this->assertEquals('form', $result);
- $this->Task->Template->templatePaths = array(
- 'test' => CORE_TESTS . '/test_app/TestApp/Template/Bake/test/'
- );
- $this->Task->Template->params['template'] = 'test';
- $result = $this->Task->getTemplate('edit');
- $this->assertEquals('admin_edit', $result);
- $result = $this->Task->getTemplate('add');
- $this->assertEquals('admin_form', $result);
- $result = $this->Task->getTemplate('view');
- $this->assertEquals('view', $result);
- }
- }
|