Skip to content

Commit

Permalink
Merge pull request #632 from oat-sa/feat/PISA25-411-delete-old-assets…
Browse files Browse the repository at this point in the history
…-on-test-overwrite

feat: Delete related resources on QtiTestDeletedEvent
  • Loading branch information
hectoras authored Jul 25, 2023
2 parents a013590 + 784ef53 commit b53e506
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 35 deletions.
47 changes: 47 additions & 0 deletions models/classes/Command/DeleteItemCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\taoItems\model\Command;

use core_kernel_classes_Resource;

class DeleteItemCommand
{
private core_kernel_classes_Resource $resource;
private bool $deleteRelatedAssets;

public function __construct(core_kernel_classes_Resource $resource, bool $deleteRelatedAssets = false)
{
$this->resource = $resource;
$this->deleteRelatedAssets = $deleteRelatedAssets;
}

public function getResource(): core_kernel_classes_Resource
{
return $this->resource;
}

public function mustDeleteRelatedAssets(): bool
{
return $this->deleteRelatedAssets;
}
}
50 changes: 39 additions & 11 deletions models/classes/class.ItemsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
* (under the project TAO-TRANSFER);
* 2009-2012 (update and modification) Public Research Centre Henri Tudor
* (under the project TAO-SUSTAIN & TAO-DEV);
* 2012-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT)
* 2012-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT)
*/

use oat\taoItems\model\Command\DeleteItemCommand;
use oat\taoItems\model\TaoItemOntology;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\oatbox\filesystem\Directory;
Expand Down Expand Up @@ -132,6 +133,37 @@ public function isItemClass(core_kernel_classes_Class $clazz)
return $clazz->equals($this->getRootClass()) || $clazz->isSubClassOf($this->getRootClass());
}

public function delete(DeleteItemCommand $command): void
{
$resource = $command->getResource();

if (LockManager::getImplementation()->isLocked($resource)) {
$userId = common_session_SessionManager::getSession()->getUser()->getIdentifier();
LockManager::getImplementation()->releaseLock($resource, $userId);
}

$result = $this->deleteItemContent($resource) && parent::deleteResource($resource);

if (!$result) {
throw new Exception(
sprintf(
'Error deleting item content for resource "%s" [%s]',
$resource->getLabel(),
$resource->getUri()
)
);
}

$this->getEventManager()->trigger(
new ItemRemovedEvent(
$resource->getUri(),
[
ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => $command->mustDeleteRelatedAssets(),
]
)
);
}

/**
* please call deleteResource() instead
* @deprecated
Expand All @@ -146,21 +178,17 @@ public function deleteItem(core_kernel_classes_Resource $item)
* @param core_kernel_classes_Resource $resource
* @throws common_exception_Unauthorized
* @return boolean
* @deprecated use self::delete()
*/
public function deleteResource(core_kernel_classes_Resource $resource)
{
if (LockManager::getImplementation()->isLocked($resource)) {
$userId = common_session_SessionManager::getSession()->getUser()->getIdentifier();
LockManager::getImplementation()->releaseLock($resource, $userId);
}

$result = $this->deleteItemContent($resource) && parent::deleteResource($resource);
try {
$this->delete(new DeleteItemCommand($resource));

if ($result) {
$this->getEventManager()->trigger(new ItemRemovedEvent($resource->getUri()));
return true;
} catch (Throwable $exception) {
return false;
}

return $result;
}

/**
Expand Down
38 changes: 14 additions & 24 deletions models/classes/event/ItemRemovedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,39 @@
* 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) 2016 (original work) Open Assessment Technologies SA
*
* Copyright (c) 2016-2023 (original work) Open Assessment Technologies SA.
*/

namespace oat\taoItems\model\event;

use JsonSerializable;
use oat\oatbox\event\Event;
use JsonSerializable;

class ItemRemovedEvent implements Event, JsonSerializable
{
/** @var string */
protected $itemUri;
public const PAYLOAD_KEY_DELETE_RELATED_ASSETS = 'deleteRelatedAssets';
public const PAYLOAD_KEY_ITEM_URI = 'itemUri';

/**
* @param String $itemUri
*/
public function __construct($itemUri)
protected string $itemUri;

private array $payload;

public function __construct(string $itemUri, array $payload = [])
{
$this->itemUri = $itemUri;
$this->payload = $payload;
}


/**
* Return a unique name for this event
* @see \oat\oatbox\event\Event::getName()
*/
public function getName()
{
return get_class($this);
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return [
'itemUri' => $this->itemUri
];
return array_merge(
[self::PAYLOAD_KEY_ITEM_URI => $this->itemUri],
$this->payload
);
}
}
48 changes: 48 additions & 0 deletions test/unit/model/event/ItemRemovedEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\taoItems\test\unit\model\event;

use oat\taoItems\model\event\ItemRemovedEvent;
use PHPUnit\Framework\TestCase;

class ItemRemovedEventTest extends TestCase
{
public function testGetters(): void
{
$event = new ItemRemovedEvent(
'itemUri',
[
ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => true,
]
);

$this->assertSame(ItemRemovedEvent::class, $event->getName());
$this->assertSame(
[
ItemRemovedEvent::PAYLOAD_KEY_ITEM_URI => 'itemUri',
ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => true,
],
$event->jsonSerialize()
);
}
}
39 changes: 39 additions & 0 deletions test/unit/models/classes/Command/DeleteItemCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\taoItems\test\unit\models\classes\Command;

use core_kernel_classes_Resource;
use oat\taoItems\model\Command\DeleteItemCommand;
use PHPUnit\Framework\TestCase;

class DeleteItemCommandTest extends TestCase
{
public function testGetters(): void
{
$resource = $this->createMock(core_kernel_classes_Resource::class);
$command = new DeleteItemCommand($resource, true);

$this->assertSame($resource, $command->getResource());
$this->assertTrue($command->mustDeleteRelatedAssets());
}
}

0 comments on commit b53e506

Please sign in to comment.