@@ -2,8 +2,11 @@

namespace Shopware\Core\Content\Test\ImportExport\Service;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\ImportExport\Exception\InvalidFileAccessTokenException;
use Shopware\Core\Content\ImportExport\Service\DownloadService;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
@@ -24,18 +27,82 @@ public function testUtf8Filename(): void
'originalName' => $asciiName . ' öäüß',
'path' => 'test.csv',
'expireDate' => new \DateTime(),
'accessToken' => 'token',
];
$filesystem->put($fileData['path'], $fileData['originalName']);
$context = Context::createDefaultContext();
$fileRepository->create([$fileData], $context);

$downloadService = new DownloadService($filesystem, $fileRepository);
$accessToken = $downloadService->regenerateToken($context, $fileData['id']);

$response = $downloadService->createFileResponse($context, $fileData['id'], 'token');
$response = $downloadService->createFileResponse($context, $fileData['id'], $accessToken);
static::assertStringContainsString($asciiName, $response->headers->get('Content-Disposition'));

$response->sendContent();
$this->expectOutputString($fileData['originalName']);
}

public function testDownloadWithInvalidAccessToken(): void
{
$filesystem = $this->getContainer()->get('shopware.filesystem.private');
$fileRepository = $this->getContainer()->get('import_export_file.repository');

$asciiName = 'Name with non-ascii chars';

$fileData = [
'id' => Uuid::randomHex(),
'originalName' => $asciiName . ' öäüß',
'path' => 'test.csv',
'expireDate' => new \DateTime(),
'accessToken' => 'token',
];
$filesystem->put($fileData['path'], $fileData['originalName']);
$context = Context::createDefaultContext();
$fileRepository->create([$fileData], $context);

$downloadService = new DownloadService($filesystem, $fileRepository);

static::expectException(InvalidFileAccessTokenException::class);

$downloadService->createFileResponse($context, $fileData['id'], 'token');
}

public function testDownloadWithExpiredAccessToken(): void
{
$filesystem = $this->getContainer()->get('shopware.filesystem.private');
$fileRepository = $this->getContainer()->get('import_export_file.repository');

$asciiName = 'Name with non-ascii chars';

$fileData = [
'id' => Uuid::randomHex(),
'originalName' => $asciiName . ' öäüß',
'path' => 'test.csv',
'expireDate' => new \DateTime(),
'accessToken' => 'token',
];
$filesystem->put($fileData['path'], $fileData['originalName']);
$context = Context::createDefaultContext();
$fileRepository->create([$fileData], $context);

$downloadService = new DownloadService($filesystem, $fileRepository);

$validToken = $downloadService->regenerateToken($context, $fileData['id']);

// Expire it
$connection = $this->getContainer()->get(Connection::class);
$connection->update(
'import_export_file',
[
'updated_at' => date(Defaults::STORAGE_DATE_TIME_FORMAT, strtotime('-6minutes')),
],
[
'id' => Uuid::fromHexToBytes($fileData['id']),
]
);

static::expectException(InvalidFileAccessTokenException::class);

$downloadService->createFileResponse($context, $fileData['id'], $validToken);
}
}
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration;

/**
* @deprecated tag:v6.5.0 - Will be deleted. Migrations are now namespaced by major version
*/
class Migration1627540693MakeAccessTokenNullable extends \Shopware\Core\Migration\V6_4\Migration1627540693MakeAccessTokenNullable
{
}
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\Test;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
use Shopware\Core\Migration\V6_4\Migration1627540693MakeAccessTokenNullable;

class Migration1627540693MakeAccessTokenNullableTest extends TestCase
{
use KernelTestBehaviour;

public function testMigrate(): void
{
$connection = $this->getContainer()->get(Connection::class);

// Make it not nullable
$connection->executeStatement('ALTER TABLE `import_export_file` CHANGE `access_token` `access_token` varchar(255) COLLATE \'utf8mb4_unicode_ci\' NOT NULL AFTER `created_at`;');

$m = new Migration1627540693MakeAccessTokenNullable();
$m->update($connection);

$columns = $connection->getSchemaManager()->listTableColumns('import_export_file');

$foundColumn = false;
foreach ($columns as $column) {
if ($column->getName() === 'access_token') {
$foundColumn = true;

static::assertFalse($column->getNotnull());
}
}

static::assertTrue($foundColumn, 'Could not find column access_token');
}
}
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\V6_4;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1627540693MakeAccessTokenNullable extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1627540693;
}

public function update(Connection $connection): void
{
$connection->executeStatement('ALTER TABLE `import_export_file` CHANGE `access_token` `access_token` varchar(255) COLLATE \'utf8mb4_unicode_ci\' NULL AFTER `created_at`;');
}

public function updateDestructive(Connection $connection): void
{
}
}