From 340afbdf1f72632b7384cfe81686d8e9ffaa2488 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 5 May 2020 15:52:08 -0400 Subject: [PATCH] APIv4 - Deprecate option_value joins in favor of pseudoconstant suffix --- Civi/Api4/Query/Api4SelectQuery.php | 4 ++++ .../api/v4/Action/ComplexQueryTest.php | 2 +- .../v4/Action/CreateWithOptionGroupTest.php | 20 +++++++++---------- tests/phpunit/api/v4/Action/FkJoinTest.php | 2 +- .../api/v4/Entity/ContactInterchangeTest.php | 8 ++++---- .../phpunit/api/v4/Entity/ContactJoinTest.php | 9 +++------ .../Query/Api4SelectQueryComplexJoinTest.php | 6 ++---- .../phpunit/api/v4/Query/OneToOneJoinTest.php | 6 +++--- .../api/v4/Query/OptionValueJoinTest.php | 8 ++++---- 9 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 63cba4e280..d7e6da44c8 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -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 */ diff --git a/tests/phpunit/api/v4/Action/ComplexQueryTest.php b/tests/phpunit/api/v4/Action/ComplexQueryTest.php index 6a383e22e6..e859e144bd 100644 --- a/tests/phpunit/api/v4/Action/ComplexQueryTest.php +++ b/tests/phpunit/api/v4/Action/ComplexQueryTest.php @@ -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)); diff --git a/tests/phpunit/api/v4/Action/CreateWithOptionGroupTest.php b/tests/phpunit/api/v4/Action/CreateWithOptionGroupTest.php index 0a101f0274..9992be8ca7 100644 --- a/tests/phpunit/api/v4/Action/CreateWithOptionGroupTest.php +++ b/tests/phpunit/api/v4/Action/CreateWithOptionGroupTest.php @@ -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']); } diff --git a/tests/phpunit/api/v4/Action/FkJoinTest.php b/tests/phpunit/api/v4/Action/FkJoinTest.php index 2a1ec5d0f2..babd1cec0f 100644 --- a/tests/phpunit/api/v4/Action/FkJoinTest.php +++ b/tests/phpunit/api/v4/Action/FkJoinTest.php @@ -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); diff --git a/tests/phpunit/api/v4/Entity/ContactInterchangeTest.php b/tests/phpunit/api/v4/Entity/ContactInterchangeTest.php index 57bc3fd122..ff34cc04d6 100644 --- a/tests/phpunit/api/v4/Entity/ContactInterchangeTest.php +++ b/tests/phpunit/api/v4/Entity/ContactInterchangeTest.php @@ -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([ diff --git a/tests/phpunit/api/v4/Entity/ContactJoinTest.php b/tests/phpunit/api/v4/Entity/ContactJoinTest.php index b0ac14c98e..0b9e16d11e 100644 --- a/tests/phpunit/api/v4/Entity/ContactJoinTest.php +++ b/tests/phpunit/api/v4/Entity/ContactJoinTest.php @@ -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']); } } diff --git a/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php b/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php index c466cee34b..e51ed6b49e 100644 --- a/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php +++ b/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php @@ -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]); diff --git a/tests/phpunit/api/v4/Query/OneToOneJoinTest.php b/tests/phpunit/api/v4/Query/OneToOneJoinTest.php index a87269420a..138034c77b 100644 --- a/tests/phpunit/api/v4/Query/OneToOneJoinTest.php +++ b/tests/phpunit/api/v4/Query/OneToOneJoinTest.php @@ -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'); } } diff --git a/tests/phpunit/api/v4/Query/OptionValueJoinTest.php b/tests/phpunit/api/v4/Query/OptionValueJoinTest.php index ebed0bce71..eac88f25a0 100644 --- a/tests/phpunit/api/v4/Query/OptionValueJoinTest.php +++ b/tests/phpunit/api/v4/Query/OptionValueJoinTest.php @@ -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 ); } -- 2.25.1