summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/TestCreationParameterProvider.php
blob: ef10cea981f205ad586089bdb2a823ea698d5e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace Civi\Test\Api4\Service;

use Civi\Api4\Service\Spec\FieldSpec;
use Civi\Api4\Service\Spec\SpecGatherer;
use \CRM_Utils_String as StringHelper;

class TestCreationParameterProvider {

  /**
   * @var SpecGatherer
   */
  protected $gatherer;

  /**
   * @param SpecGatherer $gatherer
   */
  public function __construct(SpecGatherer $gatherer) {
    $this->gatherer = $gatherer;
  }

  /**
   * @param $entity
   *
   * @return array
   */
  public function getRequired($entity) {
    $createSpec = $this->gatherer->getSpec($entity, 'create', FALSE);
    $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());

    if ($entity === 'Contact') {
      $requiredFields[] = $createSpec->getFieldByName('first_name');
      $requiredFields[] = $createSpec->getFieldByName('last_name');
    }

    $requiredParams = [];
    foreach ($requiredFields as $requiredField) {
      $value = $this->getRequiredValue($requiredField);
      $requiredParams[$requiredField->getName()] = $value;
    }

    unset($requiredParams['id']);

    return $requiredParams;
  }

  /**
   * Attempt to get a value using field option, defaults, FKEntity, or a random
   * value based on the data type.
   *
   * @param FieldSpec $field
   *
   * @return mixed
   * @throws \Exception
   */
  private function getRequiredValue(FieldSpec $field) {

    if ($field->getOptions()) {
      return $this->getOption($field);
    }
    elseif ($field->getDefaultValue()) {
      return $field->getDefaultValue();
    }
    elseif ($field->getFkEntity()) {
      return $this->getFkID($field);
    }

    $randomValue = $this->getRandomValue($field->getDataType());

    if ($randomValue) {
      return $randomValue;
    }

    throw new \Exception('Could not provide default value');
  }

  /**
   * @param FieldSpec $field
   *
   * @return mixed
   */
  private function getOption(FieldSpec $field) {
    $options = $field->getOptions();
    $useKeyNames = ['data_type', 'html_type'];
    $shouldUseKey = in_array($field->getName(), $useKeyNames);
    $isIdField = substr($field->getName(), -3) === '_id';

    if ($isIdField || $shouldUseKey) {
      return array_rand($options); // return key (ID)
    }
    else {
      return $options[array_rand($options)];
    }
  }

  /**
   * @param FieldSpec $field
   *
   * @return mixed
   * @throws \Exception
   */
  private function getFkID(FieldSpec $field) {
    $fkEntity = $field->getFkEntity();
    $params = ['checkPermissions' => FALSE];
    // Be predictable about what type of contact we select
    if ($fkEntity === 'Contact') {
      $params['where'] = [['contact_type', '=', 'Individual']];
    }
    $entityList = civicrm_api4($fkEntity, 'get', $params);
    if ($entityList->count() < 1) {
      $msg = sprintf('At least one %s is required in test', $fkEntity);
      throw new \Exception($msg);
    }

    return $entityList->last()['id'];
  }

  /**
   * @param $dataType
   *
   * @return int|null|string
   */
  private function getRandomValue($dataType) {
    switch ($dataType) {
      case 'Boolean':
        return TRUE;

      case 'Integer':
        return rand(1, 2000);

      case 'String':
        return StringHelper::createRandom(10, implode('', range('a', 'z')));

      case 'Text':
        return StringHelper::createRandom(100, implode('', range('a', 'z')));

      case 'Money':
        return sprintf('%d.%2d', rand(0, 2000), rand(1, 99));

      case 'Date':
        return '20100102';
    }

    return NULL;
  }

}