Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
51.52% |
34 / 66 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryContainerService | |
51.52% |
34 / 66 |
|
25.00% |
2 / 8 |
71.26 | |
0.00% |
0 / 1 |
| getProviders | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| getPlugins | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
7.02 | |||
| getBootstrap | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTestDefinition | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getTestCompilation | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getActiveFeatures | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getPluginsDisabledForDelivery | |
75.00% |
15 / 20 |
|
0.00% |
0 / 1 |
5.39 | |||
| getAllAvailableFeatures | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoDeliveryRdf\model; |
| 22 | |
| 23 | use common_exception_Error; |
| 24 | use common_ext_ExtensionsManager as ExtensionsManager; |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\taoDelivery\model\AssignmentService; |
| 29 | use oat\taoDelivery\model\DeliveryContainerService as DeliveryContainerServiceInterface; |
| 30 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 31 | use oat\taoTests\models\runner\plugins\TestPlugin; |
| 32 | use oat\taoTests\models\runner\plugins\TestPluginService; |
| 33 | use oat\taoTests\models\runner\features\TestRunnerFeatureService; |
| 34 | use oat\taoTests\models\runner\providers\TestProviderService; |
| 35 | use oat\generis\model\OntologyAwareTrait; |
| 36 | |
| 37 | /** |
| 38 | * RDF implementation for the Delivery container service. |
| 39 | * It means the container data are retrieved into the ontology. |
| 40 | * |
| 41 | * TODO The actual implementation still uses serviceCall for the test definition and the test compilation |
| 42 | * and the config for the bootstrap. All those infos should be added during the assemble phase. |
| 43 | * |
| 44 | * @author Bertrand Chevier <bertrand@taotesting.com> |
| 45 | */ |
| 46 | class DeliveryContainerService extends ConfigurableService implements DeliveryContainerServiceInterface |
| 47 | { |
| 48 | use OntologyAwareTrait; |
| 49 | |
| 50 | public const PROPERTY_EXCLUDED_SUBJECTS = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#ExcludedSubjects'; |
| 51 | public const PROPERTY_MAX_EXEC = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#Maxexec'; |
| 52 | public const PROPERTY_ACCESS_SETTINGS = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AccessSettings'; |
| 53 | // phpcs:disable Generic.Files.LineLength |
| 54 | public const TEST_RUNNER_FEATURES_PROPERTY = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#DeliveryTestRunnerFeatures'; |
| 55 | // phpcs:enable Generic.Files.LineLength |
| 56 | |
| 57 | /** @deprecated use DeliveryAssemblyService::PROPERTY_START */ |
| 58 | public const PROPERTY_START = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#PeriodStart'; |
| 59 | /** @deprecated use DeliveryAssemblyService::PROPERTY_END */ |
| 60 | public const PROPERTY_END = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#PeriodEnd'; |
| 61 | |
| 62 | /** |
| 63 | * @var TestPlugin[] |
| 64 | */ |
| 65 | private $deliveryPlugins = []; |
| 66 | |
| 67 | /** |
| 68 | * Get the list of providers for the current execution |
| 69 | * @param DeliveryExecution $execution |
| 70 | * @return array the list of providers |
| 71 | */ |
| 72 | public function getProviders(DeliveryExecution $execution) |
| 73 | { |
| 74 | $serviceManager = $this->getServiceManager(); |
| 75 | $providerService = $serviceManager->get(TestProviderService::SERVICE_ID); |
| 76 | $activeProviders = array_filter( |
| 77 | $providerService->getAllProviders(), |
| 78 | function ($provider) { |
| 79 | return !is_null($provider) && $provider->isActive(); |
| 80 | } |
| 81 | ); |
| 82 | $providers = []; |
| 83 | foreach ($activeProviders as $provider) { |
| 84 | $category = $provider->getCategory(); |
| 85 | if (!isset($providers[$category])) { |
| 86 | $providers[$category] = []; |
| 87 | } |
| 88 | $providers[$category][] = $provider; |
| 89 | } |
| 90 | $providers['plugins'] = array_values($this->getPlugins($execution)); |
| 91 | return $providers; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the list of active plugins for the current execution |
| 96 | * @param DeliveryExecution $deliveryExecution |
| 97 | * @return TestPlugin[] the list of plugins |
| 98 | * |
| 99 | * @throws \common_exception_NotFound |
| 100 | */ |
| 101 | public function getPlugins(DeliveryExecution $deliveryExecution) |
| 102 | { |
| 103 | $delivery = $deliveryExecution->getDelivery(); |
| 104 | $deliveryUri = $delivery->getUri(); |
| 105 | if (!isset($this->deliveryPlugins[$deliveryUri])) { |
| 106 | $this->deliveryPlugins[$deliveryUri] = []; |
| 107 | $pluginService = $this->getServiceLocator()->get(TestPluginService::SERVICE_ID); |
| 108 | $allPlugins = $pluginService->getAllPlugins(); |
| 109 | |
| 110 | $pluginsToDisable = $this->getPluginsDisabledForDelivery($delivery); |
| 111 | foreach ($allPlugins as $key => $plugin) { |
| 112 | if (empty($plugin) || !$plugin instanceof TestPlugin) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if ($plugin->isActive() && !in_array($plugin->getId(), $pluginsToDisable, true)) { |
| 117 | $this->deliveryPlugins[$deliveryUri][$key] = $plugin; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $this->deliveryPlugins[$deliveryUri]; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the container bootstrap |
| 127 | * @param DeliveryExecution $deliveryExecution |
| 128 | * @return string the bootstrap |
| 129 | */ |
| 130 | public function getBootstrap(DeliveryExecution $deliveryExecution) |
| 131 | { |
| 132 | //FIXME this config is misplaced, this should be a delivery property |
| 133 | $config = ExtensionsManager::singleton()->getExtensionById('taoQtiTest')->getConfig('testRunner'); |
| 134 | return $config['bootstrap']; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the container testDefinition |
| 139 | * @param DeliveryExecution $deliveryExecution |
| 140 | * @return string the testDefinition |
| 141 | */ |
| 142 | public function getTestDefinition(DeliveryExecution $deliveryExecution) |
| 143 | { |
| 144 | //FIXME this shouldn't be a service call anymore, a delivery property instead |
| 145 | $delivery = $deliveryExecution->getDelivery(); |
| 146 | $runtime = ServiceManager::getServiceManager()->get(AssignmentService::SERVICE_ID)->getRuntime($delivery); |
| 147 | $inputParameters = \tao_models_classes_service_ServiceCallHelper::getInputValues($runtime, []); |
| 148 | |
| 149 | return $inputParameters['QtiTestDefinition']; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the container test compilation |
| 154 | * @param DeliveryExecution $deliveryExecution |
| 155 | * @return string the testCompilation |
| 156 | */ |
| 157 | public function getTestCompilation(DeliveryExecution $deliveryExecution) |
| 158 | { |
| 159 | |
| 160 | //FIXME this shouldn't be a service call anymore, a delivery property instead |
| 161 | $delivery = $deliveryExecution->getDelivery(); |
| 162 | $runtime = ServiceManager::getServiceManager()->get(AssignmentService::SERVICE_ID)->getRuntime($delivery); |
| 163 | $inputParameters = \tao_models_classes_service_ServiceCallHelper::getInputValues($runtime, []); |
| 164 | |
| 165 | return $inputParameters['QtiTestCompilation']; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param \core_kernel_classes_Resource $delivery |
| 170 | * @return array of feature ids |
| 171 | * @throws \core_kernel_persistence_Exception |
| 172 | */ |
| 173 | protected function getActiveFeatures(\core_kernel_classes_Resource $delivery) |
| 174 | { |
| 175 | return explode( |
| 176 | ',', |
| 177 | $delivery->getOnePropertyValue($this->getProperty(self::TEST_RUNNER_FEATURES_PROPERTY)) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param core_kernel_classes_Resource $delivery |
| 183 | * @return array |
| 184 | */ |
| 185 | protected function getPluginsDisabledForDelivery(core_kernel_classes_Resource $delivery) |
| 186 | { |
| 187 | $disabledDeliveryPlugins = []; |
| 188 | try { |
| 189 | $allTestRunnerFeatures = $this->getAllAvailableFeatures(); |
| 190 | |
| 191 | if (empty($allTestRunnerFeatures)) { |
| 192 | return $disabledDeliveryPlugins; |
| 193 | } |
| 194 | |
| 195 | $enabledFeaturesPlugins = [[]]; |
| 196 | $disabledFeaturesPlugins = [[]]; |
| 197 | $activeTestRunnerFeaturesIds = $this->getActiveFeatures($delivery); |
| 198 | foreach ($allTestRunnerFeatures as $feature) { |
| 199 | if (in_array($feature->getId(), $activeTestRunnerFeaturesIds, true)) { |
| 200 | $enabledFeaturesPlugins[] = $feature->getPluginsIds(); |
| 201 | } else { |
| 202 | $disabledFeaturesPlugins[] = $feature->getPluginsIds(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | $enabledPlugins = array_unique(array_merge(...$enabledFeaturesPlugins)); |
| 207 | $disabledPlugins = array_unique(array_merge(...$disabledFeaturesPlugins)); |
| 208 | |
| 209 | // We disable only plugins which are not enabled via any other active test runner feature. |
| 210 | $disabledDeliveryPlugins = array_diff($disabledPlugins, $enabledPlugins); |
| 211 | } catch (common_exception_Error $e) { |
| 212 | $this->logWarning( |
| 213 | 'Error getting plugins disabled for delivery.', |
| 214 | ['message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()] |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | return $disabledDeliveryPlugins; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @return array |
| 223 | */ |
| 224 | protected function getAllAvailableFeatures() |
| 225 | { |
| 226 | $testRunnerFeatureService = $this->getServiceLocator()->get(TestRunnerFeatureService::SERVICE_ID); |
| 227 | $allTestRunnerFeatures = $testRunnerFeatureService->getAll(); |
| 228 | |
| 229 | return $allTestRunnerFeatures; |
| 230 | } |
| 231 | } |