View source
<?php
namespace Drupal\acquia_connector\Controller;
use Drupal\Core\Controller\ControllerBase;
class TestStatusController extends ControllerBase {
public function testStatus($log = FALSE) {
$custom_data = [];
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'],
];
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 ($log) {
return $this
->redirect('system.status');
}
return $custom_data;
}
public function testValidate(array $collection) {
$result = TRUE;
$check_result_value = [];
$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;
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 (!empty($check_result_value)) {
$result = FALSE;
}
return [
'result' => $result,
'failure' => isset($check_result_value['failed']) ? $check_result_value['failed'] : [],
];
}
}