APIv4 - Deprecate option_value joins in favor of pseudoconstant suffix
authorColeman Watts <coleman@civicrm.org>
Tue, 5 May 2020 19:52:08 +0000 (15:52 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 5 May 2020 19:52:08 +0000 (15:52 -0400)
Civi/Api4/Query/Api4SelectQuery.php
tests/phpunit/api/v4/Action/ComplexQueryTest.php
tests/phpunit/api/v4/Action/CreateWithOptionGroupTest.php
tests/phpunit/api/v4/Action/FkJoinTest.php
tests/phpunit/api/v4/Entity/ContactInterchangeTest.php
tests/phpunit/api/v4/Entity/ContactJoinTest.php
tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php
tests/phpunit/api/v4/Query/OneToOneJoinTest.php
tests/phpunit/api/v4/Query/OptionValueJoinTest.php

index 63cba4e2807b06bbf60e3a25e904177da3ea6841..d7e6da44c8138e75ead7e3248d389dce45ee1be1 100644 (file)
@@ -16,6 +16,7 @@ use Civi\Api4\Event\Events;
 use Civi\Api4\Event\PostSelectQueryEvent;
 use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
 use Civi\Api4\Service\Schema\Joinable\Joinable;
+use Civi\Api4\Service\Schema\Joinable\OptionValueJoinable;
 use Civi\Api4\Utils\FormattingUtil;
 use Civi\Api4\Utils\CoreUtil;
 use Civi\Api4\Utils\SelectUtil;
@@ -429,6 +430,9 @@ class Api4SelectQuery extends SelectQuery {
       if ($joinable->getJoinType() === Joinable::JOIN_TYPE_ONE_TO_MANY) {
         $isMany = TRUE;
       }
+      if ($joinable instanceof OptionValueJoinable) {
+        \Civi::log()->warning('Use API pseudoconstant suffix like :name or :label instead of join.', ['civi.tag' => 'deprecated']);
+      }
     }
 
     /** @var \Civi\Api4\Service\Schema\Joinable\Joinable $lastLink */
index 6a383e22e616bb27b0b4141ea201b905c0ed9f58..e859e144bd27bbad4f61c6acc4a916a083dd60ad 100644 (file)
@@ -50,7 +50,7 @@ class ComplexQueryTest extends UnitTestCase {
   public function testGetAllHousingSupportActivities() {
     $results = Activity::get()
       ->setCheckPermissions(FALSE)
-      ->addWhere('activity_type.name', '=', 'Phone Call')
+      ->addWhere('activity_type_id:name', '=', 'Phone Call')
       ->execute();
 
     $this->assertGreaterThan(0, count($results));
index 0a101f02741b034dfe7db208f8b7bc934d60a66b..9992be8ca70c4bec1589e05221b55e2bf93a3eee 100644 (file)
@@ -99,16 +99,16 @@ class CreateWithOptionGroupTest extends BaseCustomValueTest {
     $result = Contact::get()
       ->setCheckPermissions(FALSE)
       ->addSelect('first_name')
-      ->addSelect("$group.$colorField.label")
-      ->addSelect("$group.$foodField.label")
+      ->addSelect("$group.$colorField:label")
+      ->addSelect("$group.$foodField:label")
       ->addSelect('FinancialStuff.Salary')
-      ->addWhere("$group.$foodField.label", 'IN', ['Corn', 'Potatoes'])
+      ->addWhere("$group.$foodField:label", 'IN', ['Corn', 'Potatoes'])
       ->addWhere('FinancialStuff.Salary', '>', '10000')
       ->execute()
       ->first();
 
-    $this->assertEquals('Red', $result["$group.$colorField.label"]);
-    $this->assertEquals('Corn', $result["$group.$foodField.label"]);
+    $this->assertEquals('Red', $result["$group.$colorField:label"]);
+    $this->assertEquals('Corn', $result["$group.$foodField:label"]);
     $this->assertEquals(50000, $result['FinancialStuff.Salary']);
   }
 
@@ -183,10 +183,10 @@ class CreateWithOptionGroupTest extends BaseCustomValueTest {
       ->setCheckPermissions(FALSE)
       ->addSelect('first_name')
       ->addSelect('last_name')
-      ->addSelect("$group.$colorField.label")
-      ->addSelect("$group.$foodField.label")
+      ->addSelect("$group.$colorField:label")
+      ->addSelect("$group.$foodField:label")
       ->addSelect('FinancialStuff.Salary')
-      ->addWhere("$group.$foodField.label", 'IN', ['Corn', 'Cheese'])
+      ->addWhere("$group.$foodField:label", 'IN', ['Corn', 'Cheese'])
       ->execute();
 
     $blueCheese = NULL;
@@ -196,8 +196,8 @@ class CreateWithOptionGroupTest extends BaseCustomValueTest {
       }
     }
 
-    $this->assertEquals('Blue', $blueCheese["$group.$colorField.label"]);
-    $this->assertEquals('Cheese', $blueCheese["$group.$foodField.label"]);
+    $this->assertEquals('Blue', $blueCheese["$group.$colorField:label"]);
+    $this->assertEquals('Cheese', $blueCheese["$group.$foodField:label"]);
     $this->assertEquals(500000, $blueCheese['FinancialStuff.Salary']);
   }
 
index 2a1ec5d0f232fd4f55779df75cf68795da172f7c..babd1cec0fabeb1dda14764ab839aef72eefa605 100644 (file)
@@ -49,7 +49,7 @@ class FkJoinTest extends UnitTestCase {
   public function testThreeLevelJoin() {
     $results = Activity::get()
       ->setCheckPermissions(FALSE)
-      ->addWhere('activity_type.name', '=', 'Phone Call')
+      ->addWhere('activity_type_id:name', '=', 'Phone Call')
       ->execute();
 
     $this->assertCount(1, $results);
index 57bc3fd12235973427381604a1306c1c27bc7168..ff34cc04d6ef718caf0920d2a03cf18bbc4ce634 100644 (file)
@@ -217,7 +217,7 @@ class ContactInterchangeTest extends UnitTestCase implements TransactionalInterf
 
   public function readNameByActSubjectJoin_4($cid, $strs) {
     $get = ActivityContact::get()
-      ->addWhere('activity_contacts.label', '=', 'Activity Source')
+      ->addWhere('record_type_id:label', '=', 'Activity Source')
       ->addWhere('activity.subject', '=', $strs['api'])
       ->addSelect('contact.first_name')
       ->execute();
@@ -226,7 +226,7 @@ class ContactInterchangeTest extends UnitTestCase implements TransactionalInterf
 
   public function readNameByActSubjectChain_4($cid, $strs) {
     $get = ActivityContact::get()
-      ->addWhere('activity_contacts.label', '=', 'Activity Source')
+      ->addWhere('record_type_id:label', '=', 'Activity Source')
       ->addWhere('activity.subject', '=', $strs['api'])
       ->setSelect(['activity_id', 'contact_id'])
       ->setChain([
@@ -269,7 +269,7 @@ class ContactInterchangeTest extends UnitTestCase implements TransactionalInterf
 
   public function readNameByActDetailsJoin_4($cid, $strs) {
     $get = ActivityContact::get()
-      ->addWhere('activity_contacts.label', '=', 'Activity Source')
+      ->addWhere('record_type_id:label', '=', 'Activity Source')
       ->addWhere('activity.details', '=', $strs['api'])
       ->addSelect('contact.first_name')
       ->execute();
@@ -278,7 +278,7 @@ class ContactInterchangeTest extends UnitTestCase implements TransactionalInterf
 
   public function readNameByActDetailsChain_4($cid, $strs) {
     $get = ActivityContact::get()
-      ->addWhere('activity_contacts.label', '=', 'Activity Source')
+      ->addWhere('record_type_id:label', '=', 'Activity Source')
       ->addWhere('activity.details', '=', $strs['api'])
       ->setSelect(['activity_id', 'contact_id'])
       ->setChain([
index b0ac14c98e259e07cbf73d5ac481f78972427adf..0b9e16d11e70d60b2129808dc43d87253fefbac6 100644 (file)
@@ -84,7 +84,7 @@ class ContactJoinTest extends UnitTestCase {
 
   public function testJoinToPCMOptionValueWillShowLabel() {
     $options = OptionValue::get()
-      ->addWhere('option_group.name', '=', 'preferred_communication_method')
+      ->addWhere('option_group_id:name', '=', 'preferred_communication_method')
       ->execute()
       ->getArrayCopy();
 
@@ -109,14 +109,11 @@ class ContactJoinTest extends UnitTestCase {
 
     $fetchedContact = Contact::get()
       ->addWhere('id', 'IN', $contactIds)
-      ->addSelect('preferred_communication_method.label')
+      ->addSelect('preferred_communication_method:label')
       ->execute()
       ->first();
 
-    $preferredMethod = $fetchedContact['preferred_communication_method'];
-    $returnedLabels = array_column($preferredMethod, 'label');
-
-    $this->assertEquals($labels, $returnedLabels);
+    $this->assertEquals($labels, $fetchedContact['preferred_communication_method:label']);
   }
 
 }
index c466cee34bf2bc6d3fc86909ab7991b2117ae436..e51ed6b49e059d443c0dbf1f92c587d5d6a1a10b 100644 (file)
@@ -54,7 +54,7 @@ class Api4SelectQueryComplexJoinTest extends UnitTestCase {
     $query->select[] = 'emails.location_type.name';
     $query->select[] = 'created_activities.contact_id';
     $query->select[] = 'created_activities.activity.subject';
-    $query->select[] = 'created_activities.activity.activity_type.name';
+    $query->select[] = 'created_activities.activity.activity_type_id:name';
     $query->where[] = ['first_name', '=', 'Single'];
     $query->where[] = ['id', '=', $this->getReference('test_contact_1')['id']];
     $results = $query->run();
@@ -72,9 +72,7 @@ class Api4SelectQueryComplexJoinTest extends UnitTestCase {
     $this->assertArrayHasKey('activity', $firstCreatedActivity);
     $firstActivity = $firstCreatedActivity['activity'];
     $this->assertContains($firstActivity['subject'], $activitySubjects);
-    $this->assertArrayHasKey('activity_type', $firstActivity);
-    $activityType = $firstActivity['activity_type'];
-    $this->assertArrayHasKey('name', $activityType);
+    $this->assertArrayHasKey('activity_type_id:name', $firstActivity);
 
     $this->assertArrayHasKey('name', $firstResult['emails'][0]['location_type']);
     $this->assertArrayHasKey('location_type_id', $firstResult['phones'][0]);
index a87269420abc78ab2a851ff6582fa6d700eb10ad..138034c77b69bfdd6975c27ba2e74ea2396e2a90 100644 (file)
@@ -50,14 +50,14 @@ class OneToOneJoinTest extends UnitTestCase {
 
     $contacts = Contact::get()
       ->addWhere('id', 'IN', [$armenianContact['id'], $basqueContact['id']])
-      ->addSelect('preferred_language.label')
+      ->addSelect('preferred_language:label')
       ->addSelect('last_name')
       ->execute()
       ->indexBy('last_name')
       ->getArrayCopy();
 
-    $this->assertEquals($contacts['One']['preferred_language.label'], 'Armenian');
-    $this->assertEquals($contacts['Two']['preferred_language.label'], 'Basque');
+    $this->assertEquals($contacts['One']['preferred_language:label'], 'Armenian');
+    $this->assertEquals($contacts['Two']['preferred_language:label'], 'Basque');
   }
 
 }
index ebed0bce715e9bae2827197cf67a94baa73ab574..eac88f25a0225c58a0186714d17972a5d122f820 100644 (file)
@@ -51,18 +51,18 @@ class OptionValueJoinTest extends UnitTestCase {
     $api = \Civi\API\Request::create('Contact', 'get', ['version' => 4, 'checkPermissions' => FALSE]);
     $query = new Api4SelectQuery($api);
     $query->select[] = 'first_name';
-    $query->select[] = 'preferred_communication_method.label';
+    $query->select[] = 'preferred_communication_method:label';
     $query->where[] = ['preferred_communication_method', 'IS NOT NULL'];
     $results = $query->run();
     $first = array_shift($results);
     $keys = array_keys($first);
     sort($keys);
-    $this->assertEquals(['first_name', 'id', 'preferred_communication_method'], $keys);
-    $firstPreferredMethod = array_shift($first['preferred_communication_method']);
+    $this->assertEquals(['first_name', 'id', 'preferred_communication_method:label'], $keys);
+    $firstPreferredMethod = array_shift($first['preferred_communication_method:label']);
 
     $this->assertEquals(
       'Phone',
-      $firstPreferredMethod['label']
+      $firstPreferredMethod
     );
   }