You are here

class TestStatusController in Acquia Connector 8

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

Class SpiController.

Hierarchy

Expanded class hierarchy of TestStatusController

2 files declare their use of TestStatusController
AcquiaConnectorCommands.php in src/Commands/AcquiaConnectorCommands.php
acquia_connector.install in ./acquia_connector.install
Install, update, and uninstall functions for the Acquia Connector module.

File

src/Controller/TestStatusController.php, line 10

Namespace

Drupal\acquia_connector\Controller
View source
class TestStatusController extends ControllerBase {

  /**
   * Determines status of user-contributed tests.
   *
   * Determines the status of all user-contributed tests and logs any failures
   * to a tracking table.
   *
   * @param bool $log
   *   (Optional) If TRUE, log all failures.
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   An associative array containing any tests which failed validation.
   */
  public function testStatus($log = FALSE) {
    $custom_data = [];

    // Iterate through modules which contain hook_acquia_spi_test().
    foreach ($this
      ->moduleHandler()
      ->getImplementations('acquia_connector_spi_test') as $module) {
      $function = $module . '_acquia_connector_spi_test';
      if (function_exists($function)) {
        $result = $this
          ->testValidate($function());
        if (!$result['result']) {
          $custom_data[$module] = $result;
          foreach ($result['failure'] as $test_name => $test_failures) {
            foreach ($test_failures as $test_param => $test_value) {
              $variables = [
                '@module' => $module,
                '@message' => $test_value['message'],
                '@param_name' => $test_param,
                '@test' => $test_name,
                '@value' => $test_value['value'],
              ];

              // Only log if we're performing a full validation check.
              if ($log) {
                $this
                  ->messenger()
                  ->addError($this
                  ->t("Custom test validation failed for @test in @module and has been logged: @message for parameter '@param_name'; current value '@value'.", $variables));
                $this
                  ->getLogger('acquia spi test')
                  ->notice("<em>Custom test validation failed</em>: @message for parameter '@param_name'; current value '@value'. (<em>Test '@test_name' in module '@module_name'</em>)", $variables);
              }
            }
          }
        }
      }
    }

    // If a full validation check is being performed, go to the status page to
    // show the results.
    if ($log) {
      return $this
        ->redirect('system.status');
    }
    return $custom_data;
  }

  /**
   * Validates data from custom test callbacks.
   *
   * @param array $collection
   *   An associative array containing a collection of user-contributed tests.
   *
   * @return array
   *   An associative array containing the validation result of the given tests,
   *   along with any failed parameters.
   */
  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'] : [],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestStatusController::testStatus public function Determines status of user-contributed tests.
TestStatusController::testValidate public function Validates data from custom test callbacks.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.