Browse Source

Sending all options to finder method from Paginator

Add a test for the paginator->find method to persist options.
Patrick Conroy 11 years ago
parent
commit
df3451853f

+ 1 - 1
src/Controller/Component/PaginatorComponent.php

@@ -164,7 +164,7 @@ class PaginatorComponent extends Component {
 		unset($options['finder'], $options['maxLimit']);
 
 		if (empty($query)) {
-			$query = $object->find($type, isset($options['finderOptions']) ? $options['finderOptions'] : []);
+			$query = $object->find($type, $options);
 		}
 
 		$query->applyOptions($options);

+ 33 - 1
tests/TestCase/Controller/ControllerTest.php

@@ -207,7 +207,10 @@ class ControllerTest extends TestCase {
  */
 	public $fixtures = array(
 		'core.post',
-		'core.comment'
+		'core.comment',
+		'core.article',
+		'core.articles_tag',
+		'core.tag'
 	);
 
 /**
@@ -871,4 +874,33 @@ class ControllerTest extends TestCase {
 		$this->assertTrue(isset($registry->Paginator));
 	}
 
+/**
+ * Testing that when you paginate, your options persist over to your custom finder.
+ * Using fixture data, this tests by uses matching() in both places.
+ * If the Table gets the 'tags' array successfully, it will do a matching().
+ *
+ * @return void
+ */
+	public function testPaginateSendsFinderOptions() {
+		$request = new Request('/');
+		$request->params['pass'] = array();
+		$response = $this->getMock('Cake\Network\Response');
+		$testTags = [2, 3];
+		$Controller = new Controller($request, $response);
+		$Controller->loadModel('Articles');
+		$Controller->paginate = [
+			'Articles' => [
+				'finder' => 'customTags',
+				'tags' => $testTags,
+				'maxLimit' => 1000
+			]
+		];
+
+		$result = $Controller->paginate('Articles')->count();
+		$expected = $Controller->Articles->find('all')->matching('Tags', function($q) use ($testTags) {
+			return $q->where(['Tags.id IN' => $testTags]);
+		})->count();
+
+		$this->assertEquals($expected, $result);
+	}
 }

+ 1 - 1
tests/TestCase/Shell/Task/TestTaskTest.php

@@ -200,7 +200,7 @@ class TestTaskTest extends TestCase {
  */
 	public function testMethodIntrospection() {
 		$result = $this->Task->getTestableMethods('TestApp\Model\Table\ArticlesTable');
-		$expected = ['findpublished', 'dosomething', 'dosomethingelse'];
+		$expected = ['findpublished', 'dosomething', 'dosomethingelse', 'findcustomtags'];
 		$this->assertEquals($expected, array_map('strtolower', $result));
 	}
 

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

@@ -11,6 +11,7 @@
  */
 namespace TestApp\Model\Table;
 
+use Cake\ORM\Query;
 use Cake\ORM\Table;
 
 /**
@@ -52,6 +53,22 @@ class ArticlesTable extends Table {
 	}
 
 /**
+ * Custom finder, used with fixture data to ensure Paginator is sending options
+ *
+ * @param Cake\ORM\Query $query
+ * @param array $options
+ * @return Cake\ORM\Query
+ */
+	public function findCustomTags(Query $query, array $options = []) {
+		if (isset($options['tags']) && is_array($options['tags'])) {
+			return $query->matching('Tags', function($q) use ($options) {
+				return $q->where(['Tags.id IN' => $options['tags']]);
+			});
+		}
+		return $query;
+	}
+
+/**
  * Example protected method
  *
  * @return void