Skip to content

Commit

Permalink
OXDEV-8733 Prepared event to clean up exported file
Browse files Browse the repository at this point in the history
  • Loading branch information
RahatHameed committed Oct 7, 2024
1 parent 01f276b commit 58c0f16
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/UserData/Event/UserDataExportCleanupEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GdprOptinModule\UserData\Event;

use Symfony\Contracts\EventDispatcher\Event;

class UserDataExportCleanupEvent extends Event
{
public function __construct(
private readonly string $filePath
) {
}

public function getFilePath(): string
{
return $this->filePath;
}
}
34 changes: 34 additions & 0 deletions src/UserData/Event/UserDataExportCleanupSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GdprOptinModule\UserData\Event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserDataExportCleanupSubscriber implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
UserDataExportCleanupEvent::class => 'onUserDataExportCleanup',
];
}

public function onUserDataExportCleanup(UserDataExportCleanupEvent $event): void
{
$filePath = $event->getFilePath();

if (file_exists($filePath)) {
unlink($filePath);
}
}
}
9 changes: 8 additions & 1 deletion src/UserData/Service/UserDataFileDownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/UserData/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
25 changes: 25 additions & 0 deletions tests/Unit/UserData/Event/UserDataExportCleanupEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GdprOptinModule\Tests\Unit\UserData\Event;

use PHPUnit\Framework\TestCase;
use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEvent;

class UserDataExportCleanupEventTest extends TestCase
{
public function testEventStoresFilePath(): void
{
$testFilePath = 'test/' . uniqid() . '.zip';

$event = new UserDataExportCleanupEvent($testFilePath);

$this->assertSame($testFilePath, $event->getFilePath());
}
}
49 changes: 49 additions & 0 deletions tests/Unit/UserData/Event/UserDataExportCleanupSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GdprOptinModule\Tests\Unit\UserData\Event;

use org\bovigo\vfs\vfsStream;
use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupSubscriber;
use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEvent;
use PHPUnit\Framework\TestCase;

class UserDataExportCleanupSubscriberTest extends TestCase
{
public function testOnUserDataExportCleanupUnlinkedFile(): void
{
$fileName = uniqid() . '.zip';


$vfsRoot = vfsStream::setup('root');
$testFilePath = vfsStream::url('root/' . $fileName);

$fileContents = uniqid();
vfsStream::newFile($fileName, 0755)
->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()
);
}
}
67 changes: 55 additions & 12 deletions tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -36,9 +39,8 @@ public function testFilenameHeaderSet(): void
};
});

$sut = new UserDataFileDownloadService(
shopUtils: $shopUtilsSpy
);
$sut = $this->getSut(shopUtils: $shopUtilsSpy);

$sut->downloadFile($filePath);
}

Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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,
);
}
}

0 comments on commit 58c0f16

Please sign in to comment.