You are here

protected function WebformSubmissionGenerate::getTestValues in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionGenerate.php \Drupal\webform\WebformSubmissionGenerate::getTestValues()

Get test values from a webform element.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

string $name: The name of the element.

array $element: The FAPI element.

array $options: (options) Options for generated value.

Return value

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

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

File

src/WebformSubmissionGenerate.php, line 176

Class

WebformSubmissionGenerate
Webform submission generator.

Namespace

Drupal\webform

Code

protected function getTestValues(WebformInterface $webform, $name, array $element, array $options = []) {

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

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

  // Get test values from options.
  if (isset($element['#options'])) {
    return array_keys(OptGroup::flattenOptions($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 #unique value.
  if (!empty($element['#unique'])) {
    return uniqid();
  }

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