-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAmazonS3DriverTest.php
More file actions
237 lines (218 loc) · 8.44 KB
/
Copy pathAmazonS3DriverTest.php
File metadata and controls
237 lines (218 loc) · 8.44 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/***
*
* This file is part of an extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2020 Markus Hölzle <typo3@markus-hoelzle.de>
*
***/
namespace AUS\AusDriverAmazonS3\Tests\Unit\Driver;
use AUS\AusDriverAmazonS3\Driver\AmazonS3Driver;
use Aws\Api\DateTimeResult;
use Aws\Result;
use Aws\S3\S3Client;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class AmazonS3DriverTest
*
* @author Markus Hölzle <typo3@markus-hoelzle.de>
* @package AUS\AusDriverAmazonS3\Tests\Unit\Driver
*/
class AmazonS3DriverTest extends TestCase
{
use ProphecyTrait;
/**
* @var AmazonS3Driver
*/
protected $driver = null;
/**
* @var ObjectProphecy
*/
protected $s3Client = null;
/**
* @var string[]
*/
protected $testConfiguration = [
'protocol' => 'https://',
'publicBaseUrl' => 'www.example.com',
'bucket' => 'test-bucket',
'region' => 'test-region',
'key' => 'test-key',
'secretKey' => 'test-secretKey',
];
/**
* @return void
*/
public function setUp(): void
{
parent::setUp();
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][AmazonS3Driver::EXTENSION_KEY] = [];
$GLOBALS['TYPO3_CONF_VARS']['LOG'] = [];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType']['youtube'] = 'video/youtube';
Environment::initialize(
$this->prophesize(ApplicationContext::class)->reveal(),
false,
true,
'',
'',
'',
'',
'',
''
);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('applicationType')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_FE);
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$cacheManager->setCacheConfigurations([
'ausdriveramazons3_metainfocache' => [
'backend' => \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend::class,
'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
],
'ausdriveramazons3_requestcache' => [
'backend' => \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend::class,
'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
]
]);
$this->s3Client = $this->prophesize(S3Client::class);
$eventDispatcher = $this->prophesize(EventDispatcher::class);
$this->driver = new AmazonS3Driver($this->testConfiguration, $this->s3Client->reveal(), $eventDispatcher->reveal());
$this->driver->setStorageUid(42);
$this->driver->initialize();
}
public function tearDown(): void
{
unset($GLOBALS['TYPO3_REQUEST']);
parent::tearDown();
}
/**
* @test
*/
public function testPublicUrlGetter()
{
$assertedMappings = [
'/foo/bar/test.file' => 'https://www.example.com/foo/bar/test.file', // start with slash
'foo/bar/test.file' => 'https://www.example.com/foo/bar/test.file', // start without slash
'/foo//bar/test.file' => 'https://www.example.com/foo/bar/test.file', // duplicated slash in path
'/test.file' => 'https://www.example.com/test.file', // file in root, starts with slash
'test.file' => 'https://www.example.com/test.file', // file in root, starts without slash
];
foreach ($assertedMappings as $input => $expectedOutput) {
$this->assertEquals($expectedOutput, $this->driver->getPublicUrl($input));
}
}
/**
* @test
*/
public function testDefaultFolderGetter()
{
$this->assertEquals('/', $this->driver->getDefaultFolder());
}
/**
* @test
*/
public function testRootLevelFolderGetter()
{
$this->assertEquals('/', $this->driver->getRootLevelFolder());
}
/**
* @test
*/
public function testGetFileInfoByIdentifier()
{
$fileIdentifier = 'foo/bar/test.file';
$lastModifiedDateTime = new DateTimeResult();
$result = new Result([
'LastModified' => $lastModifiedDateTime,
'ContentType' => 'image/png',
'ContentLength' => 123,
]);
$expectedInfoKeys = ['name', 'identifier', 'ctime', 'mtime', 'extension', 'mimetype', 'size', 'identifier_hash', 'folder_hash', 'storage'];
$this->s3Client->headObject([
'Bucket' => $this->testConfiguration['bucket'],
'Key' => $fileIdentifier
])->willReturn($result);
$info = $this->driver->getFileInfoByIdentifier($fileIdentifier);
$infoKeys = array_keys($info);
sort($infoKeys);
sort($expectedInfoKeys);
$this->assertEquals($expectedInfoKeys, $infoKeys);
$this->assertEquals(basename($fileIdentifier), $info['name']);
$this->assertEquals($fileIdentifier, $info['identifier']);
$this->assertEquals($lastModifiedDateTime->getTimestamp(), $info['ctime']);
$this->assertEquals($lastModifiedDateTime->getTimestamp(), $info['mtime']);
$this->assertEquals(sha1('/' . $fileIdentifier), $info['identifier_hash']);
$this->assertEquals(sha1('/' . dirname($fileIdentifier)), $info['folder_hash']);
$this->assertEquals('file', $info['extension']);
$this->assertEquals('image/png', $info['mimetype']);
$this->assertEquals(123, $info['size']);
$this->assertEquals($this->driver->getStorageUid(), $info['storage']);
}
/**
* @test
*/
public function testGetFileInfoByIdentifierWithLimitedProperties()
{
$fileIdentifier = 'foo/bar/test.file';
$properties = ['name', 'identifier', 'size', 'storage'];
$result = new Result([
'LastModified' => new DateTimeResult(),
'ContentType' => 'image/png',
'ContentLength' => 123,
]);
$this->s3Client->headObject([
'Bucket' => $this->testConfiguration['bucket'],
'Key' => $fileIdentifier
])->willReturn($result);
$info = $this->driver->getFileInfoByIdentifier($fileIdentifier, $properties);
$infoKeys = array_keys($info);
sort($infoKeys);
sort($properties);
$this->assertEquals($properties, $infoKeys);
}
/**
* @test
*/
public function testGetFileInfoByIdentifierWithPseudoMimeType()
{
$fileIdentifier = 'foo/bar/test.youtube';
$lastModifiedDateTime = new DateTimeResult();
$result = new Result([
'LastModified' => $lastModifiedDateTime,
'ContentType' => 'text/plain',
'ContentLength' => 12345,
]);
$expectedInfoKeys = ['name', 'identifier', 'ctime', 'mtime', 'extension', 'mimetype', 'size', 'identifier_hash', 'folder_hash', 'storage'];
$this->s3Client->headObject([
'Bucket' => $this->testConfiguration['bucket'],
'Key' => $fileIdentifier
])->willReturn($result);
$info = $this->driver->getFileInfoByIdentifier($fileIdentifier);
$infoKeys = array_keys($info);
sort($infoKeys);
sort($expectedInfoKeys);
$this->assertEquals($expectedInfoKeys, $infoKeys);
$this->assertEquals(basename($fileIdentifier), $info['name']);
$this->assertEquals($fileIdentifier, $info['identifier']);
$this->assertEquals($lastModifiedDateTime->getTimestamp(), $info['ctime']);
$this->assertEquals($lastModifiedDateTime->getTimestamp(), $info['mtime']);
$this->assertEquals(sha1('/' . $fileIdentifier), $info['identifier_hash']);
$this->assertEquals(sha1('/' . dirname($fileIdentifier)), $info['folder_hash']);
$this->assertEquals('youtube', $info['extension']);
$this->assertEquals('video/youtube', $info['mimetype']);
$this->assertEquals(12345, $info['size']);
$this->assertEquals($this->driver->getStorageUid(), $info['storage']);
}
}