Skip to content

Commit

Permalink
Merge pull request #3679 from markstory/issue-3665
Browse files Browse the repository at this point in the history
3.0 - Fix plugin controllers not getting the correct modelClass.
  • Loading branch information
lorenzo committed Jun 9, 2014
2 parents d23ad39 + edcef5b commit 3d40307
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
27 changes: 21 additions & 6 deletions src/Controller/Controller.php
Expand Up @@ -89,6 +89,8 @@ class Controller implements EventListener {
/**
* The name of this controller. Controller names are plural, named after the model they manipulate.
*
* Set automatically using conventions in Controller::__construct().
*
* @var string
* @link http://book.cakephp.org/2.0/en/controllers.html#controller-attributes
*/
Expand Down Expand Up @@ -215,9 +217,22 @@ class Controller implements EventListener {
*/
public $methods = array();

/**
* The path to this controllers view templates.
* Example `Articles`
*
* Set automatically using conventions in Controller::__construct().
*
* @var string
*/
public $viewPath;

/**
* Constructor.
*
* Sets a number of properties based on conventions if they are empty. To override the
* conventions CakePHP uses you can define properties in your class declaration.
*
* @param \Cake\Network\Request $request Request object for this controller. Can be null for testing,
* but expect that features that use the request parameters will not work.
* @param \Cake\Network\Response $response Response object for this controller.
Expand All @@ -240,20 +255,20 @@ public function __construct($request = null, $response = null, $name = null) {
$this->viewPath = $viewPath;
}

$this->_setModelClass($this->name);
$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);

$childMethods = get_class_methods($this);
$parentMethods = get_class_methods('Cake\Controller\Controller');

$this->methods = array_diff($childMethods, $parentMethods);
$baseMethods = get_class_methods('Cake\Controller\Controller');
$this->methods = array_diff($childMethods, $baseMethods);

if ($request instanceof Request) {
$this->setRequest($request);
}
if ($response instanceof Response) {
$this->response = $response;
}

$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
$modelClass = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
$this->_setModelClass($modelClass);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Model/ModelAwareTrait.php
Expand Up @@ -29,7 +29,9 @@ trait ModelAwareTrait {
* This object's primary model class name. Should be a plural form.
* CakePHP will not inflect the name.
*
* Example: For a object named 'Comments', the modelClass would be 'Comments'
* Example: For a object named 'Comments', the modelClass would be 'Comments'.
* Plugin classes should use `Plugin.Comments` style names to correctly load
* models from the correct plugin.
*
* @var string
*/
Expand Down
28 changes: 22 additions & 6 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -219,6 +219,7 @@ public function setUp() {
parent::setUp();

App::objects('Plugin', null, false);
Configure::write('App.namespace', 'TestApp');
Router::reload();
}

Expand All @@ -238,7 +239,6 @@ public function tearDown() {
* @return void
*/
public function testTableAutoload() {
Configure::write('App.namespace', 'TestApp');
$request = new Request('controller_posts/index');
$response = $this->getMock('Cake\Network\Response');
$Controller = new Controller($request, $response);
Expand All @@ -256,7 +256,6 @@ public function testTableAutoload() {
* @return void
*/
public function testLoadModel() {
Configure::write('App.namespace', 'TestApp');
$request = new Request('controller_posts/index');
$response = $this->getMock('Cake\Network\Response');
$Controller = new Controller($request, $response);
Expand All @@ -277,7 +276,6 @@ public function testLoadModel() {
* @return void
*/
public function testLoadModelInPlugins() {
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');

$Controller = new TestPluginController();
Expand All @@ -293,13 +291,33 @@ public function testLoadModelInPlugins() {
);
}

/**
* Test that the constructor sets modelClass properly.
*
* @return void
*/
public function testConstructSetModelClass() {
Plugin::load('TestPlugin');

$request = new Request();
$response = new Response();
$controller = new \TestApp\Controller\PostsController($request, $response);
$this->assertEquals('Posts', $controller->modelClass);

$controller = new \TestApp\Controller\Admin\PostsController($request, $response);
$this->assertEquals('Posts', $controller->modelClass);

$request->params['plugin'] = 'TestPlugin';
$controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
$this->assertEquals('TestPlugin.Comments', $controller->modelClass);
}

/**
* testConstructClassesWithComponents method
*
* @return void
*/
public function testConstructClassesWithComponents() {
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');

$Controller = new TestPluginController(new Request(), new Response());
Expand All @@ -315,7 +333,6 @@ public function testConstructClassesWithComponents() {
* @return void
*/
public function testRender() {
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');

$request = new Request('controller_posts/index');
Expand All @@ -342,7 +359,6 @@ public function testRender() {
* @return void
*/
public function testBeforeRenderCallbackChangingViewClass() {
Configure::write('App.namespace', 'TestApp');
$Controller = new Controller(new Request, new Response());

$Controller->eventManager()->attach(function ($event) {
Expand Down
Expand Up @@ -22,8 +22,6 @@

class TestPluginController extends TestPluginAppController {

public $uses = array();

public function index() {
$this->autoRender = false;
}
Expand Down
Expand Up @@ -21,8 +21,6 @@

class TestsController extends TestPluginAppController {

public $uses = array();

public $helpers = array('TestPlugin.OtherHelper', 'Html');

public $components = array('TestPlugin.Plugins');
Expand Down

0 comments on commit 3d40307

Please sign in to comment.