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;
/**
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]];
*
* @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', [
* @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();
}
* @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' => [],
* @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')));
}
* @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,
['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]]],
],
],
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,
],
],
];
* @param array $field
*
* @return mixed
- * @throws \Exception
+ * @throws \CRM_Core_Exception
*/
private function getRequiredValue(array $field) {
if (!empty($field['options'])) {
*
* @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') {
* @param $dataType
*
* @return int|null|string
+ *
+ * @noinspection PhpUnhandledExceptionInspection
+ * @noinspection PhpDocMissingThrowsInspection
*/
private function getRandomValue($dataType) {
switch ($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';