Skip to content

Commit

Permalink
Fixing bug in Model::escapeField() where it would return the wrong st…
Browse files Browse the repository at this point in the history
…ring id the datasource's name method returs the unmodified string.

Tests added. Closes #473
  • Loading branch information
lorenzo committed Mar 17, 2010
1 parent bc990f4 commit 28cb57a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cake/libs/model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,7 @@ function escapeField($field = null, $alias = null) {
$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);
Expand Down
52 changes: 52 additions & 0 deletions cake/tests/cases/libs/model/model_integration.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1831,5 +1852,36 @@ function testCreation() {
$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);
}
}
?>

0 comments on commit 28cb57a

Please sign in to comment.