File size: 3,233 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?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();

        $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 beforeTearDown(): void
    {
        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->detach($asset);

        return $asset;
    }

    /**
     * Generate the csv asset and return the path of the asset.
     */
    protected function generateCsv(): void
    {
        $uploadDir  = static::getContainer()->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'],
            ['[email protected]', 'John', 'Doe'],
            ['[email protected]', 'John', 'Smith'],
            ['[email protected]', 'Jim', 'Doe'],
            [''],
            ['[email protected]', 'Jim', 'Smith'],
        ];

        foreach ($initialList as $line) {
            fputcsv($file, $line);
        }

        fclose($file);

        $this->csvPath = $tmpFile;
    }
}