You are here

protected function YamlFormSubmissionGenerate::getTestValues in YAML Form 8

Get test values from a form element.

Parameters

\Drupal\yamlform\YamlFormInterface $yamlform: A form.

string $name: The name of the element.

array $element: The FAPI element.

Return value

array|int|null An array containing multiple test values or a single test value.

1 call to YamlFormSubmissionGenerate::getTestValues()
YamlFormSubmissionGenerate::getTestValue in src/YamlFormSubmissionGenerate.php
Get test value for a form element.

File

src/YamlFormSubmissionGenerate.php, line 146

Class

YamlFormSubmissionGenerate
Form submission generator.

Namespace

Drupal\yamlform

Code

protected function getTestValues(YamlFormInterface $yamlform, $name, array $element) {

  // Get test value from the actual element.
  if (isset($element['#test'])) {
    return $element['#test'];
  }

  // Never populate hidden and value elements.
  if (in_array($element['#type'], [
    'hidden',
    'value',
  ])) {
    return NULL;
  }

  // Invoke YamlFormElement::test and get a test value.
  // If test value is NULL this element should be populated with test data.
  // @see \Drupal\yamlform\Plugin\YamlFormElement\ContainerBase::getTestValue().
  $test_value = $this->elementManager
    ->invokeMethod('getTestValue', $element, $yamlform);
  if ($test_value) {
    return $test_value;
  }
  elseif ($test_value === NULL) {
    return NULL;
  }

  // Get test values from options.
  if (isset($element['#options'])) {
    return array_keys($element['#options']);
  }

  // Get test values using #type.
  if (isset($this->types[$element['#type']])) {
    return $this->types[$element['#type']];
  }

  // Get test values using on exact name matches.
  if (isset($this->types[$name])) {
    return $this->types[$name];
  }

  // Get test values using partial name matches.
  foreach ($this->names as $key => $values) {
    if (preg_match('/(^|_)' . $key . '(_|$)/i', $name)) {
      return $values;
    }
  }

  // Get test value using #type.
  switch ($element['#type']) {
    case 'range':
    case 'number':
      $element += [
        '#min' => 1,
        '#max' => 10,
      ];
      return rand($element['#min'], $element['#max']);
  }

  // Get test #unique value.
  if (!empty($element['#unique'])) {
    return uniqid();
  }

  // Return default values.
  return isset($this->names['default']) ? $this->names['default'] : NULL;
}