Skip to content

Commit

Permalink
!fixup OXDEV-8733 Prepared UserDataexportController and adopted UserD…
Browse files Browse the repository at this point in the history
…ataExportService
  • Loading branch information
RahatHameed committed Oct 1, 2024
1 parent 26e2ec8 commit 42f2909
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
10 changes: 4 additions & 6 deletions src/UserData/Service/UserDataExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public function __construct(

public function exportUserData(string $userId): void
{
$outputZipFilePath = sys_get_temp_dir() . '/' . $this->userDataZipFilePath;
$serializedFiles = $this->userDataCollectionService->getUserDataAsFilesList($userId);
$this->zipCreatorService->createZip($serializedFiles, $outputZipFilePath);
$this->userDataFileDownloadService->downloadFile($outputZipFilePath);

unlink($outputZipFilePath);
$outputZipFilePath = '../' . $this->userDataZipFilePath . '/' . $userId . '.zip';
$serializedFiles = $this->userDataCollectionService->getUserDataAsFilesList(userId: $userId);
$this->zipCreatorService->createZip(files: $serializedFiles, outputFilePath: $outputZipFilePath);
$this->userDataFileDownloadService->downloadFile(filePath: $outputZipFilePath);
}
}
2 changes: 1 addition & 1 deletion src/UserData/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ services:
class: OxidEsales\GdprOptinModule\UserData\Service\UserDataExportService
public: true
arguments:
$userDataZipFilePath: 'user_data_export.zip'
$userDataZipFilePath: 'tmp/UserData'

OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactoryInterface:
class: OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactory
Expand Down
35 changes: 16 additions & 19 deletions tests/Unit/UserData/Service/UserDataExportServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

use org\bovigo\vfs\vfsStream;
use OxidEsales\GdprOptinModule\UserData\DataType\ResultFileInterface;
use OxidEsales\GdprOptinModule\UserData\Service\UserDataCollectionServiceInterface;
use OxidEsales\GdprOptinModule\UserData\Service\UserDataExportService;
Expand All @@ -19,21 +20,18 @@

class UserDataExportServiceTest extends TestCase
{
private string $zipFileName;
private string $tempDirectory;
private string $directorySuffix;
private string $userId;
private string $tempDir;

protected function setUp(): void
{
$this->zipFileName = 'user_data_export.zip';
$this->directorySuffix = 'test';
$this->userId = uniqid();
$this->createTempDirectory();
}

public function testZipCreationServiceTriggeredWithCorrectlyProcessedData(): void
{
$userId = uniqid();
$userDataZipFilePath = $this->tempDirectory . '/' . $this->zipFileName;
$expectedOutputFilePath = '../' . $this->tempDir . '/' . $this->userId . '.zip';

$filesListExample = [
$this->createStub(ResultFileInterface::class),
Expand All @@ -42,29 +40,28 @@ public function testZipCreationServiceTriggeredWithCorrectlyProcessedData(): voi

$userDataCollectionServiceMock = $this->createMock(UserDataCollectionServiceInterface::class);
$userDataCollectionServiceMock->method('getUserDataAsFilesList')
->with($userId)
->with($this->userId)
->willReturn($filesListExample);

$zipCreatorServiceSpy = $this->createMock(ZipCreatorServiceInterface::class);
$zipCreatorServiceSpy->expects($this->once())
->method('createZip')
->with($filesListExample, $userDataZipFilePath);
->with($filesListExample, $expectedOutputFilePath);

$fileDownloadServiceSpy = $this->createMock(UserDataFileDownloadServiceInterface::class);
$fileDownloadServiceSpy
->expects($this->once())
->method('downloadFile')
->with($userDataZipFilePath);
->with($expectedOutputFilePath);

$directoryPathWithoutTempDirectory = $this->directorySuffix . '/' . $this->zipFileName;
$sut = $this->getSut(
userDataCollectionService: $userDataCollectionServiceMock,
zipCreatorService: $zipCreatorServiceSpy,
userDataFileDownloadService: $fileDownloadServiceSpy,
userDataZipFilePath: $directoryPathWithoutTempDirectory
userDataZipFilePath: $this->tempDir
);

$sut->exportUserData(userId: $userId);
$sut->exportUserData(userId: $this->userId);
}

public function getSut(
Expand All @@ -76,7 +73,7 @@ public function getSut(
$userDataCollectionService ??= $this->createStub(UserDataCollectionServiceInterface::class);
$zipCreatorService ??= $this->createStub(ZipCreatorServiceInterface::class);
$userDataFileDownloadService ??= $this->createStub(UserDataFileDownloadServiceInterface::class);
$userDataZipFilePath ??= 'user_data_export.zip';
$userDataZipFilePath ??= 'tmp/UserData';

return new UserDataExportService(
userDataCollectionService: $userDataCollectionService,
Expand All @@ -88,16 +85,16 @@ public function getSut(

private function createTempDirectory(): void
{
$this->tempDirectory = sys_get_temp_dir() . '/' . $this->directorySuffix;
$this->tempDir = sys_get_temp_dir() . '/testDir';

mkdir($this->tempDirectory);
chmod($this->tempDirectory, 0777);
mkdir($this->tempDir);
chmod($this->tempDir, 0777);
}

protected function tearDown(): void
{
if (file_exists($this->tempDirectory)) {
rmdir($this->tempDirectory);
if (file_exists($this->tempDir)) {
rmdir($this->tempDir);
}
}
}

0 comments on commit 42f2909

Please sign in to comment.