Browse Source

Adding a test case for using a custom finder for associations

Jose Lorenzo Rodriguez 11 years ago
parent
commit
684d556e34

+ 19 - 0
tests/TestCase/ORM/QueryTest.php

@@ -1771,6 +1771,25 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * Tests that custom finders are applied to associations when using the proxies
+ *
+ * @return void
+ */
+	public function testCustomFinderInBelongsTo() {
+		$table = TableRegistry::get('ArticlesTags');
+		$table->belongsTo('Articles', [
+			'className' => 'TestApp\Model\Table\ArticlesTable',
+			'finder' => 'published'
+		]);
+		$result = $table->find()->contain('Articles');
+		$this->assertCount(4, $result->extract('article')->filter()->toArray());
+		$table->Articles->updateAll(['published' => 'N'], ['1 = 1']);
+
+		$result = $table->find()->contain('Articles');
+		$this->assertCount(0, $result->extract('article')->filter()->toArray());
+	}
+
+/**
  * Tests that it is possible to attach more association when using a query
  * builder for other associations
  *

+ 10 - 0
tests/test_app/TestApp/Model/Table/ArticlesTable.php

@@ -26,6 +26,16 @@ class ArticlesTable extends Table {
 	}
 
 /**
+ * Find published
+ *
+ * @param Cake\ORM\Query $query The query
+ * @return Cake\ORM\Query
+ */
+	public function findPublished($query) {
+		return $query->where(['published' => 'Y']);
+	}
+
+/**
  * Example public method
  *
  * @return void