diff --git a/lib/Cake/Controller/Controller.php b/lib/Cake/Controller/Controller.php index 7489efa554f..747c3758d44 100644 --- a/lib/Cake/Controller/Controller.php +++ b/lib/Cake/Controller/Controller.php @@ -404,15 +404,6 @@ public function __isset($name) { */ public function __get($name) { switch ($name) { - case 'base': - case 'here': - case 'webroot': - case 'data': - return $this->request->{$name}; - case 'action': - return isset($this->request->params['action']) ? $this->request->params['action'] : ''; - case 'params': - return $this->request; case 'paginate': return $this->Components->load('Paginator')->settings; } @@ -433,15 +424,6 @@ public function __get($name) { */ public function __set($name, $value) { switch ($name) { - case 'base': - case 'here': - case 'webroot': - case 'data': - return $this->request->{$name} = $value; - case 'action': - return $this->request->params['action'] = $value; - case 'params': - return $this->request->params = $value; case 'paginate': return $this->Components->load('Paginator')->settings = $value; } @@ -955,10 +937,9 @@ public function render($view = null, $layout = null) { $models = ClassRegistry::keys(); foreach ($models as $currentModel) { $currentObject = ClassRegistry::getObject($currentModel); - if (is_a($currentObject, 'Model')) { + if ($currentObject instanceof \Cake\Model\Model) { $className = get_class($currentObject); - list($plugin) = pluginSplit(App::location($className)); - $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className'); + $this->request->params['models'][$currentObject->alias] = compact('className'); $View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors; } } @@ -1020,65 +1001,6 @@ public function flash($message, $url, $pause = 1, $layout = 'flash') { $this->render(false, $layout); } -/** - * Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call. - * - * @param array $data POST'ed data organized by model and field - * @param string|array $op A string containing an SQL comparison operator, or an array matching operators - * to fields - * @param string $bool SQL boolean operator: AND, OR, XOR, etc. - * @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be - * included in the returned conditions - * @return array An array of model conditions - * @deprecated - */ - public function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) { - if (!is_array($data) || empty($data)) { - if (!empty($this->request->data)) { - $data = $this->request->data; - } else { - return null; - } - } - $cond = array(); - - if ($op === null) { - $op = ''; - } - - $arrayOp = is_array($op); - foreach ($data as $model => $fields) { - foreach ($fields as $field => $value) { - $key = $model . '.' . $field; - $fieldOp = $op; - if ($arrayOp) { - if (array_key_exists($key, $op)) { - $fieldOp = $op[$key]; - } elseif (array_key_exists($field, $op)) { - $fieldOp = $op[$field]; - } else { - $fieldOp = false; - } - } - if ($exclusive && $fieldOp === false) { - continue; - } - $fieldOp = strtoupper(trim($fieldOp)); - if ($fieldOp === 'LIKE') { - $key = $key . ' LIKE'; - $value = '%' . $value . '%'; - } elseif ($fieldOp && $fieldOp != '=') { - $key = $key . ' ' . $fieldOp; - } - $cond[$key] = $value; - } - } - if ($bool != null && strtoupper($bool) != 'AND') { - $cond = array($bool => $cond); - } - return $cond; - } - /** * Handles automatic pagination of model records. * diff --git a/lib/Cake/Test/TestApp/Controller/AppController.php b/lib/Cake/Test/TestApp/Controller/AppController.php index 28feebaf81c..cec06c093c4 100644 --- a/lib/Cake/Test/TestApp/Controller/AppController.php +++ b/lib/Cake/Test/TestApp/Controller/AppController.php @@ -1,12 +1,5 @@ markTestIncomplete('DbAcl will not work until models do.'); Configure::write('Acl.classname', __NAMESPACE__ . '\DbAclTwoTest'); Configure::write('Acl.database', 'test'); $Collection = new ComponentCollection(); diff --git a/lib/Cake/Test/TestCase/Controller/ControllerTest.php b/lib/Cake/Test/TestCase/Controller/ControllerTest.php index fdd10fbb89a..aeab4ee732c 100644 --- a/lib/Cake/Test/TestCase/Controller/ControllerTest.php +++ b/lib/Cake/Test/TestCase/Controller/ControllerTest.php @@ -13,8 +13,10 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ namespace Cake\Test\TestCase\Controller; + use Cake\Controller\Controller; use Cake\Core\App; +use Cake\Core\Configure; use Cake\Core\Object; use Cake\Core\Plugin; use Cake\Network\Request; @@ -119,115 +121,6 @@ public function find($type = 'first', $options = array()) { } -/** - * ControllerPostsController class - * - * @package Cake.Test.Case.Controller - */ -class ControllerCommentsController extends ControllerTestAppController { - -/** - * name property - * - * @var string 'ControllerPost' - */ - public $name = 'ControllerComments'; - - protected $_mergeParent = 'ControllerTestAppController'; -} - -/** - * ControllerComment class - * - * @package Cake.Test.Case.Controller - */ -class ControllerComment extends TestModel { - -/** - * name property - * - * @var string 'ControllerComment' - */ - public $name = 'Comment'; - -/** - * useTable property - * - * @var string 'comments' - */ - public $useTable = 'comments'; - -/** - * data property - * - * @var array - */ - public $data = array('name' => 'Some Name'); - -/** - * alias property - * - * @var string 'ControllerComment' - */ - public $alias = 'ControllerComment'; -} - -/** - * ControllerAlias class - * - * @package Cake.Test.Case.Controller - */ -class ControllerAlias extends TestModel { - -/** - * name property - * - * @var string 'ControllerAlias' - */ - public $name = 'ControllerAlias'; - -/** - * alias property - * - * @var string 'ControllerSomeAlias' - */ - public $alias = 'ControllerSomeAlias'; - -/** - * useTable property - * - * @var string 'posts' - */ - public $useTable = 'posts'; -} - -/** - * NameTest class - * - * @package Cake.Test.Case.Controller - */ -class NameTest extends TestModel { - -/** - * name property - * @var string 'Name' - */ - public $name = 'Name'; - -/** - * useTable property - * @var string 'names' - */ - public $useTable = 'comments'; - -/** - * alias property - * - * @var string 'ControllerComment' - */ - public $alias = 'Name'; -} - /** * TestController class * @@ -260,7 +153,7 @@ class TestController extends ControllerTestAppController { * * @var array */ - public $uses = array('ControllerComment', 'ControllerAlias'); + public $uses = array('Comment'); protected $_mergeParent = 'ControllerTestAppController'; @@ -272,7 +165,7 @@ class TestController extends ControllerTestAppController { * @return void */ public function index($testId, $testTwoId) { - $this->data = array( + $this->request->data = array( 'testId' => $testId, 'test2Id' => $testTwoId ); @@ -286,7 +179,7 @@ public function index($testId, $testTwoId) { * @return void */ public function view($testId, $testTwoId) { - $this->data = array( + $this->request->data = array( 'testId' => $testId, 'test2Id' => $testTwoId ); @@ -312,66 +205,6 @@ public function admin_add() { } -/** - * TestComponent class - * - * @package Cake.Test.Case.Controller - */ -class TestComponent extends Object { - -/** - * beforeRedirect method - * - * @return void - */ - public function beforeRedirect() { - } - -/** - * initialize method - * - * @return void - */ - public function initialize(Controller $controller) { - } - -/** - * startup method - * - * @return void - */ - public function startup(Controller $controller) { - } - -/** - * shutdown method - * - * @return void - */ - public function shutdown(Controller $controller) { - } - -/** - * beforeRender callback - * - * @return void - */ - public function beforeRender(Controller $controller) { - if ($this->viewclass) { - $controller->viewClass = $this->viewclass; - } - } - -} - -class Test2Component extends TestComponent { - - public function beforeRender(Controller $controller) { - return false; - } - -} - /** * AnotherTestController class * @@ -445,16 +278,17 @@ public function tearDown() { * @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); - $this->assertFalse(isset($Controller->ControllerPost)); + $this->assertFalse(isset($Controller->Post)); - $result = $Controller->loadModel('ControllerPost'); + $result = $Controller->loadModel('Post'); $this->assertTrue($result); - $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); - $this->assertTrue(in_array('ControllerPost', $Controller->uses)); + $this->assertInstanceOf('TestApp\Model\Post', $Controller->Post); + $this->assertTrue(in_array('Post', $Controller->uses)); ClassRegistry::flush(); unset($Controller); @@ -466,11 +300,12 @@ public function testLoadModel() { * @return void */ public function testLoadModelInPlugins() { - App::build(array( - 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'), - 'Controller' => array(CAKE . 'Test/TestApp/Controller/'), - 'Model' => array(CAKE . 'Test/TestApp/Model/') - )); + Configure::write('App.namespace', 'TestApp'); + App::build([ + 'Plugin' => [CAKE . 'Test/TestApp/Plugin/'], + 'Controller' => [CAKE . 'Test/TestApp/Controller/'], + 'Model' => [CAKE . 'Test/TestApp/Model/'] + ]); Plugin::load('TestPlugin'); $Controller = new TestPluginController(); @@ -494,19 +329,20 @@ public function testLoadModelInPlugins() { * @return void */ public function testConstructClasses() { + Configure::write('App.namespace', 'TestApp'); $request = new Request('controller_posts/index'); $Controller = new Controller($request); - $Controller->uses = array('ControllerPost', 'ControllerComment'); + $Controller->uses = ['Post', 'Comment']; $Controller->constructClasses(); - $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); - $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment')); + $this->assertInstanceOf('TestApp\Model\Post', $Controller->Post); + $this->assertInstanceOf('TestApp\Model\Comment', $Controller->Comment); - $this->assertEquals('Comment', $Controller->ControllerComment->name); + $this->assertEquals('Comment', $Controller->Comment->name); unset($Controller); - App::build(array('Plugin' => array(CAKE . 'Test/TestApp/Plugin/'))); + App::build(['Plugin' => [CAKE . 'Test/TestApp/Plugin/']]); Plugin::load('TestPlugin'); $Controller = new Controller($request); @@ -514,24 +350,7 @@ public function testConstructClasses() { $Controller->constructClasses(); $this->assertTrue(isset($Controller->TestPluginPost)); - $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost')); - } - -/** - * testAliasName method - * - * @return void - */ - public function testAliasName() { - $request = new Request('controller_posts/index'); - $Controller = new Controller($request); - $Controller->uses = array('NameTest'); - $Controller->constructClasses(); - - $this->assertEquals('Name', $Controller->NameTest->name); - $this->assertEquals('Name', $Controller->NameTest->alias); - - unset($Controller); + $this->assertInstanceOf('TestPlugin\Model\TestPluginPost', $Controller->TestPluginPost); } /** @@ -626,10 +445,14 @@ public function testControllerSet() { * @return void */ public function testRender() { - App::build(array( - 'View' => array(CAKE . 'Test/TestApp/View/') - ), App::RESET); + Configure::write('App.namespace', 'TestApp'); + App::build([ + 'Plugin' => [CAKE . 'Test/TestApp/Plugin/'], + 'View' => [CAKE . 'Test/TestApp/View/'] + ], App::RESET); ClassRegistry::flush(); + Plugin::load('TestPlugin'); + $request = new Request('controller_posts/index'); $request->params['action'] = 'index'; @@ -648,27 +471,24 @@ public function testRender() { $Controller->view = null; $Controller = new TestController($request, new Response()); - $Controller->uses = array('ControllerAlias', 'TestPlugin.ControllerComment', 'ControllerPost'); + $Controller->uses = ['TestPlugin.TestPluginComment']; $Controller->helpers = array('Html'); $Controller->constructClasses(); - $Controller->ControllerComment->validationErrors = array('title' => 'tooShort'); - $expected = $Controller->ControllerComment->validationErrors; + $expected = ['title' => 'tooShort']; + $Controller->TestPluginComment->validationErrors = $expected; $Controller->viewPath = 'Posts'; $result = $Controller->render('index'); $View = $Controller->View; - $this->assertTrue(isset($View->validationErrors['ControllerComment'])); - $this->assertEquals($expected, $View->validationErrors['ControllerComment']); - - $expectedModels = array( - 'ControllerAlias' => array('plugin' => null, 'className' => 'ControllerAlias'), - 'ControllerComment' => array('plugin' => 'TestPlugin', 'className' => 'ControllerComment'), - 'ControllerPost' => array('plugin' => null, 'className' => 'ControllerPost') - ); + $this->assertTrue(isset($View->validationErrors['TestPluginComment'])); + $this->assertEquals($expected, $View->validationErrors['TestPluginComment']); + + $expectedModels = [ + 'TestPluginComment' => [ + 'className' => 'TestPlugin\Model\TestPluginComment' + ], + ]; $this->assertEquals($expectedModels, $Controller->request->params['models']); - - ClassRegistry::flush(); - App::build(); } /** @@ -677,21 +497,31 @@ public function testRender() { * @return void */ public function testComponentBeforeRenderChangingViewClass() { - App::build(array( - 'View' => array( + Configure::write('App.namespace', 'TestApp'); + App::build([ + 'View' => [ CAKE . 'Test/TestApp/View/' - ) - ), true); + ] + ], true); $Controller = new Controller($this->getMock('Cake\Network\Request'), new Response()); - $Controller->uses = array(); - $Controller->components = array('Test'); $Controller->constructClasses(); - $Controller->Test->viewclass = 'Theme'; - $Controller->viewPath = 'Posts'; - $Controller->theme = 'TestTheme'; + $Controller->uses = $Controller->components = []; + + $mock = $this->getMock('Cake\Controller\Component', ['beforeRender'], [$Controller->Components]); + $mock->expects($this->once()) + ->method('beforeRender') + ->will($this->returnCallback(function ($controller) { + $controller->viewClass = 'Json'; + })); + $Controller->Components->set('Test', $mock); + $Controller->Components->enable('Test'); + + $Controller->set([ + 'test' => 'value', + '_serialize' => ['test'] + ]); $result = $Controller->render('index'); - $this->assertRegExp('/default test_theme layout/', (string)$result); - App::build(); + $this->assertEquals('{"test":"value"}', $result); } /** @@ -701,9 +531,14 @@ public function testComponentBeforeRenderChangingViewClass() { */ public function testComponentCancelRender() { $Controller = new Controller($this->getMock('Cake\Network\Request'), new Response()); - $Controller->uses = array(); - $Controller->components = array('Test2'); $Controller->constructClasses(); + $mock = $this->getMock('Cake\Controller\Component', ['beforeRender'], [$Controller->Components]); + $mock->expects($this->once()) + ->method('beforeRender') + ->will($this->returnValue(false)); + $Controller->Components->set('Test', $mock); + $Controller->Components->enable('Test'); + $result = $Controller->render('index'); $this->assertInstanceOf('Cake\Network\Response', $result); } @@ -937,7 +772,7 @@ public function testMergeVars() { $this->assertEquals(0, count(array_diff($TestController->uses, $uses))); $this->assertEquals(count(array_diff_assoc(Hash::normalize($TestController->components), Hash::normalize($components))), 0); - $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost'); + $expected = array('Comment', 'ControllerPost'); $this->assertEquals($expected, $TestController->uses, '$uses was merged incorrectly, ControllerTestAppController models should be last.'); $TestController = new AnotherTestController($request); @@ -1063,10 +898,10 @@ public function testValidateErrors() { $this->assertFalse($TestController->validateErrors()); $this->assertEquals(0, $TestController->validate()); - $TestController->ControllerComment->invalidate('some_field', 'error_message'); - $TestController->ControllerComment->invalidate('some_field2', 'error_message2'); + $TestController->Comment->invalidate('some_field', 'error_message'); + $TestController->Comment->invalidate('some_field2', 'error_message2'); - $comment = new ControllerComment($request); + $comment = new \TestApp\Model\Comment($request); $comment->set('someVar', 'data'); $result = $TestController->validateErrors($comment); $expected = array('some_field' => array('error_message'), 'some_field2' => array('error_message2')); @@ -1091,68 +926,6 @@ public function testValidateErrorsOnArbitraryModels() { $this->assertEquals($expected, $result); } -/** - * testPostConditions method - * - * @return void - */ - public function testPostConditions() { - $request = new Request('controller_posts/index'); - - $Controller = new Controller($request); - - $data = array( - 'Model1' => array('field1' => '23'), - 'Model2' => array('field2' => 'string'), - 'Model3' => array('field3' => '23'), - ); - $expected = array( - 'Model1.field1' => '23', - 'Model2.field2' => 'string', - 'Model3.field3' => '23', - ); - $result = $Controller->postConditions($data); - $this->assertSame($expected, $result); - - $data = array(); - $Controller->data = array( - 'Model1' => array('field1' => '23'), - 'Model2' => array('field2' => 'string'), - 'Model3' => array('field3' => '23'), - ); - $expected = array( - 'Model1.field1' => '23', - 'Model2.field2' => 'string', - 'Model3.field3' => '23', - ); - $result = $Controller->postConditions($data); - $this->assertSame($expected, $result); - - $data = array(); - $Controller->data = array(); - $result = $Controller->postConditions($data); - $this->assertNull($result); - - $data = array(); - $Controller->data = array( - 'Model1' => array('field1' => '23'), - 'Model2' => array('field2' => 'string'), - 'Model3' => array('field3' => '23'), - ); - $ops = array( - 'Model1.field1' => '>', - 'Model2.field2' => 'LIKE', - 'Model3.field3' => '<=', - ); - $expected = array( - 'Model1.field1 >' => '23', - 'Model2.field2 LIKE' => "%string%", - 'Model3.field3 <=' => '23', - ); - $result = $Controller->postConditions($data, $ops); - $this->assertSame($expected, $result); - } - /** * testControllerHttpCodes method * @@ -1253,47 +1026,6 @@ public function testShutdownProcessIndirect() { $Controller->shutdownProcess(); } -/** - * test that BC works for attributes on the request object. - * - * @return void - */ - public function testPropertyBackwardsCompatibility() { - $request = new Request('posts/index'); - $request->addParams(array('controller' => 'posts', 'action' => 'index')); - $request->data = array('Post' => array('id' => 1)); - $request->here = '/posts/index'; - $request->webroot = '/'; - - $Controller = new TestController($request); - $this->assertEquals($request->data, $Controller->data); - $this->assertEquals($request->webroot, $Controller->webroot); - $this->assertEquals($request->here, $Controller->here); - $this->assertEquals($request->action, $Controller->action); - - $this->assertFalse(empty($Controller->data)); - $this->assertTrue(isset($Controller->data)); - $this->assertTrue(empty($Controller->something)); - $this->assertFalse(isset($Controller->something)); - - $this->assertEquals($request, $Controller->params); - $this->assertEquals($request->params['controller'], $Controller->params['controller']); - } - -/** - * test that the BC wrapper doesn't interfere with models and components. - * - * @return void - */ - public function testPropertyCompatibilityAndModelsComponents() { - $request = new Request('controller_posts/index'); - - $Controller = new TestController($request); - $Controller->constructClasses(); - $this->assertInstanceOf('Cake\Controller\Component\SecurityComponent', $Controller->Security); - $this->assertInstanceOf('ControllerComment', $Controller->ControllerComment); - } - /** * test that using Controller::paginate() falls back to PaginatorComponent * @@ -1302,27 +1034,27 @@ public function testPropertyCompatibilityAndModelsComponents() { public function testPaginateBackwardsCompatibility() { $request = new Request('controller_posts/index'); $request->params['pass'] = array(); - $response = $this->getMock('Cake\Network\Response', array('httpCodes')); + $response = $this->getMock('Cake\Network\Response', ['httpCodes']); $Controller = new Controller($request, $response); - $Controller->uses = array('ControllerPost', 'ControllerComment'); + $Controller->uses = ['Post', 'Comment']; $Controller->passedArgs[] = '1'; - $Controller->params['url'] = array(); + $Controller->request->query['url'] = []; $Controller->constructClasses(); - $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100); + $expected = ['page' => 1, 'limit' => 20, 'maxLimit' => 100]; $this->assertEquals($expected, $Controller->paginate); - $results = Hash::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); - $this->assertEquals(array(1, 2, 3), $results); + $results = Hash::extract($Controller->paginate('Post'), '{n}.Post.id'); + $this->assertEquals([1, 2, 3], $results); $Controller->passedArgs = array(); $Controller->paginate = array('limit' => '-1'); $this->assertEquals(array('limit' => '-1'), $Controller->paginate); - $Controller->paginate('ControllerPost'); - $this->assertSame($Controller->params['paging']['ControllerPost']['page'], 1); - $this->assertSame($Controller->params['paging']['ControllerPost']['pageCount'], 3); - $this->assertSame($Controller->params['paging']['ControllerPost']['prevPage'], false); - $this->assertSame($Controller->params['paging']['ControllerPost']['nextPage'], true); + $Controller->paginate('Post'); + $this->assertSame($Controller->request->params['paging']['Post']['page'], 1); + $this->assertSame($Controller->request->params['paging']['Post']['pageCount'], 3); + $this->assertSame($Controller->request->params['paging']['Post']['prevPage'], false); + $this->assertSame($Controller->request->params['paging']['Post']['nextPage'], true); } /**