diff --git a/tests/TestCase/ORM/Association/BelongsToManyTest.php b/tests/TestCase/ORM/Association/BelongsToManyTest.php index d2e09ee7c71..c28ce7ccf77 100644 --- a/tests/TestCase/ORM/Association/BelongsToManyTest.php +++ b/tests/TestCase/ORM/Association/BelongsToManyTest.php @@ -980,4 +980,66 @@ public function testEagerLoadingBelongsToManyLimitedFields() $this->assertNotEmpty($result->tags[0]->id); $this->assertEmpty($result->tags[0]->name); } + + /** + * Test that association proxy find() applies joins when conditions are involved. + * + * @return void + */ + public function testAssociationProxyFindWithConditions() + { + $table = TableRegistry::get('Articles'); + $table->belongsToMany('Tags', [ + 'foreignKey' => 'article_id', + 'associationForeignKey' => 'tag_id', + 'conditions' => ['SpecialTags.highlighted' => true], + 'through' => 'SpecialTags' + ]); + $query = $table->Tags->find(); + $result = $query->toArray(); + $this->assertCount(1, $result); + } + + /** + * Test that matching() works on belongsToMany associations. + * + * @return void + */ + public function testBelongsToManyAssociationWithConditions() + { + $table = TableRegistry::get('Articles'); + $table->belongsToMany('Tags', [ + 'foreignKey' => 'article_id', + 'associationForeignKey' => 'tag_id', + 'conditions' => ['SpecialTags.highlighted' => true], + 'through' => 'SpecialTags' + ]); + $query = $table->find()->matching('Tags', function ($q) { + return $q->where(['Tags.name' => 'tag1']); + }); + $results = $query->toArray(); + $this->assertCount(1, $results); + $this->assertNotEmpty($results[0]->_matchingData); + } + + /** + * Test that association proxy find() with matching resolves joins correctly + * + * @return void + */ + public function testAssociationProxyFindWithConditionsMatching() + { + $table = TableRegistry::get('Articles'); + $table->belongsToMany('Tags', [ + 'foreignKey' => 'article_id', + 'associationForeignKey' => 'tag_id', + 'conditions' => ['SpecialTags.highlighted' => true], + 'through' => 'SpecialTags' + ]); + $query = $table->Tags->find()->matching('Articles', function ($query) { + return $query->where(['Articles.id' => 1]); + }); + // The inner join on special_tags excludes the results. + $this->assertEquals(0, $query->count()); + } } diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index 0067e6b6478..3a9cffce06e 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -101,68 +101,6 @@ public function testEagerLoadingBelongsToManyList() $table->find()->contain('Tags')->toArray(); } - /** - * Test that association proxy find() applies joins when conditions are involved. - * - * @return void - */ - public function testBelongsToManyAssociationProxyFindWithConditions() - { - $table = TableRegistry::get('Articles'); - $table->belongsToMany('Tags', [ - 'foreignKey' => 'article_id', - 'associationForeignKey' => 'tag_id', - 'conditions' => ['SpecialTags.highlighted' => true], - 'through' => 'SpecialTags' - ]); - $query = $table->Tags->find(); - $result = $query->toArray(); - $this->assertCount(1, $result); - } - - /** - * Test that matching() works on belongsToMany associations. - * - * @return void - */ - public function testMatchingOnBelongsToManyAssociationWithConditions() - { - $table = TableRegistry::get('Articles'); - $table->belongsToMany('Tags', [ - 'foreignKey' => 'article_id', - 'associationForeignKey' => 'tag_id', - 'conditions' => ['SpecialTags.highlighted' => true], - 'through' => 'SpecialTags' - ]); - $query = $table->find()->matching('Tags', function ($q) { - return $q->where(['Tags.name' => 'tag1']); - }); - $results = $query->toArray(); - $this->assertCount(1, $results); - $this->assertNotEmpty($results[0]->_matchingData); - } - - /** - * Test that association proxy find() with matching resolves joins correctly - * - * @return void - */ - public function testBelongsToManyAssociationProxyFindWithConditionsMatching() - { - $table = TableRegistry::get('Articles'); - $table->belongsToMany('Tags', [ - 'foreignKey' => 'article_id', - 'associationForeignKey' => 'tag_id', - 'conditions' => ['SpecialTags.highlighted' => true], - 'through' => 'SpecialTags' - ]); - $query = $table->Tags->find()->matching('Articles', function ($query) { - return $query->where(['Articles.id' => 1]); - }); - // The inner join on special_tags excludes the results. - $this->assertEquals(0, $query->count()); - } - /** * Tests that duplicate aliases in contain() can be used, even when they would * naturally be attached to the query instead of eagerly loaded. What should