-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFlysystemTest.php
More file actions
164 lines (144 loc) · 5.37 KB
/
FlysystemTest.php
File metadata and controls
164 lines (144 loc) · 5.37 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
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Tests\Core\IO\FilePathNormalizer;
use Ibexa\Core\IO\FilePathNormalizer\Flysystem;
use Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
use League\Flysystem\WhitespacePathNormalizer;
use PHPUnit\Framework\TestCase;
final class FlysystemTest extends TestCase
{
/** @var \Ibexa\Core\IO\FilePathNormalizer\Flysystem */
private $filePathNormalizer;
/** @var \Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter|\PHPUnit\Framework\MockObject\MockObject */
private $slugConverter;
public function setUp(): void
{
$this->slugConverter = $this->createMock(SlugConverter::class);
$this->filePathNormalizer = new Flysystem($this->slugConverter, new WhitespacePathNormalizer());
}
/**
* @dataProvider providerForTestNormalizePath
*/
public function testNormalizePath(
string $originalPath,
string $fileName,
string $sluggedFileName,
string $regex,
bool $doHash
): void {
$this->slugConverter
->expects(self::once())
->method('convert')
->with($fileName)
->willReturn($sluggedFileName);
$normalizedPath = $this->filePathNormalizer->normalizePath($originalPath, $doHash);
self::assertStringEndsWith($sluggedFileName, $normalizedPath);
self::assertRegExp($regex, $normalizedPath);
}
public function providerForTestNormalizePath(): array
{
$defaultPattern = '/\/[0-9a-f]{12}-';
return [
'No special chars' => [
'4/3/2/234/1/image.jpg',
'image.jpg',
'image.jpg',
$defaultPattern . 'image.jpg/',
true,
],
'Spaces in the filename' => [
'4/3/2/234/1/image with spaces.jpg',
'image with spaces.jpg',
'image-with-spaces.jpg',
$defaultPattern . 'image-with-spaces.jpg/',
true,
],
'Encoded spaces in the name' => [
'4/3/2/234/1/image%20+no+spaces.jpg',
'image%20+no+spaces.jpg',
'image-20-nospaces.jpg',
$defaultPattern . 'image-20-nospaces.jpg/',
true,
],
'Special chars in the name' => [
'4/3/2/234/1/image%20+no+spaces?.jpg',
'image%20+no+spaces?.jpg',
'image-20-nospaces.jpg',
$defaultPattern . 'image-20-nospaces.jpg/',
true,
],
'Already hashed name' => [
'4/3/2/234/1/14ff44718877-hashed.jpg',
'14ff44718877-hashed.jpg',
'14ff44718877-hashed.jpg',
'/^4\/3\/2\/234\/1\/14ff44718877-hashed.jpg$/',
true,
],
'No special chars and no hashing' => [
'4/3/2/234/1/image.jpg',
'image.jpg',
'image.jpg',
'/\/image.jpg/',
false,
],
'Special chars and no hashing' => [
'4/3/2/234/1/image%20+no+spaces.jpg',
'image%20+no+spaces.jpg',
'image-20-nospaces.jpg',
'/\/image-20-nospaces.jpg/',
false,
],
'Already hashed name and no hashing' => [
'4/3/2/234/1/14ff44718877-hashed.jpg',
'14ff44718877-hashed.jpg',
'14ff44718877-hashed.jpg',
'/^4\/3\/2\/234\/1\/14ff44718877-hashed.jpg$/',
false,
],
];
}
public function testNormalizePathWithRealFilePath(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'test_image_');
file_put_contents($tempFile, 'test image content');
try {
$md5File = md5_file($tempFile);
self::assertNotFalse($md5File);
$expectedHash = substr($md5File, 0, 12);
$this->slugConverter
->expects(self::once())
->method('convert')
->with('image.jpg')
->willReturn('image.jpg');
$normalizedPath = $this->filePathNormalizer->normalizePath(
'4/3/2/234/1/image.jpg',
true,
$tempFile,
);
self::assertSame('4/3/2/234/1/' . $expectedHash . '-image.jpg', $normalizedPath);
} finally {
unlink($tempFile);
}
}
public function testNormalizePathWithRealFilePathIsDeterministic(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'test_image_');
file_put_contents($tempFile, 'test image content');
try {
$this->slugConverter
->expects(self::exactly(2))
->method('convert')
->with('image.jpg')
->willReturn('image.jpg');
$first = $this->filePathNormalizer->normalizePath('4/3/2/234/1/image.jpg', true, $tempFile);
$second = $this->filePathNormalizer->normalizePath('4/3/2/234/1/image.jpg', true, $tempFile);
self::assertSame($first, $second);
} finally {
unlink($tempFile);
}
}
}