Browse Source

Add test to prove #5150

Walther Lalk 11 years ago
parent
commit
dad681999e
1 changed files with 48 additions and 0 deletions
  1. 48 0
      tests/TestCase/ORM/QueryTest.php

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

@@ -1327,6 +1327,26 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * test count with a beforeFind.
+ *
+ * @return void
+ */
+	public function testCountBeforeFind() {
+		$table = TableRegistry::get('Articles');
+		$table->hasMany('Comments');
+		$table->eventManager()
+			->attach(function ($event, $query) {
+				$query
+					->limit(1)
+					->order(['Articles.title' => 'DESC']);
+			}, 'Model.beforeFind');
+
+		$query = $table->find();
+		$result = $query->count();
+		$this->assertSame(3, $result);
+	}
+
+/**
  * Test that count() returns correct results with group by.
  *
  * @return void
@@ -2120,6 +2140,34 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * test that cleanCopy makes a cleaned up clone with a beforeFind.
+ *
+ * @return void
+ */
+	public function testCleanCopyBeforeFind() {
+		$table = TableRegistry::get('Articles');
+		$table->hasMany('Comments');
+		$table->eventManager()
+			->attach(function($event, $query) {
+				$query
+					->limit(5)
+					->order(['Articles.title' => 'DESC']);
+			}, 'Model.beforeFind');
+
+		$query = $table->find();
+		$query->offset(10)
+			->limit(1)
+			->order(['Articles.id' => 'DESC'])
+			->contain(['Comments']);
+		$copy = $query->cleanCopy();
+
+		$this->assertNotSame($copy, $query);
+		$this->assertNull($copy->clause('offset'));
+		$this->assertNull($copy->clause('limit'));
+		$this->assertNull($copy->clause('order'));
+	}
+
+/**
  * Test that finder options sent through via contain are sent to custom finder.
  *
  * @return void