add tests, handle nots
authorlarssandergreen <lars@wildsight.ca>
Sun, 2 Oct 2022 17:33:34 +0000 (11:33 -0600)
committerlarssandergreen <lars@wildsight.ca>
Sun, 2 Oct 2022 17:33:34 +0000 (11:33 -0600)
Civi/Api4/Utils/FormattingUtil.php
tests/phpunit/api/v4/Action/DateTest.php

index 7b66db6f73061ea405c1f15a74c445e3f681d2f0..788a5b73b78a768bfe74a2d995f7bd62396cc7ec 100644 (file)
@@ -161,11 +161,11 @@ class FormattingUtil {
         $operator = ($operator === '=' || $operator === 'LIKE') ? 'BETWEEN' : 'NOT BETWEEN';
 
         if (is_null($dateFrom) && !is_null($dateTo)) {
-          $operator = '<=';
+          $operator = ($operator === 'BETWEEN') ? '<=' : '>=';
           return self::formatDateValue($format, $dateTo);
         }
         elseif (!is_null($dateFrom) && is_null($dateTo)) {
-          $operator = '>=';
+          $operator = ($operator === 'BETWEEN') ? '>=' : '<=';
           return self::formatDateValue($format, $dateFrom);
         }
         else {
index b58a59ad06d3a96eb7fa5705138df6ee378aed76..e61a22313e74a09eb054de4a64bade73410bf501 100644 (file)
@@ -197,6 +197,48 @@ class DateTest extends Api4TestBase implements TransactionalInterface {
       ->addWhere('id', '=', $c1)
       ->execute();
     $this->assertCount(2, $contact);
+
+    // Find contributions To end of previous calendar year
+    $contact = \Civi\Api4\Contact::get()
+      ->addSelect('id', 'contribution.total_amount')
+      ->setJoin([
+        ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '=', '"earlier.year"']],
+      ])
+      ->addWhere('id', '=', $c1)
+      ->execute();
+    $this->assertCount(2, $contact);
+
+    // Find contributions not To end of previous calendar year
+    $contact = \Civi\Api4\Contact::get()
+      ->addSelect('id', 'contribution.total_amount')
+      ->setJoin([
+        ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '!=', '"earlier.year"']],
+      ])
+      ->addWhere('id', '=', $c1)
+      ->execute();
+    $this->assertCount(1, $contact);
+    $this->assertEquals(6, $contact[0]['contribution.total_amount']);
+
+    // Find contributions From start of current calendar year
+    $contact = \Civi\Api4\Contact::get()
+      ->addSelect('id', 'contribution.total_amount')
+      ->setJoin([
+        ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '=', '"greater.year"']],
+      ])
+      ->addWhere('id', '=', $c1)
+      ->execute();
+    $this->assertCount(1, $contact);
+    $this->assertEquals(6, $contact[0]['contribution.total_amount']);
+
+    // Find contributions not From start of current calendar year
+    $contact = \Civi\Api4\Contact::get()
+      ->addSelect('id', 'contribution.total_amount')
+      ->setJoin([
+        ['Contribution AS contribution', FALSE, NULL, ['contribution.receive_date', '!=', '"greater.year"']],
+      ])
+      ->addWhere('id', '=', $c1)
+      ->execute();
+    $this->assertCount(2, $contact);
   }
 
 }