From 28cb57a92c54a223fed4aacc738db5ccc8bc71c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Wed, 17 Mar 2010 10:32:36 -0430 Subject: [PATCH] Fixing bug in Model::escapeField() where it would return the wrong string id the datasource's name method returs the unmodified string. Tests added. Closes #473 --- cake/libs/model/model.php | 2 +- .../libs/model/model_integration.test.php | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index f3c26e01a..12de7f31f 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2631,7 +2631,7 @@ class Model extends Overloadable { $field = $this->primaryKey; } $db =& ConnectionManager::getDataSource($this->useDbConfig); - if (strpos($field, $db->name($alias)) === 0) { + if (strpos($field, $db->name($alias) . '.') === 0) { return $field; } return $db->name($alias . '.' . $field); diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index 3da333592..85de2ed96 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -24,6 +24,27 @@ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ require_once dirname(__FILE__) . DS . 'model.test.php'; +App::import('Core', 'DboSource'); + +/** + * DboMock class + * A Dbo Source driver to mock a connection and a identity name() method + */ +class DboMock extends DboSource { + +/** +* Returns the $field without modifications +*/ + function name($field) { + return $field; + } +/** +* Returns true to fake a database connection +*/ + function connect() { + return true; + } +} /** * ModelIntegrationTest @@ -1831,5 +1852,36 @@ class ModelIntegrationTest extends BaseModelTest { $this->assertEqual($FeaturedModel->create($data), $expected); } +/** + * testEscapeField to prove it escapes the field well even when it has part of the alias on it + * @see ttp://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/473-escapefield-doesnt-consistently-prepend-modelname + * + * @access public + * @return void + */ + function testEscapeField() { + $TestModel =& new Test(); + $db =& $TestModel->getDataSource(); + + $result = $TestModel->escapeField('test_field'); + $expected = $db->name('Test.test_field'); + $this->assertEqual($result, $expected); + + $result = $TestModel->escapeField('TestField'); + $expected = $db->name('Test.TestField'); + $this->assertEqual($result, $expected); + + $result = $TestModel->escapeField('DomainHandle', 'Domain'); + $expected = $db->name('Domain.DomainHandle'); + $this->assertEqual($result, $expected); + + ConnectionManager::create('mock', array('driver' => 'mock')); + $TestModel->setDataSource('mock'); + $db =& $TestModel->getDataSource(); + + $result = $TestModel->escapeField('DomainHandle', 'Domain'); + $expected = $db->name('Domain.DomainHandle'); + $this->assertEqual($result, $expected); + } } ?> \ No newline at end of file