|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Test\Interaction; |
| 6 | + |
| 7 | +use OCP\EventDispatcher\IEventDispatcher; |
| 8 | +use OCP\Interaction\InteractionAction; |
| 9 | +use OCP\Interaction\InteractionReceiver; |
| 10 | +use OCP\Interaction\InteractionResource; |
| 11 | +use OCP\Interaction\InteractionRestrictedException; |
| 12 | +use OCP\Interaction\RestrictInteractionEvent; |
| 13 | +use OCP\IUser; |
| 14 | +use OCP\Log\Audit\CriticalActionPerformedEvent; |
| 15 | +use OCP\Server; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use Test\TestCase; |
| 18 | + |
| 19 | +final class RestrictInteractionEventTest extends TestCase { |
| 20 | + /** |
| 21 | + * @return list<array{bool}> |
| 22 | + */ |
| 23 | + public static function dataIsInteractionRestricted(): array { |
| 24 | + return [ |
| 25 | + [true], |
| 26 | + [false], |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + #[DataProvider('dataIsInteractionRestricted')] |
| 31 | + public function testIsInteractionRestricted(bool $isRestricted): void { |
| 32 | + $eventDispatcher = Server::get(IEventDispatcher::class); |
| 33 | + |
| 34 | + $auditEvents = []; |
| 35 | + $auditEventListener = function (CriticalActionPerformedEvent $event) use (&$auditEvents): void { |
| 36 | + $auditEvents[] = $event; |
| 37 | + }; |
| 38 | + $eventDispatcher->addListener(CriticalActionPerformedEvent::class, $auditEventListener); |
| 39 | + |
| 40 | + /** @psalm-suppress UnusedClosureParam */ |
| 41 | + $restrictInteractionEventListener = function (RestrictInteractionEvent $event) use ($isRestricted): void { |
| 42 | + if ($isRestricted) { |
| 43 | + throw new InteractionRestrictedException('my restriction'); |
| 44 | + } |
| 45 | + }; |
| 46 | + $eventDispatcher->addListener(RestrictInteractionEvent::class, $restrictInteractionEventListener); |
| 47 | + |
| 48 | + $user = $this->createMock(IUser::class); |
| 49 | + $user |
| 50 | + ->method('getUID') |
| 51 | + ->willReturn('my-uid'); |
| 52 | + |
| 53 | + $resource = $this->createMock(InteractionResource::class); |
| 54 | + $resource |
| 55 | + ->method('getID') |
| 56 | + ->willReturn('my-resource'); |
| 57 | + |
| 58 | + $action = $this->createStub(InteractionAction::class); |
| 59 | + |
| 60 | + $receiver = $this->createMock(InteractionReceiver::class); |
| 61 | + $receiver |
| 62 | + ->method('getID') |
| 63 | + ->willReturn('my-receiver'); |
| 64 | + |
| 65 | + $event = new RestrictInteractionEvent( |
| 66 | + $user, |
| 67 | + $resource, |
| 68 | + $action, |
| 69 | + $receiver, |
| 70 | + ); |
| 71 | + |
| 72 | + $this->assertEquals($isRestricted, $event->isInteractionRestricted()); |
| 73 | + |
| 74 | + $this->assertEquals([ |
| 75 | + new CriticalActionPerformedEvent( |
| 76 | + $isRestricted |
| 77 | + ? 'Interaction "%s" from user "%s" on "%s" to "%s" is restricted: my restriction' |
| 78 | + : 'Interaction "%s" from user "%s" on "%s" to "%s" is allowed.', |
| 79 | + [ |
| 80 | + $action::class, |
| 81 | + 'my-uid', |
| 82 | + 'my-resource', |
| 83 | + 'my-receiver', |
| 84 | + ], |
| 85 | + ), |
| 86 | + ], $auditEvents); |
| 87 | + |
| 88 | + $eventDispatcher->removeListener(CriticalActionPerformedEvent::class, $auditEventListener); |
| 89 | + $eventDispatcher->removeListener(RestrictInteractionEvent::class, $restrictInteractionEventListener); |
| 90 | + } |
| 91 | +} |
0 commit comments