Skip to content
Permalink
Browse files

Fix ViewTask not correctly handling plugin models.

When baking plugin views we need to use the correct aliases or things
don't work very well.

Refs #4007
  • Loading branch information...
markstory committed Jul 18, 2014
1 parent 9829646 commit 96a1f36516257ffea22af2948fc32cddeac70c57
Showing with 79 additions and 17 deletions.
  1. +21 −5 src/Console/Command/Task/ViewTask.php
  2. +58 −12 tests/TestCase/Console/Command/Task/ViewTaskTest.php
@@ -56,11 +56,11 @@ class ViewTask extends BakeTask {
public $controllerClass = null;
/**
* Name of the table views are being baked against.
* Name with plugin of the model being used
*
* @var string
*/
public $tableName = null;
public $modelName = null;
/**
* The template file to use
@@ -119,6 +119,7 @@ public function main($name = null, $template = null, $action = null) {
$controller = $this->params['controller'];
}
$this->controller($name, $controller);
$this->model($name);
if (isset($template)) {
$this->template = $template;
@@ -141,6 +142,21 @@ public function main($name = null, $template = null, $action = null) {
}
}
/**
* Set the model class for the table.
*
* @param string $table The table/model that is being baked.
* @return void
*/
public function model($table) {
$tableName = $this->_controllerName($table);
$plugin = null;
if (!empty($this->params['plugin'])) {
$plugin = $this->params['plugin'] . '.';
}
$this->modelName = $plugin . $tableName;
}
/**
* Set the controller related properties.
*
@@ -149,9 +165,9 @@ public function main($name = null, $template = null, $action = null) {
* @return void
*/
public function controller($table, $controller = null) {
$this->tableName = $this->_controllerName($table);
$tableName = $this->_controllerName($table);
if (empty($controller)) {
$controller = $this->tableName;
$controller = $tableName;
}
$this->controllerName = $controller;
@@ -237,7 +253,7 @@ public function all() {
* @return array Returns an variables to be made available to a view template
*/
protected function _loadController() {
$modelObj = TableRegistry::get($this->tableName);
$modelObj = TableRegistry::get($this->modelName);
$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
@@ -87,7 +87,9 @@ class ViewTaskTest extends TestCase {
*
* @var array
*/
public $fixtures = array('core.article', 'core.post', 'core.comment', 'core.articles_tag', 'core.tag');
public $fixtures = array(
'core.article', 'core.post', 'core.comment',
'core.articles_tag', 'core.tag', 'core.test_plugin_comment');
/**
* setUp method
@@ -160,7 +162,6 @@ public function testController() {
public function testControllerVariations($name) {
$this->Task->controller($name);
$this->assertEquals('ViewTaskComments', $this->Task->controllerName);
$this->assertEquals('ViewTaskComments', $this->Task->tableName);
}
/**
@@ -172,7 +173,6 @@ public function testControllerPlugin() {
$this->Task->params['plugin'] = 'TestPlugin';
$this->Task->controller('Tests');
$this->assertEquals('Tests', $this->Task->controllerName);
$this->assertEquals('Tests', $this->Task->tableName);
$this->assertEquals(
'TestPlugin\Controller\TestsController',
$this->Task->controllerClass
@@ -188,7 +188,6 @@ public function testControllerPrefix() {
$this->Task->params['prefix'] = 'Admin';
$this->Task->controller('Posts');
$this->assertEquals('Posts', $this->Task->controllerName);
$this->assertEquals('Posts', $this->Task->tableName);
$this->assertEquals(
'TestApp\Controller\Admin\PostsController',
$this->Task->controllerClass
@@ -197,7 +196,6 @@ public function testControllerPrefix() {
$this->Task->params['plugin'] = 'TestPlugin';
$this->Task->controller('Comments');
$this->assertEquals('Comments', $this->Task->controllerName);
$this->assertEquals('Comments', $this->Task->tableName);
$this->assertEquals(
'TestPlugin\Controller\Admin\CommentsController',
$this->Task->controllerClass
@@ -212,13 +210,39 @@ public function testControllerPrefix() {
public function testControllerWithOverride() {
$this->Task->controller('Comments', 'Posts');
$this->assertEquals('Posts', $this->Task->controllerName);
$this->assertEquals('Comments', $this->Task->tableName);
$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('NotTheres', $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()
*
@@ -332,7 +356,7 @@ public function testGetContentWithRoutingPrefix() {
*/
public function testBakeView() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->at(0))
@@ -352,7 +376,7 @@ public function testBakeView() {
*/
public function testBakeEdit() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->at(0))->method('createFile')
@@ -373,7 +397,7 @@ public function testBakeEdit() {
*/
public function testBakeIndex() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->at(0))->method('createFile')
@@ -384,14 +408,36 @@ public function testBakeIndex() {
$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);
}
/**
* test that baking a view with no template doesn't make a file.
*
* @return void
*/
public function testBakeWithNoTemplate() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->never())->method('createFile');
@@ -405,7 +451,7 @@ public function testBakeWithNoTemplate() {
*/
public function testBakeActions() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->at(0))
@@ -435,7 +481,7 @@ public function testBakeActions() {
*/
public function testCustomAction() {
$this->Task->controllerName = 'ViewTaskComments';
$this->Task->tableName = 'ViewTaskComments';
$this->Task->modelName = 'ViewTaskComments';
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
$this->Task->expects($this->any())->method('in')

0 comments on commit 96a1f36

Please sign in to comment.
You can’t perform that action at this time.