Skip to content

Commit

Permalink
feat: Delete old assets on test overwrite (#2393)
Browse files Browse the repository at this point in the history
* feat: Delete old assets on test overwrite

* feat: Rename event, trigger event before the deletions

* feat: Remove logic to extract media references form this extension

* fix: dont delete classes whose label don't match the expected label

---------

Co-authored-by: Gabriel Felipe Soares <gabrielfs7@gmail.com>
  • Loading branch information
hectoras and gabrielfs7 authored Jul 25, 2023
1 parent c272c07 commit d2d83c3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"qtism/qtism": ">=0.28.3",
"oat-sa/generis" : ">=15.22",
"oat-sa/tao-core": ">=53.0.0",
"oat-sa/extension-tao-item" : ">=12.0.0",
"oat-sa/extension-tao-item" : ">=12.1.0",
"oat-sa/extension-tao-itemqti" : ">=30.0.0",
"oat-sa/extension-tao-test" : ">=16.0.0",
"oat-sa/extension-tao-delivery" : ">=15.0.0",
Expand Down
55 changes: 36 additions & 19 deletions models/classes/class.QtiTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* 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) 2013-2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* Copyright (c) 2013-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

use League\Flysystem\FileExistsException;
Expand All @@ -26,7 +25,7 @@
use oat\tao\model\resources\ResourceAccessDeniedException;
use oat\tao\model\resources\SecureResourceServiceInterface;
use oat\tao\model\TaoOntology;
use oat\taoBackOffice\model\tree\TreeService;
use oat\taoItems\model\Command\DeleteItemCommand;
use oat\taoQtiItem\model\qti\ImportService;
use oat\taoQtiItem\model\qti\metadata\importer\MetadataImporter;
use oat\taoQtiItem\model\qti\metadata\MetadataGuardianResource;
Expand All @@ -40,6 +39,7 @@
use oat\taoQtiTest\models\render\QtiPackageImportPreprocessing;
use oat\taoQtiTest\models\test\AssessmentTestXmlFactory;
use oat\taoTests\models\event\TestUpdatedEvent;
use Psr\Container\ContainerInterface;
use qtism\common\utils\Format;
use qtism\data\AssessmentItemRef;
use qtism\data\QtiComponentCollection;
Expand All @@ -57,7 +57,6 @@
* @author Bertrand Chevrier <bertrand@taotesting.com>
* @author Jerome Bogaerts <jerome@taotesting.com>
* @package taoQtiTest
*/
class taoQtiTest_models_classes_QtiTestService extends TestService
{
Expand Down Expand Up @@ -348,7 +347,6 @@ public function importMultipleTests(
bool $overwriteTest = false,
?string $itemClassUri = null
) {

$testClass = $targetClass;
$report = new common_report_Report(common_report_Report::TYPE_INFO);
$validPackage = false;
Expand Down Expand Up @@ -866,26 +864,24 @@ protected function importTest(
return $report;
}

/**
* @throws common_Exception
*/
private function deleteTestsFromClassByLabel(
string $testLabel,
string $itemsClassLabel,
core_kernel_classes_Resource $testClass,
core_kernel_classes_Class $itemClass
): void {
$testService = $this->getTestService();
$itemTreeService = $this->getItemTreeService();

$this->deleteItemSubclassesByLabel($itemClass, $itemsClassLabel);

foreach ($testClass->getInstances() as $testInstance) {
if ($testInstance->getLabel() === $testLabel) {
$testService->deleteTest($testInstance);
}
}

foreach ($itemClass->getSubClasses() as $subClass) {
if ($subClass->getLabel() === $itemsClassLabel) {
$itemTreeService->deleteClass($subClass);
}
}
}

/**
Expand Down Expand Up @@ -1446,21 +1442,42 @@ private function verifyItemPermissions(core_kernel_classes_Resource $oldTest, st
$this->getSecureResourceService()->validatePermissions($ids, ['READ']);
}

/**
* @return QtiPackageImportPreprocessing
*/
private function getQtiPackageImportPreprocessing()
private function deleteItemSubclassesByLabel(
core_kernel_classes_Class $root,
string $label
): void {
$itemTreeService = $this->getItemTreeService();

foreach ($root->getSubClasses() as $subClass) {
if ($subClass->getLabel() !== $label) {
continue;
}

foreach ($subClass->getInstances(true) as $instance) {
$itemTreeService->delete(new DeleteItemCommand($instance, true));
}

$itemTreeService->deleteClass($subClass);
}
}

private function getQtiPackageImportPreprocessing(): QtiPackageImportPreprocessing
{
return $this->getServiceLocator()->get(QtiPackageImportPreprocessing::SERVICE_ID);
return $this->getPsrContainer()->get(QtiPackageImportPreprocessing::SERVICE_ID);
}

private function getItemTreeService(): taoItems_models_classes_ItemsService
{
return taoItems_models_classes_ItemsService::singleton();
return $this->getPsrContainer()->get(taoItems_models_classes_ItemsService::class);
}

private function getTestService(): taoTests_models_classes_TestsService
{
return taoTests_models_classes_TestsService::singleton();
return $this->getPsrContainer()->get(taoTests_models_classes_TestsService::class);
}

private function getPsrContainer(): ContainerInterface
{
return $this->getServiceLocator()->getContainer();
}
}
61 changes: 47 additions & 14 deletions test/unit/models/classes/xml/XmlEditorTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
<?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) 2020-2023 (original work) Open Assessment Technologies SA.
*/

namespace oat\taoQtiTest\test\unit\models\classes\xml;

use core_kernel_classes_Resource;
use oat\generis\test\TestCase;
use oat\tao\model\featureFlag\FeatureFlagChecker;
use oat\taoQtiTest\models\xmlEditor\XmlEditor;
use PHPUnit\Framework\MockObject\MockObject;
use qtism\data\storage\xml\XmlDocument;
Expand All @@ -16,33 +35,40 @@

class XmlEditorTest extends TestCase
{
/**
* @var XmlDocument
*/
/** @var XmlDocument */
private $xmlDoc;
/**
* @var core_kernel_classes_Resource|MockObject
*/

/** @var core_kernel_classes_Resource|MockObject */
private $testResourceMock;
/**
* @var ServiceLocatorInterface
*/

/** @var ServiceLocatorInterface */
private $serviceLocatorMock;
/**
* @var MockObject|taoQtiTest_models_classes_QtiTestService
*/

/** @var taoQtiTest_models_classes_QtiTestService|MockObject */
private $qtiTestServiceMock;

/** @var FeatureFlagChecker|MockObject */
private $featureFlagCheckerMock;

public function setUp(): void
{
$doc = new XmlDocument();
$doc->load(__DIR__ . '/../../../../samples/xml/test.xml');
$this->xmlDoc = $doc;

$this->testResourceMock = $this->createMock(core_kernel_classes_Resource::class);

$this->qtiTestServiceMock = $this->createMock(taoQtiTest_models_classes_QtiTestService::class);
$this->qtiTestServiceMock->method('getDoc')->with($this->testResourceMock)->willReturn($this->xmlDoc);
$this->qtiTestServiceMock
->method('getDoc')
->with($this->testResourceMock)
->willReturn($this->xmlDoc);

$this->featureFlagCheckerMock = $this->createMock(FeatureFlagChecker::class);

$this->serviceLocatorMock = $this->getServiceLocatorMock([
taoQtiTest_models_classes_QtiTestService::class => $this->qtiTestServiceMock
taoQtiTest_models_classes_QtiTestService::class => $this->qtiTestServiceMock,
FeatureFlagChecker::class => $this->featureFlagCheckerMock,
]);
}

Expand Down Expand Up @@ -145,6 +171,13 @@ public function testSaveStringTest()
public function testIsLocked()
{
$service = new XmlEditor([XmlEditor::OPTION_XML_EDITOR_LOCK => false]);
$service->setServiceLocator($this->serviceLocatorMock);

$this->featureFlagCheckerMock
->method('isEnabled')
->with('XML_EDITOR_ENABLED')
->willReturn(true);

$this->assertEquals(false, $service->isLocked());
}
}

0 comments on commit d2d83c3

Please sign in to comment.