|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestSuite\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
|
6
|
|
|
use \SimpleSAML\Module\monitor\TestCase as TestCase; |
|
7
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
|
8
|
|
|
use \SimpleSAML\Module\monitor\TestResult as TestResult; |
|
9
|
|
|
|
|
10
|
|
|
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $required; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $available; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dependencies; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $type; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param TestData $testData |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function initialize($testData) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->setRequired($testData->getInputItem('required')); |
|
40
|
|
|
$this->setAvailable($testData->getInputItem('available')); |
|
41
|
|
|
$this->setDependencies($testData->getInputItem('dependencies')); |
|
42
|
|
|
$this->setType($testData->getInputItem('type')); |
|
43
|
|
|
$this->setCategory($this->type.' modules'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array |
|
48
|
|
|
*/ |
|
49
|
|
|
private function setRequired($required) |
|
50
|
|
|
{ |
|
51
|
|
|
assert(is_array($required)); |
|
52
|
|
|
$this->required = $required; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array |
|
58
|
|
|
*/ |
|
59
|
|
|
private function setAvailable($available) |
|
60
|
|
|
{ |
|
61
|
|
|
assert(is_array($available)); |
|
62
|
|
|
$this->available = $available; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array |
|
68
|
|
|
*/ |
|
69
|
|
|
private function setDependencies($dependencies) |
|
70
|
|
|
{ |
|
71
|
|
|
assert(is_array($dependencies)); |
|
72
|
|
|
$this->dependencies = $dependencies; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string |
|
78
|
|
|
*/ |
|
79
|
|
|
private function setType($type) |
|
80
|
|
|
{ |
|
81
|
|
|
assert(is_string($type)); |
|
82
|
|
|
$this->type = $type; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function invokeTest() |
|
90
|
|
|
{ |
|
91
|
|
|
if (empty($this->available)) { |
|
92
|
|
|
$state = State::SKIPPED; |
|
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
foreach ($this->required as $module) { |
|
95
|
|
|
$testData = new TestData([ |
|
96
|
|
|
'required' => $module, |
|
97
|
|
|
'available' => $this->available, |
|
98
|
|
|
'type' => $this->type, |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
$moduleTest = new TestCase\Module($testData); |
|
102
|
|
|
$moduleTestResult = $moduleTest->getTestResult(); |
|
103
|
|
|
if ($moduleTestResult->getState() !== State::OK) { |
|
104
|
|
|
$missing = $this->findMissingDependencies($module); |
|
105
|
|
|
if (!empty($missing)) { |
|
106
|
|
|
$moduleTestResult->setSubject($moduleTest->getModuleName()); |
|
107
|
|
|
$moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing)); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
$this->addTestResult($moduleTestResult); |
|
111
|
|
|
} |
|
112
|
|
|
$state = $this->calculateState(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$testResult = new TestResult($this->type, implode(', ', $this->required)); |
|
116
|
|
|
$this->setTestResult($testResult); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param TestResult $testResult |
|
121
|
|
|
* @param State $state |
|
122
|
|
|
* return void |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function setTestResult($testResult, $state) |
|
125
|
|
|
{ |
|
126
|
|
|
if ($state === State::OK) { |
|
|
|
|
|
|
127
|
|
|
$testResult->setMessage('All required modules are loaded'); |
|
128
|
|
|
} elseif ($state === State::SKIPPED) { |
|
129
|
|
|
$testResult->setMessage('Unable to verify installed modules'); |
|
130
|
|
|
} else { |
|
131
|
|
|
$testResult->setMessage('Not all required modules are loaded'); |
|
132
|
|
|
} |
|
133
|
|
|
$testResult->setState($state); |
|
|
|
|
|
|
134
|
|
|
parent::setTestResult($testResult); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $module |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
private function findMissingDependencies($module) |
|
142
|
|
|
{ |
|
143
|
|
|
$dependencies = $this->dependencies; |
|
144
|
|
|
$missing = array(); |
|
145
|
|
|
while ($dependency = array_search($module, $dependencies)) { |
|
146
|
|
|
if (\SimpleSAML\Module::isModuleEnabled($dependency)) { |
|
147
|
|
|
$missing[] = $dependency; |
|
148
|
|
|
} |
|
149
|
|
|
unset($dependencies[$dependency]); |
|
150
|
|
|
} |
|
151
|
|
|
return $missing; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|