Merge pull request #17874 from colemanw/checkPermShort
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ContactGetTest.php
index 78d78eb3752eb61f1ce76e23313e85f9758cbc90..e7d5ba1adaae54a720cc33fa9f85e466ea940fb6 100644 (file)
@@ -58,4 +58,38 @@ class ContactGetTest extends \api\v4\UnitTestCase {
     $this->assertContains($del['id'], $contacts->column('id'));
   }
 
+  public function testGetWithLimit() {
+    $last_name = uniqid('getWithLimitTest');
+
+    $bob = Contact::create()
+      ->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
+      ->execute()->first();
+
+    $jan = Contact::create()
+      ->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
+      ->execute()->first();
+
+    $dan = Contact::create()
+      ->setValues(['first_name' => 'Dan', 'last_name' => $last_name])
+      ->execute()->first();
+
+    $num = Contact::get(FALSE)->selectRowCount()->execute()->count();
+
+    // The object's count() method will account for all results, ignoring limit & offset, while the array results are limited
+    $offset1 = Contact::get(FALSE)->setOffset(1)->execute();
+    $this->assertCount($num, $offset1);
+    $this->assertCount($num - 1, (array) $offset1);
+    $offset2 = Contact::get(FALSE)->setOffset(2)->execute();
+    $this->assertCount($num - 2, (array) $offset2);
+    $this->assertCount($num, $offset2);
+    // With limit, it doesn't fetch total count by default
+    $limit2 = Contact::get(FALSE)->setLimit(2)->execute();
+    $this->assertCount(2, (array) $limit2);
+    $this->assertCount(2, $limit2);
+    // With limit, you have to trigger the full row count manually
+    $limit2 = Contact::get(FALSE)->setLimit(2)->addSelect('sort_name', 'row_count')->execute();
+    $this->assertCount(2, (array) $limit2);
+    $this->assertCount($num, $limit2);
+  }
+
 }