Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assets are streamed by default (#11043)
* Assets are streamed by default * Add a functional test * Add functional tests * Modify tests * Fix tests * Fix tests Co-authored-by: Ruth Cheesley <ruth.cheesley@acquia.com>
- Loading branch information
1 parent
63491f4
commit 52f221e
Showing
5 changed files
with
231 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
app/bundles/AssetBundle/Tests/Asset/AbstractAssetTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mautic\AssetBundle\Tests\Asset; | ||
|
||
use Doctrine\ORM\ORMException; | ||
use Doctrine\Persistence\Mapping\MappingException; | ||
use Mautic\AssetBundle\Entity\Asset; | ||
use Mautic\CoreBundle\Test\MauticMysqlTestCase; | ||
|
||
abstract class AbstractAssetTest extends MauticMysqlTestCase | ||
{ | ||
protected Asset $asset; | ||
protected string $expectedMimeType; | ||
protected string $expectedContentDisposition; | ||
protected string $expectedPngContent; | ||
protected string $csvPath; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
defined('MAUTIC_TABLE_PREFIX') or define('MAUTIC_TABLE_PREFIX', ''); | ||
|
||
$this->generateCsv(); | ||
|
||
$assetData = [ | ||
'title' => 'Asset controller test. Preview action', | ||
'alias' => 'Test', | ||
'createdAt' => new \DateTime('2021-05-05 22:30:00'), | ||
'updatedAt' => new \DateTime('2022-05-05 22:30:00'), | ||
'createdBy' => 'User', | ||
'storage' => 'local', | ||
'path' => basename($this->csvPath), | ||
'extension' => 'png', | ||
]; | ||
$this->asset = $this->createAsset($assetData); | ||
|
||
$this->expectedMimeType = 'text/plain; charset=UTF-8'; | ||
$this->expectedContentDisposition = 'attachment;filename="'; | ||
$this->expectedPngContent = file_get_contents($this->csvPath); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
if (file_exists($this->csvPath)) { | ||
unlink($this->csvPath); | ||
} | ||
} | ||
|
||
/** | ||
* Create an asset entity in the DB. | ||
* | ||
* @param array<string, string|mixed> $assetData | ||
* | ||
* @throws ORMException | ||
* @throws MappingException | ||
*/ | ||
protected function createAsset(array $assetData): Asset | ||
{ | ||
$asset = new Asset(); | ||
$asset->setTitle($assetData['title']); | ||
$asset->setAlias($assetData['alias']); | ||
$asset->setDateAdded($assetData['createdAt'] ?? new \DateTime()); | ||
$asset->setDateModified($assetData['updatedAt'] ?? new \DateTime()); | ||
$asset->setCreatedByUser($assetData['createdBy'] ?? 'User'); | ||
$asset->setStorageLocation($assetData['storage'] ?? 'local'); | ||
$asset->setPath($assetData['path'] ?? ''); | ||
$asset->setExtension($assetData['extension'] ?? ''); | ||
|
||
$this->em->persist($asset); | ||
$this->em->flush(); | ||
$this->em->clear(); | ||
|
||
return $asset; | ||
} | ||
|
||
/** | ||
* Generate the csv asset and return the path of the asset. | ||
*/ | ||
protected function generateCsv(): void | ||
{ | ||
$uploadDir = self::$container->get('mautic.helper.core_parameters')->get('upload_dir') ?? sys_get_temp_dir(); | ||
$tmpFile = tempnam($uploadDir, 'mautic_asset_test_'); | ||
$file = fopen($tmpFile, 'w'); | ||
|
||
$initialList = [ | ||
['email', 'firstname', 'lastname'], | ||
['john.doe@his-site.com.email', 'John', 'Doe'], | ||
['john.smith@his-site.com.email', 'John', 'Smith'], | ||
['jim.doe@his-site.com.email', 'Jim', 'Doe'], | ||
[''], | ||
['jim.smith@his-site.com.email', 'Jim', 'Smith'], | ||
]; | ||
|
||
foreach ($initialList as $line) { | ||
fputcsv($file, $line); | ||
} | ||
|
||
fclose($file); | ||
|
||
$this->csvPath = $tmpFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/bundles/AssetBundle/Tests/Controller/PublicControllerFunctionalTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Mautic\AssetBundle\Tests\Controller; | ||
|
||
use Mautic\AssetBundle\Tests\Asset\AbstractAssetTest; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class PublicControllerFunctionalTest extends AbstractAssetTest | ||
{ | ||
/** | ||
* Download action should return the file content. | ||
*/ | ||
public function testDownloadActionStreamByDefault(): void | ||
{ | ||
$assetSlug = $this->asset->getId().':'.$this->asset->getAlias(); | ||
|
||
$this->client->request('GET', '/asset/'.$assetSlug); | ||
ob_start(); | ||
$response = $this->client->getResponse(); | ||
$response->sendContent(); | ||
$content = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); | ||
$this->assertSame($this->expectedMimeType, $response->headers->get('Content-Type')); | ||
$this->assertNotSame($this->expectedContentDisposition.$this->asset->getOriginalFileName(), $response->headers->get('Content-Disposition')); | ||
$this->assertEquals($this->expectedPngContent, $content); | ||
} | ||
|
||
/** | ||
* Download action should return the file content. | ||
*/ | ||
public function testDownloadActionStreamIsZero(): void | ||
{ | ||
$assetSlug = $this->asset->getId().':'.$this->asset->getAlias(); | ||
|
||
$this->client->request('GET', '/asset/'.$assetSlug.'?stream=0'); | ||
ob_start(); | ||
$response = $this->client->getResponse(); | ||
$response->sendContent(); | ||
$content = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); | ||
$this->assertSame($this->expectedContentDisposition.$this->asset->getOriginalFileName(), $response->headers->get('Content-Disposition')); | ||
$this->assertEquals($this->expectedPngContent, $content); | ||
} | ||
} |