From 12a8b146610fde2fe293b55d7443039d1a0d4a72 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 20 Aug 2021 10:11:34 -0400 Subject: [PATCH] APIv4 - Properly show deprecated joins in unit tests --- Civi/Api4/Query/Api4SelectQuery.php | 2 +- Civi/Api4/Service/Schema/Joiner.php | 4 ++-- tests/phpunit/api/v4/Action/FkJoinTest.php | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 7232d02b3f..6e8cb701e4 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -988,7 +988,7 @@ class Api4SelectQuery { try { $joinPath = $joiner->autoJoin($this, $pathArray); } - catch (\Exception $e) { + catch (\API_Exception $e) { return; } $lastLink = array_pop($joinPath); diff --git a/Civi/Api4/Service/Schema/Joiner.php b/Civi/Api4/Service/Schema/Joiner.php index b09aaaef32..d64cac920a 100644 --- a/Civi/Api4/Service/Schema/Joiner.php +++ b/Civi/Api4/Service/Schema/Joiner.php @@ -106,7 +106,7 @@ class Joiner { * @param array $joinPath * * @return \Civi\Api4\Service\Schema\Joinable\Joinable[] - * @throws \Exception + * @throws \API_Exception */ protected function getPath(string $baseTable, array $joinPath) { $cacheKey = sprintf('%s.%s', $baseTable, implode('.', $joinPath)); @@ -117,7 +117,7 @@ class Joiner { $links = $this->schemaMap->getPath($baseTable, $targetAlias); if (empty($links)) { - throw new \Exception(sprintf('Cannot join %s to %s', $baseTable, $targetAlias)); + throw new \API_Exception(sprintf('Cannot join %s to %s', $baseTable, $targetAlias)); } else { $fullPath = array_merge($fullPath, $links); diff --git a/tests/phpunit/api/v4/Action/FkJoinTest.php b/tests/phpunit/api/v4/Action/FkJoinTest.php index d0ed5c87b1..213726b6e2 100644 --- a/tests/phpunit/api/v4/Action/FkJoinTest.php +++ b/tests/phpunit/api/v4/Action/FkJoinTest.php @@ -374,4 +374,22 @@ class FkJoinTest extends UnitTestCase { $this->assertEquals('TesterCo', $emailGet['contact_id.employer_id.display_name']); } + public function testDeprecatedJoins() { + $message = ''; + try { + \Civi\Api4\Email::get(FALSE) + ->addWhere('contact.first_name', '=', 'Peter') + ->addWhere('contact.last_name', '=', '') + ->addWhere('contact.is_deleted', '=', 0) + ->addWhere('contact.is_deceased', '=', 0) + ->addWhere('email', '=', '') + ->addWhere('is_primary', '=', TRUE) + ->setSelect(['contact_id'])->execute(); + } + catch (\Exception $e) { + $message = $e->getMessage(); + } + $this->assertStringContainsString("Deprecated join alias 'contact' used in APIv4 get. Should be changed to 'contact_id'", $message); + } + } -- 2.25.1