Skip to content

Commit

Permalink
Fix ViewTask generating links for self associations.
Browse files Browse the repository at this point in the history
When getting the list of associations, self associations should be
excluded.

Refs #4440
  • Loading branch information
markstory committed Sep 1, 2014
1 parent 081f271 commit ae7d610
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Shell/Task/ViewTask.php
Expand Up @@ -444,6 +444,10 @@ protected function _associations(Table $model) {
$assocName = $assoc->name();
$alias = $target->alias();

if (get_class($target) === get_class($model)) {
continue;
}

$associations[$type][$assocName] = [
'property' => $assoc->property(),
'variable' => Inflector::variable($assocName),
Expand Down
30 changes: 25 additions & 5 deletions tests/TestCase/Shell/Task/ViewTaskTest.php
Expand Up @@ -25,7 +25,6 @@

/**
* Test View Task Comment Model
*
*/
class ViewTaskCommentsTable extends Table {

Expand All @@ -40,7 +39,6 @@ public function initialize(array $config) {

/**
* Test View Task Article Model
*
*/
class ViewTaskArticlesTable extends Table {

Expand All @@ -52,7 +50,6 @@ public function intialize(array $config) {

/**
* Test View Task Comments Controller
*
*/
class ViewTaskCommentsController extends Controller {

Expand All @@ -76,7 +73,6 @@ public function add() {

}


/**
* ViewTaskTest class
*/
Expand All @@ -89,7 +85,11 @@ class ViewTaskTest extends TestCase {
*/
public $fixtures = array(
'core.article', 'core.post', 'core.comment',
'core.articles_tag', 'core.tag', 'core.test_plugin_comment');
'core.articles_tag',
'core.tag',
'core.test_plugin_comment',
'core.category_thread',
);

/**
* setUp method
Expand Down Expand Up @@ -430,6 +430,26 @@ public function testBakeIndexPlugin() {
$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.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/test_app/TestApp/Model/Table/CategoryThreadsTable.php
@@ -0,0 +1,33 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp\Model\Table;

use Cake\ORM\Table;

/**
* CategoryThreadsTable
*/
class CategoryThreadsTable extends Table {

public function initialize(array $config) {
$this->table('category_threads');
$this->belongsTo('ParentCategoryThreads', [
'className' => __CLASS__,
'foreignKey' => 'parent_id'
]);
$this->hasMany('ChildCategoryThreads', [
'className' => __CLASS__,
'foreignKey' => 'parent_id'
]);
}

}

0 comments on commit ae7d610

Please sign in to comment.