From 9bafc5a3bbfa4e588397082620c2f9dab3cde548 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Sun, 3 Jun 2012 15:52:21 +0700 Subject: [PATCH 1/4] CakeEmail: create request object before rendering Closes #2931 --- lib/Cake/Network/Email/CakeEmail.php | 6 ++++ .../Test/Case/Network/Email/CakeEmailTest.php | 32 +++++++++++++++++++ .../Test/test_app/View/Emails/html/image.ctp | 23 +++++++++++++ lib/Cake/View/Helper.php | 3 ++ 4 files changed, 64 insertions(+) create mode 100644 lib/Cake/Test/test_app/View/Emails/html/image.ctp diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index be21205ad..74c9e0a0f 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -1441,6 +1441,12 @@ class CakeEmail { $View = new $viewClass(null); $View->viewVars = $this->_viewVars; $View->helpers = $this->_helpers; + if (!$request = Router::getRequest(true)) { + $request = new CakeRequest('/', false); + $request->base = ''; + $request->here = $request->webroot = '/'; + } + $View->request = $request; list($templatePlugin, $template) = pluginSplit($this->_template); list($layoutPlugin, $layout) = pluginSplit($this->_layout); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 6d429aff1..ee4e8f640 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -1028,6 +1028,38 @@ class CakeEmailTest extends CakeTestCase { $this->assertEquals(array('Time'), $result); } +/** + * testSendRenderWithImage method + * + * @return void + */ + public function testSendRenderWithImage() { + $this->CakeEmail->reset(); + $this->CakeEmail->transport('Debug'); + + $this->CakeEmail->from('cake@cakephp.org'); + $this->CakeEmail->to(array('you@cakephp.org' => 'You')); + $this->CakeEmail->subject('My title'); + $this->CakeEmail->config(array('empty')); + $this->CakeEmail->template('image'); + $this->CakeEmail->emailFormat('html'); + + $View = new View(); + $View->request = new CakeRequest('/', true); + $View->request->base = ''; + $View->request->webroot = '/'; + $View->request->here = '/'; + $View->Helpers->load('Html'); + + $expected = $View->Html->image('image.gif', array( + 'fullBase' => true, 'alt' => 'cool image', + 'width' => 100, 'height' => 100, + )); + + $result = $this->CakeEmail->send(); + $this->assertContains($expected, $result['message']); + } + /** * testSendRenderPlugin method * diff --git a/lib/Cake/Test/test_app/View/Emails/html/image.ctp b/lib/Cake/Test/test_app/View/Emails/html/image.ctp new file mode 100644 index 000000000..073d58ab6 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Emails/html/image.ctp @@ -0,0 +1,23 @@ +Html->image('image.gif', array( + 'alt' => 'cool image', + 'width' => 100, + 'height' => 100, + 'fullBase' => true, + )); diff --git a/lib/Cake/View/Helper.php b/lib/Cake/View/Helper.php index d925ee8d3..0061eac3f 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -313,6 +313,9 @@ class Helper extends Object { $path = h($this->assetTimestamp($this->webroot($path))); if (!empty($options['fullBase'])) { + if ($path[0] == '/') { + $path = substr($path, 1); + } $path = $this->url('/', true) . $path; } } From e821c27f54218a9bdbbf3e5bc4942fe0165cddcb Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Mon, 4 Jun 2012 09:17:05 +0700 Subject: [PATCH 2/4] remove unnecessary lines --- lib/Cake/Test/Case/Error/ExceptionRendererTest.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php index e1fb8bd8b..72f80949b 100644 --- a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php +++ b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php @@ -296,21 +296,8 @@ class ExceptionRendererTest extends CakeTestCase { $testApp . 'Error' . DS ), ), App::RESET); - Configure::write('Error', array( - 'handler' => 'TestAppsErrorHandler::handleError', - 'level' => E_ALL & ~E_DEPRECATED, - 'trace' => true - )); - Configure::write('Exception', array( - 'handler' => 'TestAppsErrorHandler::handleException', - 'renderer' => 'TestAppsExceptionRenderer', - 'log' => true - )); - - App::uses('TestAppsErrorController', 'Controller'); App::uses('TestAppsExceptionRenderer', 'Error'); - $exception = new SocketException('socket exception'); $renderer = new TestAppsExceptionRenderer($exception); From 8966f1b324c1967326cc9f57729eefe7883947f6 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Mon, 4 Jun 2012 20:29:28 +0700 Subject: [PATCH 3/4] create CakeRequest in View instead of in CakeEmail --- lib/Cake/Network/Email/CakeEmail.php | 6 ------ lib/Cake/Test/Case/Network/Email/CakeEmailTest.php | 13 +------------ lib/Cake/Test/Case/Utility/DebuggerTest.php | 2 +- lib/Cake/View/View.php | 5 +++++ 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 74c9e0a0f..be21205ad 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -1441,12 +1441,6 @@ class CakeEmail { $View = new $viewClass(null); $View->viewVars = $this->_viewVars; $View->helpers = $this->_helpers; - if (!$request = Router::getRequest(true)) { - $request = new CakeRequest('/', false); - $request->base = ''; - $request->here = $request->webroot = '/'; - } - $View->request = $request; list($templatePlugin, $template) = pluginSplit($this->_template); list($layoutPlugin, $layout) = pluginSplit($this->_layout); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index ee4e8f640..209009415 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -1044,18 +1044,7 @@ class CakeEmailTest extends CakeTestCase { $this->CakeEmail->template('image'); $this->CakeEmail->emailFormat('html'); - $View = new View(); - $View->request = new CakeRequest('/', true); - $View->request->base = ''; - $View->request->webroot = '/'; - $View->request->here = '/'; - $View->Helpers->load('Html'); - - $expected = $View->Html->image('image.gif', array( - 'fullBase' => true, 'alt' => 'cool image', - 'width' => 100, 'height' => 100, - )); - + $expected = 'cool image'; $result = $this->CakeEmail->send(); $this->assertContains($expected, $result['message']); } diff --git a/lib/Cake/Test/Case/Utility/DebuggerTest.php b/lib/Cake/Test/Case/Utility/DebuggerTest.php index f45f19cdc..2eaab04f6 100644 --- a/lib/Cake/Test/Case/Utility/DebuggerTest.php +++ b/lib/Cake/Test/Case/Utility/DebuggerTest.php @@ -324,7 +324,7 @@ object(View) { validationErrors => array() hasRendered => false uuids => array() - request => null + request => object(CakeRequest) {} response => object(CakeResponse) {} elementCache => 'default' int => (int) 2 diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index f2e40eac5..948d9dc55 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -314,6 +314,11 @@ class View extends Object { } $this->_eventManager = $controller->getEventManager(); } + if (empty($this->request) && !($this->request = Router::getRequest(true))) { + $this->request = new CakeRequest(null, false); + $this->request->base = ''; + $this->request->here = $this->request->webroot = '/'; + } if (is_object($controller) && isset($controller->response)) { $this->response = $controller->response; } else { From 111a23274e5c6ab315dabacf87f39dee3d6df621 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 6 Jun 2012 10:07:01 -0430 Subject: [PATCH 4/4] Fixing yet another issue related to beforeValidate and validateAssociated --- lib/Cake/Model/Model.php | 4 ++- .../Test/Case/Model/ModelValidationTest.php | 30 +++++++++++++++++++ lib/Cake/Test/Case/Model/ModelWriteTest.php | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index a2bf2a588..6a0083fe1 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2357,7 +2357,9 @@ class Model extends Object implements CakeEventListener { if ($options['deep']) { $validates = $this->{$association}->validateAssociated($values, $options); } else { - $validates = $this->{$association}->create($values) !== null && $this->{$association}->validates($options); + $this->{$association}->create(null); + $validates = $this->{$association}->set($values) && $this->{$association}->validates($options); + $data[$association] = $this->{$association}->data[$this->{$association}->alias]; } if (is_array($validates)) { if (in_array(false, $validates, true)) { diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index eb90ebbfa..edbd9efd4 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1127,4 +1127,34 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals($expected['Article'], $result['Article']); } +/** + * Tests that altering data in a beforeValidate callback will lead to saving those + * values in database, this time with belongsTo associations + * + * @return void + */ + public function testValidateFirstAssociatedWithBeforeValidate2() { + $this->loadFixtures('Article', 'User'); + $model = new CustomArticle(); + $model->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => true + ) + ) + ); + + $data = array( + 'User' => array('user' => 'foo', 'password' => 'bar'), + 'CustomArticle' => array( + 'body' => 'a test' + ) + ); + $result = $model->saveAll($data, array('validate' => 'first')); + $this->assertTrue($result); + + $this->assertEquals('foo', $model->field('title', array('body' => 'a test'))); + } + } \ No newline at end of file diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 0c8b4e054..1be1bbf77 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -5947,6 +5947,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ public function testValidateAssociated() { + $this->loadFixtures('Attachment', 'Article', 'Comment'); $TestModel = new Comment(); $TestModel->Attachment->validate = array('attachment' => 'notEmpty');