You are here

public function TestStatusController::testValidate in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 src/Controller/TestStatusController.php \Drupal\acquia_connector\Controller\TestStatusController::testValidate()
  2. 3.x src/Controller/TestStatusController.php \Drupal\acquia_connector\Controller\TestStatusController::testValidate()

Validates data from custom test callbacks.

Parameters

array $collection: An associative array containing a collection of user-contributed tests.

Return value

array An associative array containing the validation result of the given tests, along with any failed parameters.

1 call to TestStatusController::testValidate()
TestStatusController::testStatus in src/Controller/TestStatusController.php
Determines status of user-contributed tests.

File

src/Controller/TestStatusController.php, line 75

Class

TestStatusController
Class SpiController.

Namespace

Drupal\acquia_connector\Controller

Code

public function testValidate(array $collection) {
  $result = TRUE;
  $check_result_value = [];

  // Load valid categories and severities.
  $categories = [
    'performance',
    'security',
    'best_practices',
  ];
  $severities = [
    0,
    1,
    2,
    4,
    8,
    16,
    32,
    64,
    128,
  ];
  foreach ($collection as $machine_name => $tests) {
    foreach ($tests as $check_name => $check_value) {
      $fail_value = '';
      $message = '';
      $check_name = strtolower($check_name);
      $check_value = is_string($check_value) ? strtolower($check_value) : $check_value;

      // Validate the data inputs for each check.
      switch ($check_name) {
        case 'category':
          if (!is_string($check_value) || !in_array($check_value, $categories)) {
            $type = gettype($check_value);
            $fail_value = "{$check_value} ({$type})";
            $message = 'Value must be a string and one of ' . implode(', ', $categories);
          }
          break;
        case 'solved':
          if (!is_bool($check_value)) {
            $type = gettype($check_value);
            $fail_value = "{$check_value} ({$type})";
            $message = 'Value must be a boolean';
          }
          break;
        case 'severity':
          if (!is_int($check_value) || !in_array($check_value, $severities)) {
            $type = gettype($check_value);
            $fail_value = "{$check_value} ({$type})";
            $message = 'Value must be an integer and set to one of ' . implode(', ', $severities);
          }
          break;
        default:
          if (!is_string($check_value) || strlen($check_value) > 1024) {
            $type = gettype($check_value);
            $fail_value = "{$check_value} ({$type})";
            $message = 'Value must be a string and no more than 1024 characters';
          }
          break;
      }
      if (!empty($fail_value) && !empty($message)) {
        $check_result_value['failed'][$machine_name][$check_name]['value'] = $fail_value;
        $check_result_value['failed'][$machine_name][$check_name]['message'] = $message;
      }
    }
  }

  // If there were any failures, the test has failed. Into exile it must go.
  if (!empty($check_result_value)) {
    $result = FALSE;
  }
  return [
    'result' => $result,
    'failure' => isset($check_result_value['failed']) ? $check_result_value['failed'] : [],
  ];
}