Merge pull request #15321 from yashodha/dev_1065
[civicrm-core.git] / tests / phpunit / api / v4 / Service / TestCreationParameterProvider.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Service;
39
40 use Civi\Api4\Service\Spec\FieldSpec;
41 use Civi\Api4\Service\Spec\SpecGatherer;
42
43 class TestCreationParameterProvider {
44
45 /**
46 * @var \Civi\Api4\Service\Spec\SpecGatherer
47 */
48 protected $gatherer;
49
50 /**
51 * @param \Civi\Api4\Service\Spec\SpecGatherer $gatherer
52 */
53 public function __construct(SpecGatherer $gatherer) {
54 $this->gatherer = $gatherer;
55 }
56
57 /**
58 * @param $entity
59 *
60 * @return array
61 */
62 public function getRequired($entity) {
63 $createSpec = $this->gatherer->getSpec($entity, 'create', FALSE);
64 $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());
65
66 if ($entity === 'Contact') {
67 $requiredFields[] = $createSpec->getFieldByName('first_name');
68 $requiredFields[] = $createSpec->getFieldByName('last_name');
69 }
70
71 $requiredParams = [];
72 foreach ($requiredFields as $requiredField) {
73 $value = $this->getRequiredValue($requiredField);
74 $requiredParams[$requiredField->getName()] = $value;
75 }
76
77 unset($requiredParams['id']);
78
79 return $requiredParams;
80 }
81
82 /**
83 * Attempt to get a value using field option, defaults, FKEntity, or a random
84 * value based on the data type.
85 *
86 * @param \Civi\Api4\Service\Spec\FieldSpec $field
87 *
88 * @return mixed
89 * @throws \Exception
90 */
91 private function getRequiredValue(FieldSpec $field) {
92
93 if ($field->getOptions()) {
94 return $this->getOption($field);
95 }
96 elseif ($field->getDefaultValue()) {
97 return $field->getDefaultValue();
98 }
99 elseif ($field->getFkEntity()) {
100 return $this->getFkID($field, $field->getFkEntity());
101 }
102 elseif (in_array($field->getName(), ['entity_id', 'contact_id'])) {
103 return $this->getFkID($field, 'Contact');
104 }
105
106 $randomValue = $this->getRandomValue($field->getDataType());
107
108 if ($randomValue) {
109 return $randomValue;
110 }
111
112 throw new \Exception('Could not provide default value');
113 }
114
115 /**
116 * @param \Civi\Api4\Service\Spec\FieldSpec $field
117 *
118 * @return mixed
119 */
120 private function getOption(FieldSpec $field) {
121 $options = $field->getOptions();
122 return array_rand($options);
123 }
124
125 /**
126 * @param \Civi\Api4\Service\Spec\FieldSpec $field
127 * @param string $fkEntity
128 *
129 * @return mixed
130 * @throws \Exception
131 */
132 private function getFkID(FieldSpec $field, $fkEntity) {
133 $params = ['checkPermissions' => FALSE];
134 // Be predictable about what type of contact we select
135 if ($fkEntity === 'Contact') {
136 $params['where'] = [['contact_type', '=', 'Individual']];
137 }
138 $entityList = civicrm_api4($fkEntity, 'get', $params);
139 if ($entityList->count() < 1) {
140 $msg = sprintf('At least one %s is required in test', $fkEntity);
141 throw new \Exception($msg);
142 }
143
144 return $entityList->last()['id'];
145 }
146
147 /**
148 * @param $dataType
149 *
150 * @return int|null|string
151 */
152 private function getRandomValue($dataType) {
153 switch ($dataType) {
154 case 'Boolean':
155 return TRUE;
156
157 case 'Integer':
158 return rand(1, 2000);
159
160 case 'String':
161 return \CRM_Utils_String::createRandom(10, implode('', range('a', 'z')));
162
163 case 'Text':
164 return \CRM_Utils_String::createRandom(100, implode('', range('a', 'z')));
165
166 case 'Money':
167 return sprintf('%d.%2d', rand(0, 2000), rand(10, 99));
168
169 case 'Date':
170 return '20100102';
171
172 case 'Timestamp':
173 return 'now';
174 }
175
176 return NULL;
177 }
178
179 }