Skip to content

Commit

Permalink
dev/core#4088 Superficial cleanup on Api4testBase
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 19, 2023
1 parent c33fb61 commit f76dfa8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__ . '/../../../../../../../tests/phpunit/api/v4/Api4TestBase.php';

use Civi\Api4\Afform;
use Civi\Test\CiviEnvBuilder;

/**
* @group headless
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Civi\Api4\SavedSearch;
use Civi\Api4\SearchDisplay;
use Civi\Api4\UFMatch;
use Civi\Test\CiviEnvBuilder;
use Civi\Test\TransactionalInterface;

/**
Expand All @@ -22,7 +23,7 @@
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
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();
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/CRM/Report/Form/ContactSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setUp(): void {
/**
* 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',
Expand Down Expand Up @@ -195,9 +195,9 @@ public function testOddEvenStreetNumber() {
}

/**
* 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',
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/api/v4/Action/AutocompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function on_civi_api_prepare(\Civi\API\Event\PrepareEvent $event) {
}
}

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();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/api/v4/Action/BasicActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;

Expand All @@ -40,7 +41,7 @@ public function on_civi_api4_entityTypes(GenericHookEvent $e): void {
$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();
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/api/v4/Action/ContactIsDeletedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
namespace api\v4\Action;

use api\v4\Api4TestBase;
use Civi\Test\CiviEnvBuilder;
use Civi\Test\TransactionalInterface;

/**
* @group headless
*/
class ContactIsDeletedTest extends Api4TestBase implements TransactionalInterface {

public function setUpHeadless() {
public function setUpHeadless(): CiviEnvBuilder {
$relatedTables = [
'civicrm_address',
'civicrm_email',
Expand Down
92 changes: 51 additions & 41 deletions tests/phpunit/api/v4/Api4TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -47,16 +49,19 @@ public function __construct($name = NULL, array $data = [], $dataName = '') {
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]];
Expand All @@ -78,28 +83,30 @@ public function tearDown(): void {
*
* @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', [
Expand All @@ -124,7 +131,7 @@ public function createLoggedInUser() {
* @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();
}

Expand All @@ -139,7 +146,7 @@ public function createTestRecord(string $entityName, array $values = []) {
* @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' => [],
Expand All @@ -163,7 +170,7 @@ public function saveTestRecords(string $entityName, array $saveParams) {
* @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')));
}

Expand All @@ -176,7 +183,7 @@ public function randomLetters(int $len = 10) {
* @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,
Expand All @@ -185,7 +192,7 @@ public function getRequiredValuesToCreate(string $entity, &$values = []) {
['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]]],
],
],
Expand Down Expand Up @@ -226,51 +233,51 @@ public function getRequiredValuesToCreate(string $entity, &$values = []) {

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,
],
],
];
Expand All @@ -288,7 +295,7 @@ public function getRequiredValuesToCreate(string $entity, &$values = []) {
* @param array $field
*
* @return mixed
* @throws \Exception
* @throws \CRM_Core_Exception
*/
private function getRequiredValue(array $field) {
if (!empty($field['options'])) {
Expand Down Expand Up @@ -332,7 +339,7 @@ private function getRequiredValue(array $field) {
*
* @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') {
Expand All @@ -351,6 +358,9 @@ private function getFkID(string $fkEntity) {
* @param $dataType
*
* @return int|null|string
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
private function getRandomValue($dataType) {
switch ($dataType) {
Expand All @@ -367,7 +377,7 @@ private function getRandomValue($dataType) {
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';
Expand Down

0 comments on commit f76dfa8

Please sign in to comment.