From f76dfa8883f90e3d1ce77edd755bc3100bd71883 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 19 Jan 2023 12:47:39 +1300 Subject: [PATCH] dev/core#4088 Superficial cleanup on Api4testBase --- .../Civi/Afform/AfformContactSummaryTest.php | 3 +- .../api/v4/SearchDisplay/SearchRunTest.php | 3 +- .../SearchRunWithCustomFieldTest.php | 3 +- .../CRM/Report/Form/ContactSummaryTest.php | 6 +- .../api/v4/Action/AutocompleteTest.php | 3 +- .../api/v4/Action/BasicActionsTest.php | 3 +- .../api/v4/Action/ContactIsDeletedTest.php | 3 +- tests/phpunit/api/v4/Api4TestBase.php | 92 ++++++++++--------- 8 files changed, 66 insertions(+), 50 deletions(-) diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformContactSummaryTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformContactSummaryTest.php index 21aafaeb5f..ab2aca9b4f 100644 --- a/ext/afform/core/tests/phpunit/Civi/Afform/AfformContactSummaryTest.php +++ b/ext/afform/core/tests/phpunit/Civi/Afform/AfformContactSummaryTest.php @@ -5,6 +5,7 @@ namespace Civi\Afform; require_once __DIR__ . '/../../../../../../../tests/phpunit/api/v4/Api4TestBase.php'; use Civi\Api4\Afform; +use Civi\Test\CiviEnvBuilder; /** * @group headless @@ -19,7 +20,7 @@ class AfformContactSummaryTest extends \api\v4\Api4TestBase { 'contact_summary_test5', ]; - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { return \Civi\Test::headless()->installMe(__DIR__)->install('org.civicrm.search_kit')->apply(); } diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 819d3b879f..985d000248 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -14,6 +14,7 @@ use Civi\Api4\Phone; use Civi\Api4\SavedSearch; use Civi\Api4\SearchDisplay; use Civi\Api4\UFMatch; +use Civi\Test\CiviEnvBuilder; use Civi\Test\TransactionalInterface; /** @@ -22,7 +23,7 @@ use Civi\Test\TransactionalInterface; class SearchRunTest extends Api4TestBase implements TransactionalInterface { use \Civi\Test\ACLPermissionTrait; - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest return \Civi\Test::headless() diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunWithCustomFieldTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunWithCustomFieldTest.php index 0e5bf67769..9497758b10 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunWithCustomFieldTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunWithCustomFieldTest.php @@ -10,13 +10,14 @@ use Civi\Api4\Activity; use Civi\Api4\Contact; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; +use Civi\Test\CiviEnvBuilder; /** * @group headless */ class SearchRunWithCustomFieldTest extends CustomTestBase { - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { return \Civi\Test::headless() ->installMe(__DIR__) ->apply(); diff --git a/tests/phpunit/CRM/Report/Form/ContactSummaryTest.php b/tests/phpunit/CRM/Report/Form/ContactSummaryTest.php index 83cc4cf686..5fda40038f 100644 --- a/tests/phpunit/CRM/Report/Form/ContactSummaryTest.php +++ b/tests/phpunit/CRM/Report/Form/ContactSummaryTest.php @@ -30,7 +30,7 @@ class CRM_Report_Form_ContactSummaryTest extends CiviReportTestCase { /** * Ensure the new Odd/Event street number sort column works correctly */ - public function testOddEvenStreetNumber() { + public function testOddEvenStreetNumber(): void { $customLocationType = $this->callAPISuccess('LocationType', 'create', [ 'name' => 'Custom Location Type', 'display_name' => 'CiviTest Custom Location Type', @@ -195,9 +195,9 @@ class CRM_Report_Form_ContactSummaryTest extends CiviReportTestCase { } /** - * Test that Loation Type prints out a sensible piece of data + * Test that Location Type prints out a sensible piece of data */ - public function testLocationTypeIdHandling() { + public function testLocationTypeIdHandling(): void { $customLocationType = $this->callAPISuccess('LocationType', 'create', [ 'name' => 'Custom Location Type', 'display_name' => 'CiviTest Custom Location Type', diff --git a/tests/phpunit/api/v4/Action/AutocompleteTest.php b/tests/phpunit/api/v4/Action/AutocompleteTest.php index 5b81a57b37..d26d18539e 100644 --- a/tests/phpunit/api/v4/Action/AutocompleteTest.php +++ b/tests/phpunit/api/v4/Action/AutocompleteTest.php @@ -25,6 +25,7 @@ use Civi\Api4\Contact; use Civi\Api4\MockBasicEntity; use Civi\Api4\SavedSearch; use Civi\Core\Event\GenericHookEvent; +use Civi\Test\CiviEnvBuilder; use Civi\Test\HookInterface; use Civi\Test\TransactionalInterface; @@ -54,7 +55,7 @@ class AutocompleteTest extends Api4TestBase implements HookInterface, Transactio } } - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { // TODO: search_kit should probably be part of the 'headless()' baseline. return \Civi\Test::headless()->install(['org.civicrm.search_kit'])->apply(); } diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index f9668e094d..fad0c8f079 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -23,6 +23,7 @@ use api\v4\Api4TestBase; use Civi\Api4\MockBasicEntity; use Civi\Api4\Utils\CoreUtil; use Civi\Core\Event\GenericHookEvent; +use Civi\Test\CiviEnvBuilder; use Civi\Test\HookInterface; use Civi\Test\TransactionalInterface; @@ -40,7 +41,7 @@ class BasicActionsTest extends Api4TestBase implements HookInterface, Transactio $e->entities['MockBasicEntity'] = MockBasicEntity::getInfo(); } - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { // Ensure MockBasicEntity gets added via above listener \Civi::cache('metadata')->clear(); return parent::setUpHeadless(); diff --git a/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php b/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php index 82f9201205..cc09b40a26 100644 --- a/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php +++ b/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php @@ -20,6 +20,7 @@ namespace api\v4\Action; use api\v4\Api4TestBase; +use Civi\Test\CiviEnvBuilder; use Civi\Test\TransactionalInterface; /** @@ -27,7 +28,7 @@ use Civi\Test\TransactionalInterface; */ class ContactIsDeletedTest extends Api4TestBase implements TransactionalInterface { - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { $relatedTables = [ 'civicrm_address', 'civicrm_email', diff --git a/tests/phpunit/api/v4/Api4TestBase.php b/tests/phpunit/api/v4/Api4TestBase.php index 41a7b7e594..3cec8ce020 100644 --- a/tests/phpunit/api/v4/Api4TestBase.php +++ b/tests/phpunit/api/v4/Api4TestBase.php @@ -19,8 +19,10 @@ namespace api\v4; +use Civi\Api4\Generic\Result; use Civi\Api4\UFMatch; use Civi\Api4\Utils\CoreUtil; +use Civi\Test\CiviEnvBuilder; use Civi\Test\HeadlessInterface; /** @@ -47,16 +49,19 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf error_reporting(E_ALL); } - public function setUpHeadless() { + public function setUpHeadless(): CiviEnvBuilder { return \Civi\Test::headless()->apply(); } /** + * Post test cleanup. + * + * @throws \CRM_Core_Exception */ public function tearDown(): void { - $impliments = class_implements($this); + $implements = class_implements($this); // If not created in a transaction, test records must be deleted - if (!in_array('Civi\Test\TransactionalInterface', $impliments, TRUE)) { + if (!in_array('Civi\Test\TransactionalInterface', $implements, TRUE)) { // Delete all test records in reverse order to prevent fk constraints foreach (array_reverse($this->testRecords) as $record) { $params = ['checkPermissions' => FALSE, 'where' => $record[1]]; @@ -78,28 +83,30 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * * @param array $params */ - public function cleanup($params) { + public function cleanup(array $params): void { $params += [ 'tablesToTruncate' => [], ]; - \CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;"); + \CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 0;'); foreach ($params['tablesToTruncate'] as $table) { \Civi::log()->info('truncating: ' . $table); $sql = "TRUNCATE TABLE $table"; \CRM_Core_DAO::executeQuery($sql); } - \CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;"); + \CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 1;'); } /** * Emulate a logged in user since certain functions use that. * value to store a record in the DB (like activity) + * * @see https://issues.civicrm.org/jira/browse/CRM-8180 * * @return int * Contact ID of the created user. + * @throws \CRM_Core_Exception */ - public function createLoggedInUser() { + public function createLoggedInUser(): int { $contactID = $this->createTestRecord('Contact')['id']; UFMatch::delete(FALSE)->addWhere('uf_id', '=', 6)->execute(); $this->createTestRecord('UFMatch', [ @@ -124,7 +131,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @throws \CRM_Core_Exception * @throws \Civi\API\Exception\NotImplementedException */ - public function createTestRecord(string $entityName, array $values = []) { + public function createTestRecord(string $entityName, array $values = []): ?array { return $this->saveTestRecords($entityName, ['records' => [$values]])->single(); } @@ -139,7 +146,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @throws \CRM_Core_Exception * @throws \Civi\API\Exception\NotImplementedException */ - public function saveTestRecords(string $entityName, array $saveParams) { + public function saveTestRecords(string $entityName, array $saveParams): Result { $saveParams += [ 'checkPermissions' => FALSE, 'defaults' => [], @@ -163,7 +170,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @param int $len * @return string */ - public function randomLetters(int $len = 10) { + public function randomLetters(int $len = 10): string { return \CRM_Utils_String::createRandom($len, implode('', range('a', 'z'))); } @@ -176,7 +183,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @return array * @throws \CRM_Core_Exception */ - public function getRequiredValuesToCreate(string $entity, &$values = []) { + public function getRequiredValuesToCreate(string $entity, array &$values = []): array { $requiredFields = civicrm_api4($entity, 'getfields', [ 'action' => 'create', 'loadOptions' => TRUE, @@ -185,7 +192,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf ['OR', [ ['required', '=', TRUE], - // Include contitionally-required fields only if they don't create a circular FK reference + // Include conditionally-required fields only if they don't create a circular FK reference ['AND', [['required_if', 'IS NOT EMPTY'], ['fk_entity', '!=', $entity]]], ], ], @@ -226,51 +233,51 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf case 'CaseType': $extraValues['definition'] = [ - "activityTypes" => [ + 'activityTypes' => [ [ - "name" => "Open Case", - "max_instances" => "1", + 'name' => 'Open Case', + 'max_instances' => 1, ], [ - "name" => "Follow up", + 'name' => 'Follow up', ], ], - "activitySets" => [ + 'activitySets' => [ [ - "name" => "standard_timeline", - "label" => "Standard Timeline", - "timeline" => 1, - "activityTypes" => [ + 'name' => 'standard_timeline', + 'label' => 'Standard Timeline', + 'timeline' => 1, + 'activityTypes' => [ [ - "name" => "Open Case", - "status" => "Completed", + 'name' => 'Open Case', + 'status' => 'Completed', ], [ - "name" => "Follow up", - "reference_activity" => "Open Case", - "reference_offset" => "3", - "reference_select" => "newest", + 'name' => 'Follow up', + 'reference_activity' => 'Open Case', + 'reference_offset' => 3, + 'reference_select' => 'newest', ], ], ], ], - "timelineActivityTypes" => [ + 'timelineActivityTypes' => [ [ - "name" => "Open Case", - "status" => "Completed", + 'name' => 'Open Case', + 'status' => 'Completed', ], [ - "name" => "Follow up", - "reference_activity" => "Open Case", - "reference_offset" => "3", - "reference_select" => "newest", + 'name' => 'Follow up', + 'reference_activity' => 'Open Case', + 'reference_offset' => 3, + 'reference_select' => 'newest', ], ], - "caseRoles" => [ + 'caseRoles' => [ [ - "name" => "Parent of", - "creator" => "1", - "manager" => "1", + 'name' => 'Parent of', + 'creator' => 1, + 'manager' => 1, ], ], ]; @@ -288,7 +295,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @param array $field * * @return mixed - * @throws \Exception + * @throws \CRM_Core_Exception */ private function getRequiredValue(array $field) { if (!empty($field['options'])) { @@ -332,7 +339,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * * @throws \CRM_Core_Exception */ - private function getFkID(string $fkEntity) { + private function getFkID(string $fkEntity): int { $params = ['checkPermissions' => FALSE]; // Be predictable about what type of contact we select if ($fkEntity === 'Contact') { @@ -351,6 +358,9 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf * @param $dataType * * @return int|null|string + * + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection */ private function getRandomValue($dataType) { switch ($dataType) { @@ -367,7 +377,7 @@ class Api4TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterf return $this->randomLetters(100); case 'Money': - return sprintf('%d.%2d', rand(0, 2000), rand(10, 99)); + return sprintf('%d.%2d', random_int(0, 2000), random_int(10, 99)); case 'Date': return '20100102'; -- 2.25.1