diff --git a/lib/Cake/ORM/Association/HasMany.php b/lib/Cake/ORM/Association/HasMany.php index 4302b263934..87d4e8fd54d 100644 --- a/lib/Cake/ORM/Association/HasMany.php +++ b/lib/Cake/ORM/Association/HasMany.php @@ -138,7 +138,7 @@ public function eagerLoader($results, $options = []) { */ protected function _options(array $opts) { if (isset($opts['sort'])) { - $this->order($opts['sort']); + $this->sort($opts['sort']); } } diff --git a/lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php b/lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php index e54dfb1dd47..5954e3086fa 100644 --- a/lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php +++ b/lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php @@ -21,7 +21,7 @@ use Cake\ORM\Query; /** - * Tests HasOne class + * Tests HasMany class * */ class HasOneTest extends \Cake\TestSuite\TestCase { @@ -87,8 +87,7 @@ public function testSort() { public function testEagerLoader() { $config = [ 'sourceTable' => $this->author, - 'targetTable' => $this->article, - 'conditions' => ['Article.is_active' => true] + 'targetTable' => $this->article ]; $association = new HasMany('Article', $config); $keys = [1, 2, 3, 4]; @@ -117,4 +116,49 @@ public function testEagerLoader() { ]; $this->assertEquals($row, $result); } + +/** + * Test the eager loader method with default query clauses + * + * @return void + */ + public function testEagerLoaderWithDefaults() { + $config = [ + 'sourceTable' => $this->author, + 'targetTable' => $this->article, + 'conditions' => ['Article.is_active' => true], + 'sort' => ['id' => 'ASC'] + ]; + $association = new HasMany('Article', $config); + $keys = [1, 2, 3, 4]; + $query = $this->getMock( + 'Cake\ORM\Query', + ['execute', 'where', 'andWhere', 'order'], + [null] + ); + $this->article->expects($this->once())->method('find')->with('all') + ->will($this->returnValue($query)); + $results = [ + ['id' => 1, 'title' => 'article 1', 'author_id' => 2], + ['id' => 2, 'title' => 'article 2', 'author_id' => 1] + ]; + + $query->expects($this->once())->method('execute') + ->will($this->returnValue($results)); + + $query->expects($this->once())->method('where') + ->with(['Article.is_active' => true]) + ->will($this->returnValue($query)); + + $query->expects($this->once())->method('andWhere') + ->with(['Article.author_id in' => $keys]) + ->will($this->returnValue($query)); + + $query->expects($this->once())->method('order') + ->with(['id' => 'ASC']) + ->will($this->returnValue($query)); + + $association->eagerLoader($keys); + } + }