-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from oat-sa/feat/RFE-530/integration-branch
feat: adding copy tests logic
- Loading branch information
Showing
8 changed files
with
555 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2023 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\taoTests\models\Copier; | ||
|
||
use oat\generis\model\data\Ontology; | ||
use oat\generis\model\OntologyRdf; | ||
use oat\generis\model\OntologyRdfs; | ||
use oat\tao\model\resources\Service\InstanceCopierProxy; | ||
use oat\tao\model\TaoOntology; | ||
use oat\oatbox\event\EventManager; | ||
use oat\tao\model\resources\Service\ClassCopier; | ||
use oat\tao\model\resources\Service\InstanceCopier; | ||
use oat\tao\model\resources\Service\ClassCopierProxy; | ||
use oat\tao\model\resources\Service\ClassMetadataCopier; | ||
use oat\tao\model\resources\Service\ClassMetadataMapper; | ||
use oat\tao\model\resources\Service\InstanceMetadataCopier; | ||
use oat\tao\model\resources\Service\RootClassesListService; | ||
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use taoTests_models_classes_TestsService; | ||
|
||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class CopierServiceProvider implements ContainerServiceProviderInterface | ||
{ | ||
public function __invoke(ContainerConfigurator $configurator): void | ||
{ | ||
$services = $configurator->services(); | ||
|
||
$services | ||
->set(taoTests_models_classes_TestsService::class, taoTests_models_classes_TestsService::class) | ||
->factory(taoTests_models_classes_TestsService::class . '::singleton'); | ||
|
||
$services | ||
->get(InstanceMetadataCopier::class) | ||
->call( | ||
'addPropertyUrisToBlacklist', | ||
[ | ||
[ | ||
taoTests_models_classes_TestsService::PROPERTY_TEST_CONTENT, | ||
OntologyRdfs::RDFS_LABEL, | ||
] | ||
] | ||
); | ||
|
||
$services | ||
->set(TestContentCopier::class, TestContentCopier::class) | ||
->args( | ||
[ | ||
service(taoTests_models_classes_TestsService::class), | ||
service(EventManager::SERVICE_ID), | ||
] | ||
); | ||
|
||
$services | ||
->set(InstanceCopier::class . '::TESTS', InstanceCopier::class) | ||
->args( | ||
[ | ||
service(InstanceMetadataCopier::class), | ||
service(Ontology::SERVICE_ID) | ||
] | ||
) | ||
->call( | ||
'withInstanceContentCopier', | ||
[ | ||
service(TestContentCopier::class), | ||
] | ||
) | ||
->call( | ||
'withPermissionCopiers', | ||
[ | ||
tagged_iterator('tao.copier.permissions'), | ||
] | ||
); | ||
|
||
$services | ||
->set(ClassCopier::class . '::TESTS', ClassCopier::class) | ||
->share(false) | ||
->args( | ||
[ | ||
service(RootClassesListService::class), | ||
service(ClassMetadataCopier::class), | ||
service(InstanceCopier::class . '::TESTS'), | ||
service(ClassMetadataMapper::class), | ||
service(Ontology::SERVICE_ID), | ||
] | ||
) | ||
->call( | ||
'withPermissionCopiers', | ||
[ | ||
tagged_iterator('tao.copier.permissions'), | ||
] | ||
); | ||
|
||
$services | ||
->set(TestClassCopier::class, TestClassCopier::class) | ||
->share(false) | ||
->args( | ||
[ | ||
service(ClassCopier::class . '::TESTS'), | ||
service(Ontology::SERVICE_ID), | ||
] | ||
); | ||
|
||
$services | ||
->get(ClassCopierProxy::class) | ||
->call( | ||
'addClassCopier', | ||
[ | ||
TaoOntology::CLASS_URI_TEST, | ||
service(TestClassCopier::class), | ||
] | ||
); | ||
|
||
$services | ||
->get(InstanceCopierProxy::class) | ||
->call( | ||
'addInstanceCopier', | ||
[ | ||
TaoOntology::CLASS_URI_TEST, | ||
service(InstanceCopier::class . '::TESTS'), | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2023 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\taoTests\models\Copier; | ||
|
||
use InvalidArgumentException; | ||
use core_kernel_classes_Class; | ||
use oat\generis\model\data\Ontology; | ||
use oat\tao\model\resources\Command\ResourceTransferCommand; | ||
use oat\tao\model\resources\Contract\ResourceTransferInterface; | ||
use oat\tao\model\resources\ResourceTransferResult; | ||
use oat\tao\model\TaoOntology; | ||
use oat\tao\model\resources\Contract\ClassCopierInterface; | ||
|
||
class TestClassCopier implements ClassCopierInterface, ResourceTransferInterface | ||
{ | ||
private ResourceTransferInterface $taoClassCopier; | ||
private Ontology $ontology; | ||
|
||
public function __construct(ResourceTransferInterface $taoClassCopier, Ontology $ontology) | ||
{ | ||
$this->taoClassCopier = $taoClassCopier; | ||
$this->ontology = $ontology; | ||
} | ||
|
||
public function copy( | ||
core_kernel_classes_Class $class, | ||
core_kernel_classes_Class $destinationClass | ||
): core_kernel_classes_Class { | ||
$result = $this->transfer( | ||
new ResourceTransferCommand( | ||
$class->getUri(), | ||
$destinationClass->getUri(), | ||
ResourceTransferCommand::ACL_KEEP_ORIGINAL, | ||
ResourceTransferCommand::TRANSFER_MODE_COPY, | ||
) | ||
); | ||
|
||
return $this->ontology->getClass($result->getDestination()); | ||
} | ||
|
||
public function transfer(ResourceTransferCommand $command): ResourceTransferResult | ||
{ | ||
$this->assertRootClass($this->ontology->getClass($command->getFrom())); | ||
|
||
return $this->taoClassCopier->transfer($command); | ||
} | ||
|
||
private function assertRootClass(core_kernel_classes_Class $class): void | ||
{ | ||
$rootClass = $class->getClass(TaoOntology::CLASS_URI_TEST); | ||
|
||
if (!$class->equals($rootClass) && !$class->isSubClassOf($rootClass)) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Selected class (%s) is not supported because it is not part of the root class (%s).', | ||
$class->getUri(), | ||
TaoOntology::CLASS_URI_TEST | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2023 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\taoTests\models\Copier; | ||
|
||
use core_kernel_classes_Resource; | ||
use oat\oatbox\event\EventManager; | ||
use oat\tao\model\resources\Contract\InstanceContentCopierInterface; | ||
use oat\taoTests\models\event\TestDuplicatedEvent; | ||
use taoTests_models_classes_TestsService; | ||
|
||
class TestContentCopier implements InstanceContentCopierInterface | ||
{ | ||
private taoTests_models_classes_TestsService $testsService; | ||
private EventManager $eventManager; | ||
|
||
public function __construct(taoTests_models_classes_TestsService $testsService, EventManager $eventManager) | ||
{ | ||
$this->testsService = $testsService; | ||
$this->eventManager = $eventManager; | ||
} | ||
|
||
public function copy( | ||
core_kernel_classes_Resource $instance, | ||
core_kernel_classes_Resource $destinationInstance | ||
): void { | ||
$this->copyModel($instance, $destinationInstance); | ||
$this->testsService->cloneContent($instance, $destinationInstance); | ||
|
||
$this->eventManager->trigger(new TestDuplicatedEvent($instance->getUri(), $destinationInstance->getUri())); | ||
} | ||
|
||
private function copyModel(core_kernel_classes_Resource $from, core_kernel_classes_Resource $to): void | ||
{ | ||
$modelProperty = $from->getProperty(taoTests_models_classes_TestsService::PROPERTY_TEST_TESTMODEL); | ||
$model = $from->getOnePropertyValue($modelProperty); | ||
|
||
$to->editPropertyValues($modelProperty, $model instanceof core_kernel_classes_Resource ? $model : null); | ||
} | ||
} |
Oops, something went wrong.