4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace api\v
4\Service
;
40 use Civi\Api4\Service\Spec\FieldSpec
;
41 use Civi\Api4\Service\Spec\SpecGatherer
;
43 class TestCreationParameterProvider
{
46 * @var \Civi\Api4\Service\Spec\SpecGatherer
51 * @param \Civi\Api4\Service\Spec\SpecGatherer $gatherer
53 public function __construct(SpecGatherer
$gatherer) {
54 $this->gatherer
= $gatherer;
62 public function getRequired($entity) {
63 $createSpec = $this->gatherer
->getSpec($entity, 'create', FALSE);
64 $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());
66 if ($entity === 'Contact') {
67 $requiredFields[] = $createSpec->getFieldByName('first_name');
68 $requiredFields[] = $createSpec->getFieldByName('last_name');
72 foreach ($requiredFields as $requiredField) {
73 $value = $this->getRequiredValue($requiredField);
74 $requiredParams[$requiredField->getName()] = $value;
77 unset($requiredParams['id']);
79 return $requiredParams;
83 * Attempt to get a value using field option, defaults, FKEntity, or a random
84 * value based on the data type.
86 * @param \Civi\Api4\Service\Spec\FieldSpec $field
91 private function getRequiredValue(FieldSpec
$field) {
93 if ($field->getOptions()) {
94 return $this->getOption($field);
96 elseif ($field->getDefaultValue()) {
97 return $field->getDefaultValue();
99 elseif ($field->getFkEntity()) {
100 return $this->getFkID($field, $field->getFkEntity());
102 elseif (in_array($field->getName(), ['entity_id', 'contact_id'])) {
103 return $this->getFkID($field, 'Contact');
106 $randomValue = $this->getRandomValue($field->getDataType());
112 throw new \
Exception('Could not provide default value');
116 * @param \Civi\Api4\Service\Spec\FieldSpec $field
120 private function getOption(FieldSpec
$field) {
121 $options = $field->getOptions();
122 return array_rand($options);
126 * @param \Civi\Api4\Service\Spec\FieldSpec $field
127 * @param string $fkEntity
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']];
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);
144 return $entityList->last()['id'];
150 * @return int|null|string
152 private function getRandomValue($dataType) {
158 return rand(1, 2000);
161 return \CRM_Utils_String
::createRandom(10, implode('', range('a', 'z')));
164 return \CRM_Utils_String
::createRandom(100, implode('', range('a', 'z')));
167 return sprintf('%d.%2d', rand(0, 2000), rand(10, 99));