-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa.module
288 lines (270 loc) · 8.1 KB
/
qa.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* @file
* OSInet Quality Assurance module for Drupal.
*
* @copyright Copyright (C) 2005-2020 Frederic G. MARAND for Ouest Systèmes Informatiques (OSInet)
*
* @since DRUPAL-4-6
*
* @license GPL-2.0-or-later
*
* License note: OSInet support services only apply to the module when
* distributed by OSInet, not by any third-party further down the distribution
* chain enabled by GPL.
*
* If you obtained QA from another source, that source received it under the
* GPL, and can therefore distribute it under the GPL, and so can you and just
* anyone down the chain as long as the GPL terms are abided by, the module
* distributor in that case being that source, not OSInet.
*/
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\qa\BaseControl;
use Drupal\qa\BasePackage;
use Drupal\qa\Exportable;
use Drupal\qa\Variable;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Page callback for qa/dependencies.
*
* TODO convert to native Image_GraphViz to stop depending on graphviz_filter.
* XXX convert to Grafizzi to remove dependency on Image_GraphViz.
*
* @return string
* A DOT digraph file.
*/
function qa_page_dependencies() {
/** @var \Drupal\qa\Dependencies $qaDep */
$qaDep = Drupal::service('qa.dependencies');
$graph = $qaDep->build();
// Passed by reference: cannot pass a function return.
return graphviz_filter_render($graph);
}
/**
* Batch conclusion callback.
*
* @param bool $success
* Did the batch succeed ?
* @param array $results
* The batch steps results.
* @param array $operations
* The operations performed during the batch.
*/
function qa_report_finished(bool $success, array $results, array $operations) {
unset($results['#message']);
if ($success) {
$message = Drupal::translation()
->formatPlural(count($results), 'One control pass ran.',
'@count control passes ran.');
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
$_SESSION['qa_results'] = $results;
return new RedirectResponse(Url::fromRoute('qa.reports.results'));
}
/**
* Results page for QA Controls batch.
*
* @link http://www.php.net/manual/fr/function.unserialize.php @endlink
*/
function qa_report_results() {
if (empty($_SESSION['qa_results'])) {
return new RedirectResponse(Url::fromRoute('qa.reports'));
}
// Work around incomplete classes.
$results = unserialize(serialize($_SESSION['qa_results']));
$header = [
t('Control'),
t('Status'),
t('Results'),
];
$data = [];
$r = Drupal::service('renderer');
foreach ($results as $pass) {
$control = $pass->control;
$data[] = [
$control->title,
$pass->status
? $r->render([
'#theme' => 'image',
'#path' => 'misc/watchdog-ok.png',
'#alt' => t('OK'),
])
: $r->render([
'#theme' => 'image',
'#path' => 'misc/watchdog-error.png',
'#alt' => t('Error'),
]),
$pass->result,
];
}
$ret = $r->render([
'#theme' => 'table',
'#header' => $header,
'#rows' => $data,
'#attributes' => [
'id' => 'qa-results',
],
'#attached' => ['library' => ['qa/results']],
]);
// unset($_SESSION['qa_results']).
return $ret;
}
/**
* Form builder for QA packages/controls selection form.
*
* @return array
* A form array.
*/
function qa_report_form($form, $form_state) {
$form = [];
/** @var \Drupal\Core\Extension\ExtensionPathResolver $epr */
$epr = \Drupal::service('extension.path.resolver');
$base = $epr->getPath('module', 'qa');
$packages = Exportable::getClasses($base, BasePackage::class);
ksort($packages);
foreach ($packages as $package_name => $package) {
$collapsed = TRUE;
$form[$package_name] = [
'#type' => 'fieldset',
'#title' => Xss::filterAdmin($package->title),
'#description' => Xss::filterAdmin($package->description),
'#collapsible' => TRUE,
];
$controls = $package->getClasses($package->dir, BaseControl::class);
foreach ($controls as $control_name => $control) {
$default_value = isset($_SESSION[$control_name])
? $_SESSION[$control_name]
: NULL;
if ($default_value) {
$collapsed = FALSE;
}
$deps = [];
$met = TRUE;
foreach ($control->getDependencies() as $dep_name) {
if (Drupal::moduleHandler()->moduleExists($dep_name)) {
$deps[] = t('@module (<span class="admin-enabled">available</span>)',
['@module' => $dep_name]);
}
else {
$deps[] = t('@module (<span class="admin-disabled">unavailable</span>)',
['@module' => $dep_name]);
$met = FALSE;
}
}
$form[$package_name][$control_name] = [
'#type' => 'checkbox',
'#default_value' => $met ? $default_value : 0,
'#title' => Xss::filterAdmin($control->title),
'#description' => Xss::filterAdmin($control->description),
'#disabled' => !$met,
];
$form[$package_name][$control_name . '-dependencies'] = [
'#value' => t('Depends on: !dependencies', [
'!dependencies' => implode(', ', $deps),
]),
'#prefix' => '<div class="admin-dependencies">',
'#suffix' => '</div>',
];
}
$form[$package_name]['#collapsed'] = $collapsed;
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Run controls'),
];
return $form;
}
/**
* Submit handler for QA packages/controls selection form.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function qa_report_form_submit(array $form, FormStateInterface $form_state) {
$controls = [];
foreach ($form_state['values'] as $item => $value) {
if (class_exists($item) && is_subclass_of($item,
'\Drupal\qa\Plugin\Qa\Control\BaseControl')) {
if ($value) {
$controls[$item] = $value;
}
$_SESSION[$item] = $value;
}
elseif ($value == 1) {
$args = [
'%control' => $item,
];
drupal_set_message(t('Requested invalid control %control', $args),
'error');
\Drupal::logger('qa')->error('Requested invalid control %control', $args);
}
}
drupal_set_message(t('Prepare to run these controls: @controls', [
'@controls' => implode(', ', array_keys($controls)),
]), 'status');
$batch = [
'operations' => [],
'title' => t('QA Controls running'),
'init_message' => t('QA Controls initializing'),
'progress_message' => t('current: @current, Remaining: @remaining, Total: @total'),
'error_message' => t('Error in QA Control'),
'finished' => 'qa_report_finished',
// 'file' => '', // only if outside module file.
];
foreach ($controls as $item => $value) {
$batch['operations'][] = ['qa_report_run_pass', [$item]];
}
batch_set($batch);
}
/**
* Batch progress step.
*/
function qa_report_run_pass($class_name, &$context): void {
$name_arg = ['@class' => $class_name];
$control = new $class_name();
if (!is_object($control)) {
drupal_set_message(t('Cannot obtain an instance for @class', $name_arg),
'error');
$context['results']['#message'] = t('Control @class failed to run.',
$name_arg);
$context['message'] = t('Control @class failed to run.', $name_arg);
$context['results'][$class_name] = 'wow';
}
else {
drupal_set_message(t('Running a control instance for @class', $name_arg),
'status');
$pass = $control->run();
if (!$pass->status) {
$context['success'] = FALSE;
}
$context['results']['#message'][] = t('Control @class ran', $name_arg);
$context['message'] = \Drupal::service('renderer')->render([
'#theme' => 'item_list',
'#items' => $context['results']['#message'],
]);
$context['results'][$class_name] = $pass;
}
}
/**
* Load variable by name.
*
* @param string $name
* The name of the variable.
*
* @return \Drupal\qa\Variable|false
* The variable if it was found.
*/
function qa_variable_load($name) {
$variable = new Variable($name);
if (!$variable->is_set) {
return FALSE;
}
return $variable;
}