diff --git a/src/UserData/Event/UserDataExportCleanupEvent.php b/src/UserData/Event/UserDataExportCleanupEvent.php new file mode 100644 index 0000000..d5fee38 --- /dev/null +++ b/src/UserData/Event/UserDataExportCleanupEvent.php @@ -0,0 +1,25 @@ +filePath; + } +} diff --git a/src/UserData/Event/UserDataExportCleanupSubscriber.php b/src/UserData/Event/UserDataExportCleanupSubscriber.php new file mode 100644 index 0000000..b7059d3 --- /dev/null +++ b/src/UserData/Event/UserDataExportCleanupSubscriber.php @@ -0,0 +1,34 @@ + 'onUserDataExportCleanup', + ]; + } + + public function onUserDataExportCleanup(UserDataExportCleanupEvent $event): void + { + $filePath = $event->getFilePath(); + + if (file_exists($filePath)) { + unlink($filePath); + } + } +} diff --git a/src/UserData/Service/UserDataFileDownloadService.php b/src/UserData/Service/UserDataFileDownloadService.php index 4acb663..a409e6b 100644 --- a/src/UserData/Service/UserDataFileDownloadService.php +++ b/src/UserData/Service/UserDataFileDownloadService.php @@ -10,12 +10,15 @@ namespace OxidEsales\GdprOptinModule\UserData\Service; use OxidEsales\Eshop\Core\Utils; +use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEvent; use OxidEsales\GdprOptinModule\UserData\Exception\UserDataFileDownloadException; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class UserDataFileDownloadService implements UserDataFileDownloadServiceInterface { public function __construct( - private Utils $shopUtils + private Utils $shopUtils, + private EventDispatcherInterface $eventDispatcher ) { } @@ -30,6 +33,10 @@ public function downloadFile(string $filePath): void /** @var string $fileContent */ $fileContent = file_get_contents($filePath); + + $event = new UserDataExportCleanupEvent($filePath); + $this->eventDispatcher->dispatch(event: $event); + $this->shopUtils->showMessageAndExit($fileContent); } diff --git a/src/UserData/services.yaml b/src/UserData/services.yaml index 600e51f..1c91870 100644 --- a/src/UserData/services.yaml +++ b/src/UserData/services.yaml @@ -227,3 +227,7 @@ services: OxidEsales\GdprOptinModule\UserData\Service\ZipCreatorServiceInterface: class: OxidEsales\GdprOptinModule\UserData\Service\ZipCreatorService + + OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupSubscriber: + class: OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupSubscriber + tags: [ 'kernel.event_subscriber' ] diff --git a/tests/Unit/UserData/Event/UserDataExportCleanupEventTest.php b/tests/Unit/UserData/Event/UserDataExportCleanupEventTest.php new file mode 100644 index 0000000..e84c99b --- /dev/null +++ b/tests/Unit/UserData/Event/UserDataExportCleanupEventTest.php @@ -0,0 +1,25 @@ +assertSame($testFilePath, $event->getFilePath()); + } +} diff --git a/tests/Unit/UserData/Event/UserDataExportCleanupSubscriberTest.php b/tests/Unit/UserData/Event/UserDataExportCleanupSubscriberTest.php new file mode 100644 index 0000000..39f9a43 --- /dev/null +++ b/tests/Unit/UserData/Event/UserDataExportCleanupSubscriberTest.php @@ -0,0 +1,49 @@ +withContent($fileContents) + ->at($vfsRoot); + + $this->assertTrue(file_exists($testFilePath)); + + $userDataExportCleanupEventSpy = $this->createConfiguredMock(UserDataExportCleanupEvent::class, [ + 'getFilePath' => $testFilePath, + ]); + + $sut = new UserDataExportCleanupSubscriber(); + $sut->onUserDataExportCleanup(event: $userDataExportCleanupEventSpy); + } + + public function testSubscriberSubscribesToCorrectEvent(): void + { + $this->assertArrayHasKey( + UserDataExportCleanupEvent::class, + UserDataExportCleanupSubscriber::getSubscribedEvents() + ); + } +} diff --git a/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php b/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php index e09cdbf..75c5a42 100644 --- a/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php +++ b/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php @@ -11,9 +11,12 @@ use org\bovigo\vfs\vfsStream; use OxidEsales\Eshop\Core\Utils; +use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEvent; use OxidEsales\GdprOptinModule\UserData\Exception\UserDataFileDownloadException; use OxidEsales\GdprOptinModule\UserData\Service\UserDataFileDownloadService; +use OxidEsales\GdprOptinModule\UserData\Service\UserDataFileDownloadServiceInterface; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class UserDataFileDownloadServiceTest extends TestCase { @@ -36,9 +39,8 @@ public function testFilenameHeaderSet(): void }; }); - $sut = new UserDataFileDownloadService( - shopUtils: $shopUtilsSpy - ); + $sut = $this->getSut(shopUtils: $shopUtilsSpy); + $sut->downloadFile($filePath); } @@ -54,9 +56,8 @@ public function testCorrectFileContentShown(): void $shopUtilsSpy->expects($this->atLeastOnce()) ->method('showMessageAndExit')->with($fileContent); - $sut = new UserDataFileDownloadService( - shopUtils: $shopUtilsSpy - ); + $sut = $this->getSut(shopUtils: $shopUtilsSpy); + $sut->downloadFile($filePath); } @@ -65,9 +66,11 @@ public function testFileDoesntExist(): void $tempDirectory = vfsStream::setup('root', null, []); $filePath = $tempDirectory->url() . '/' . uniqid(); - $sut = new UserDataFileDownloadService( - shopUtils: $this->createStub(Utils::class) - ); + $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); + $eventDispatcherSpy->expects($this->never()) + ->method('dispatch'); + + $sut = $this->getSut(eventDispatcher: $eventDispatcherSpy); $this->expectException(UserDataFileDownloadException::class); $sut->downloadFile($filePath); @@ -82,11 +85,51 @@ public function testFileNotReadable(): void $filePath = $tempDirectory->url() . '/' . $fileName; chmod($filePath, 0000); - $sut = new UserDataFileDownloadService( - shopUtils: $this->createStub(Utils::class) - ); + $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); + $eventDispatcherSpy->expects($this->never()) + ->method('dispatch'); + + $sut = $this->getSut(eventDispatcher: $eventDispatcherSpy); $this->expectException(UserDataFileDownloadException::class); $sut->downloadFile($filePath); } + + public function testEventDispatchTriggered(): void + { + $fileName = uniqid() . '.zip'; + $tempDirectory = vfsStream::setup('root', null, [ + $fileName => uniqid() + ]); + $filePath = $tempDirectory->url() . '/' . $fileName; + + $shopUtilsSpy = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); + $shopUtilsSpy->expects($this->atLeastOnce()) + ->method('showMessageAndExit'); + + $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); + $eventDispatcherSpy->expects($this->atLeastOnce()) + ->method('dispatch') + ->with($this->callback(function ($event) use ($filePath) { + return $event instanceof UserDataExportCleanupEvent + && $event->getFilePath() === $filePath; + })); + + $sut = $this->getSut(shopUtils: $shopUtilsSpy, eventDispatcher: $eventDispatcherSpy); + + $sut->downloadFile($filePath); + } + + protected function getSut( + ?Utils $shopUtils = null, + ?EventDispatcherInterface $eventDispatcher = null, + ): UserDataFileDownloadServiceInterface { + $shopUtils ??= $this->createStub(Utils::class); + $eventDispatcher ??= $this->createStub(EventDispatcherInterface::class); + + return new UserDataFileDownloadService( + shopUtils: $shopUtils, + eventDispatcher: $eventDispatcher, + ); + } }