diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index d6d30e892..c75de73f8 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2688,7 +2688,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/libs/set.php b/cake/libs/set.php index 2042c6fc1..4bc87a24c 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -437,7 +437,8 @@ class Set { $items = array($items); } elseif (!isset($items[0])) { $current = current($items); - if ((is_array($current) && count($items) <= 1) || !is_array($current)) { + $currentKey = key($items); + if (!is_array($current) || (is_array($current) && count($items) <= 1 && !is_numeric($currentKey))) { $items = array($items); } } diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index 96e5940b4..dac593e6d 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -20,6 +20,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 @@ -1875,5 +1896,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 diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index f09279ea4..8b42c7e85 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -420,43 +420,15 @@ class SetTest extends CakeTestCase { array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))), ); - $nonSequential = array( - 'User' => array( - 0 => array('id' => 1), - 2 => array('id' => 2), - 6 => array('id' => 3), - 9 => array('id' => 4), - 3 => array('id' => 5), - ), - ); - - $nonZero = array( - 'User' => array( - 2 => array('id' => 1), - 4 => array('id' => 2), - 6 => array('id' => 3), - 9 => array('id' => 4), - 3 => array('id' => 5), - ), - ); - $expected = array(array('a' => $c[2]['a'])); $r = Set::extract('/a/II[a=3]/..', $c); $this->assertEqual($r, $expected); $expected = array(1, 2, 3, 4, 5); $this->assertEqual(Set::extract('/User/id', $a), $expected); - $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected); - - $result = Set::extract('/User/id', $nonZero); - $this->assertEqual($result, $expected, 'Failed non zero array key extract'); $expected = array(1, 2, 3, 4, 5); $this->assertEqual(Set::extract('/User/id', $a), $expected); - $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected); - - $result = Set::extract('/User/id', $nonZero); - $this->assertEqual($result, $expected, 'Failed non zero array key extract'); $expected = array( array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5) @@ -552,30 +524,6 @@ class SetTest extends CakeTestCase { $r = Set::extract('/User/@*', $tricky); $this->assertEqual($r, $expected); - $nonZero = array( - 1 => array( - 'User' => array( - 'id' => 1, - 'name' => 'John', - ) - ), - 2 => array( - 'User' => array( - 'id' => 2, - 'name' => 'Bob', - ) - ), - 3 => array( - 'User' => array( - 'id' => 3, - 'name' => 'Tony', - ) - ) - ); - $expected = array(1, 2, 3); - $r = Set::extract('/User/id', $nonZero); - $this->assertEqual($r, $expected); - $common = array( array( 'Article' => array( @@ -1030,6 +978,81 @@ class SetTest extends CakeTestCase { $this->assertEqual($result, $expected); } +/** + * test that extract() still works when arrays don't contain a 0 index. + * + * @return void + */ + function testExtractWithNonZeroArrays() { + $nonZero = array( + 1 => array( + 'User' => array( + 'id' => 1, + 'name' => 'John', + ) + ), + 2 => array( + 'User' => array( + 'id' => 2, + 'name' => 'Bob', + ) + ), + 3 => array( + 'User' => array( + 'id' => 3, + 'name' => 'Tony', + ) + ) + ); + $expected = array(1, 2, 3); + $r = Set::extract('/User/id', $nonZero); + $this->assertEqual($r, $expected); + + $nonSequential = array( + 'User' => array( + 0 => array('id' => 1), + 2 => array('id' => 2), + 6 => array('id' => 3), + 9 => array('id' => 4), + 3 => array('id' => 5), + ), + ); + + $nonZero = array( + 'User' => array( + 2 => array('id' => 1), + 4 => array('id' => 2), + 6 => array('id' => 3), + 9 => array('id' => 4), + 3 => array('id' => 5), + ), + ); + + $expected = array(1, 2, 3, 4, 5); + $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected); + + $result = Set::extract('/User/id', $nonZero); + $this->assertEqual($result, $expected, 'Failed non zero array key extract'); + + $expected = array(1, 2, 3, 4, 5); + $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected); + + $result = Set::extract('/User/id', $nonZero); + $this->assertEqual($result, $expected, 'Failed non zero array key extract'); + + $startingAtOne = array( + 'Article' => array( + 1 => array( + 'id' => 1, + 'approved' => 1, + ), + ) + ); + + $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1))); + $result = Set::extract('/Article[approved=1]', $startingAtOne); + $this->assertEqual($result, $expected); + } /** * testExtractWithArrays method *