Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Dec 27, 2023
1 parent 34b5925 commit 4a0f9da
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function __construct(private readonly Security $security)
#[AsCallback(table: 'tl_undo', target: 'list.operations.undo.button')]
public function __invoke(DataContainerOperation $operation): void
{
$data = StringUtil::deserialize($operation->getRecord()['data'] ?? null);
$table = $operation->getRecord()['fromTable'];
$record = $operation->getRecord();
$data = StringUtil::deserialize($record['data'] ?? null);
$table = $record['fromTable'];

// We can only disable undo if the main record access is denied, because child records cannot
// check their permissions on a non-existing parent record. DC_Table::undo() will actually verify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,76 +21,85 @@

class UndoOperationListenerTest extends TestCase
{
/**
* @dataProvider undoOperationListenerProvider
*/
public function testUndoOperationListener(array $data, array $isGranted): void
public function testUndoOperationIsGranted(): void
{
$operation = $this->createMock(DataContainerOperation::class);
$operation
->expects($this->once())
->method('getRecord')
->willReturn(['data' => serialize($data)])
->willReturn(['fromTable' => 'tl_foo', 'data' => serialize(['tl_foo' => [['id' => 42]]])])
;

$operation
->expects(\in_array(false, $isGranted, true) ? $this->once() : $this->never())
->expects($this->never())
->method('disable')
;

$calls = [];
$security = $this->createMock(Security::class);
$security
->expects($this->once())
->method('isGranted')
->with(
ContaoCorePermissions::DC_PREFIX.'tl_foo',
$this->callback(static fn ($action) => $action instanceof CreateAction && 'tl_foo' === $action->getDataSource()),
)
->willReturn(true)
;

$listener = new UndoOperationListener($security);
$listener($operation);
}

public function testUndoOperationIsDisabledIfIsNotGranted(): void
{
$operation = $this->createMock(DataContainerOperation::class);
$operation
->expects($this->once())
->method('getRecord')
->willReturn(['fromTable' => 'tl_bar', 'data' => serialize(['tl_bar' => [['id' => 42]]])])
;

foreach ($data as $table => $rows) {
foreach ($rows as $row) {
$calls[] = [
ContaoCorePermissions::DC_PREFIX.$table,
$this->callback(static fn (CreateAction $action) => $table === $action->getDataSource() && $row === $action->getNew()),
];
}
}
$operation
->expects($this->once())
->method('disable')
;

$security = $this->createMock(Security::class);
$security
->expects($this->exactly(\count($isGranted)))
->expects($this->once())
->method('isGranted')
->withConsecutive(...$calls)
->willReturnOnConsecutiveCalls(...$isGranted)
->with(
ContaoCorePermissions::DC_PREFIX.'tl_bar',
$this->callback(static fn ($action) => $action instanceof CreateAction && 'tl_bar' === $action->getDataSource()),
)
->willReturn(false)
;

$listener = new UndoOperationListener($security);
$listener($operation);
}

public function undoOperationListenerProvider(): \Generator
public function testUndoOperationIsDisabledIfRecordIsMissing(): void
{
yield [
['tl_foo' => [['id' => 42]]],
[true],
];

yield [
['tl_foo' => [['id' => 42], ['id' => 43]]],
[true, true],
];

yield [
['tl_foo' => [['id' => 42]], 'tl_bar' => [['id' => 43]]],
[true, true],
];

yield [
['tl_foo' => [['id' => 42]]],
[false],
];

yield [
['tl_foo' => [['id' => 42], ['id' => 43]]],
[true, false],
];

yield [
['tl_foo' => [['id' => 42]], 'tl_bar' => [['id' => 43]]],
[true, false],
];
$operation = $this->createMock(DataContainerOperation::class);
$operation
->expects($this->once())
->method('getRecord')
->willReturn(['fromTable' => 'tl_bar', 'data' => serialize([])])
;

$operation
->expects($this->once())
->method('disable')
;

$security = $this->createMock(Security::class);
$security
->expects($this->never())
->method('isGranted')
;

$listener = new UndoOperationListener($security);
$listener($operation);
}
}

0 comments on commit 4a0f9da

Please sign in to comment.