Skip to content

Commit

Permalink
Merge branch 'release-15.28.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Sep 18, 2023
2 parents 8647c2d + 7251dd5 commit 8a93fd3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
48 changes: 48 additions & 0 deletions core/data/event/BeforeResourceDeleted.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\generis\model\data\event;

use oat\oatbox\event\Event;

class BeforeResourceDeleted implements Event
{
private string $uri;

public function __construct(string $uri)
{
$this->uri = $uri;
}

public function getUri(): string
{
return $this->uri;
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return __CLASS__;
}
}
4 changes: 4 additions & 0 deletions core/resource/Repository/ResourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace oat\generis\model\resource\Repository;

use oat\generis\model\data\event\BeforeResourceDeleted;
use RuntimeException;
use BadMethodCallException;
use InvalidArgumentException;
Expand Down Expand Up @@ -68,6 +69,9 @@ public function delete(ContextInterface $context): void
false
);

$beforeResourceDeletedEvent = new BeforeResourceDeleted($resource->getUri());
$this->eventManager->trigger($beforeResourceDeletedEvent);

if (!$this->getImplementation()->delete($resource, $deleteReference)) {
throw new RuntimeException(
sprintf(
Expand Down
12 changes: 8 additions & 4 deletions test/unit/core/resource/Repository/ResourceRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace oat\generis\test\unit\model\resource\Repository;

use oat\generis\model\data\event\BeforeResourceDeleted;
use RuntimeException;
use InvalidArgumentException;
use core_kernel_classes_Class;
Expand Down Expand Up @@ -90,7 +91,7 @@ public function testDeleteSuccess(): void
->method('delete')
->willReturn(true);
$this->eventManager
->expects($this->once())
->expects($this->exactly(2))
->method('trigger');

$context = $this->createContext(4, $this->createResource('resourceUri'));
Expand Down Expand Up @@ -132,8 +133,11 @@ public function testDeleteFailure(): void
->method('delete')
->willReturn(false);
$this->eventManager
->expects($this->never())
->method('trigger');
->expects($this->once())
->method('trigger')
->with($this->callback(function ($parameter) {
return $parameter instanceof BeforeResourceDeleted;
}));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Resource "resourceLabel" ("resourceUri") was not deleted.');
Expand All @@ -146,7 +150,7 @@ private function createResource(string $uri, string $label = null): core_kernel_
{
$class = $this->createMock(core_kernel_classes_Resource::class);
$class
->expects($this->once())
->expects($this->exactly(2))
->method('getUri')
->willReturn($uri);
$class
Expand Down

0 comments on commit 8a93fd3

Please sign in to comment.