class TestStatusController in Acquia Connector 3.x
Same name and namespace in other branches
- 8.2 src/Controller/TestStatusController.php \Drupal\acquia_connector\Controller\TestStatusController
- 8 src/Controller/TestStatusController.php \Drupal\acquia_connector\Controller\TestStatusController
Class SpiController.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\acquia_connector\Controller\TestStatusController
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\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
46 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TestStatusController:: |
public | function | Determines status of user-contributed tests. | |
TestStatusController:: |
public | function | Validates data from custom test callbacks. |