Merge pull request #16028 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / api / v4 / Service / TestCreationParameterProvider.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
7d61e75f 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
7d61e75f
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace api\v4\Service;
23
24use Civi\Api4\Service\Spec\FieldSpec;
25use Civi\Api4\Service\Spec\SpecGatherer;
26
27class TestCreationParameterProvider {
28
29 /**
30 * @var \Civi\Api4\Service\Spec\SpecGatherer
31 */
32 protected $gatherer;
33
34 /**
35 * @param \Civi\Api4\Service\Spec\SpecGatherer $gatherer
36 */
37 public function __construct(SpecGatherer $gatherer) {
38 $this->gatherer = $gatherer;
39 }
40
41 /**
42 * @param $entity
43 *
44 * @return array
45 */
46 public function getRequired($entity) {
47 $createSpec = $this->gatherer->getSpec($entity, 'create', FALSE);
48 $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());
49
50 if ($entity === 'Contact') {
51 $requiredFields[] = $createSpec->getFieldByName('first_name');
52 $requiredFields[] = $createSpec->getFieldByName('last_name');
53 }
54
55 $requiredParams = [];
56 foreach ($requiredFields as $requiredField) {
57 $value = $this->getRequiredValue($requiredField);
58 $requiredParams[$requiredField->getName()] = $value;
59 }
60
61 unset($requiredParams['id']);
62
63 return $requiredParams;
64 }
65
66 /**
67 * Attempt to get a value using field option, defaults, FKEntity, or a random
68 * value based on the data type.
69 *
70 * @param \Civi\Api4\Service\Spec\FieldSpec $field
71 *
72 * @return mixed
73 * @throws \Exception
74 */
75 private function getRequiredValue(FieldSpec $field) {
76
77 if ($field->getOptions()) {
78 return $this->getOption($field);
79 }
80 elseif ($field->getDefaultValue()) {
81 return $field->getDefaultValue();
82 }
83 elseif ($field->getFkEntity()) {
84 return $this->getFkID($field, $field->getFkEntity());
85 }
86 elseif (in_array($field->getName(), ['entity_id', 'contact_id'])) {
87 return $this->getFkID($field, 'Contact');
88 }
89
90 $randomValue = $this->getRandomValue($field->getDataType());
91
92 if ($randomValue) {
93 return $randomValue;
94 }
95
96 throw new \Exception('Could not provide default value');
97 }
98
99 /**
100 * @param \Civi\Api4\Service\Spec\FieldSpec $field
101 *
102 * @return mixed
103 */
104 private function getOption(FieldSpec $field) {
105 $options = $field->getOptions();
106 return array_rand($options);
107 }
108
109 /**
110 * @param \Civi\Api4\Service\Spec\FieldSpec $field
111 * @param string $fkEntity
112 *
113 * @return mixed
114 * @throws \Exception
115 */
116 private function getFkID(FieldSpec $field, $fkEntity) {
117 $params = ['checkPermissions' => FALSE];
118 // Be predictable about what type of contact we select
119 if ($fkEntity === 'Contact') {
120 $params['where'] = [['contact_type', '=', 'Individual']];
121 }
122 $entityList = civicrm_api4($fkEntity, 'get', $params);
123 if ($entityList->count() < 1) {
124 $msg = sprintf('At least one %s is required in test', $fkEntity);
125 throw new \Exception($msg);
126 }
127
128 return $entityList->last()['id'];
129 }
130
131 /**
132 * @param $dataType
133 *
134 * @return int|null|string
135 */
136 private function getRandomValue($dataType) {
137 switch ($dataType) {
138 case 'Boolean':
139 return TRUE;
140
141 case 'Integer':
142 return rand(1, 2000);
143
144 case 'String':
145 return \CRM_Utils_String::createRandom(10, implode('', range('a', 'z')));
146
147 case 'Text':
148 return \CRM_Utils_String::createRandom(100, implode('', range('a', 'z')));
149
150 case 'Money':
151 return sprintf('%d.%2d', rand(0, 2000), rand(10, 99));
152
153 case 'Date':
154 return '20100102';
155
156 case 'Timestamp':
157 return 'now';
158 }
159
160 return NULL;
161 }
162
163}