Skip to content

Commit

Permalink
Fix ViewTask not correctly handling plugin models.
Browse files Browse the repository at this point in the history
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 96a1f36
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
26 changes: 21 additions & 5 deletions src/Console/Command/Task/ViewTask.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
70 changes: 58 additions & 12 deletions tests/TestCase/Console/Command/Task/ViewTaskTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
*
Expand Down Expand Up @@ -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))
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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');
Expand All @@ -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))
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 96a1f36

Please sign in to comment.