Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / filebackend / SwiftFileBackendTest.php
blobf9e420069cb31810a1e18500aa61a4f43eb63dbd
1 <?php
3 use MediaWiki\Logger\LoggerFactory;
4 use Wikimedia\FileBackend\FileBackend;
5 use Wikimedia\FileBackend\FileBackendError;
6 use Wikimedia\FileBackend\SwiftFileBackend;
7 use Wikimedia\TestingAccessWrapper;
9 /**
10 * @group FileRepo
11 * @group FileBackend
12 * @group medium
14 * @covers \Wikimedia\FileBackend\SwiftFileBackend
15 * @covers \Wikimedia\FileBackend\FileIteration\SwiftFileBackendDirList
16 * @covers \Wikimedia\FileBackend\FileIteration\SwiftFileBackendFileList
17 * @covers \Wikimedia\FileBackend\FileIteration\SwiftFileBackendList
19 class SwiftFileBackendTest extends MediaWikiIntegrationTestCase {
20 /** @var TestingAccessWrapper|SwiftFileBackend */
21 private $backend;
23 protected function setUp(): void {
24 parent::setUp();
26 $this->backend = TestingAccessWrapper::newFromObject(
27 new SwiftFileBackend( [
28 'name' => 'local-swift-testing',
29 'class' => SwiftFileBackend::class,
30 'wikiId' => 'unit-testing',
31 'lockManager' => $this->getServiceContainer()->getLockManagerGroupFactory()
32 ->getLockManagerGroup()->get( 'fsLockManager' ),
33 'swiftAuthUrl' => 'http://127.0.0.1:8080/auth', // unused
34 'swiftUser' => 'test:tester',
35 'swiftKey' => 'testing',
36 'swiftTempUrlKey' => 'b3968d0207b54ece87cccc06515a89d4', // unused
37 'logger' => LoggerFactory::getInstance( 'FileOperation' )
38 ] )
42 /**
43 * @dataProvider provider_testExtractPostableContentHeaders
45 public function testExtractPostableContentHeaders( $raw, $sanitized ) {
46 $hdrs = $this->backend->extractMutableContentHeaders( $raw );
48 $this->assertEquals( $sanitized, $hdrs, 'Correct extractPostableContentHeaders() result' );
51 public static function provider_testExtractPostableContentHeaders() {
52 return [
53 'empty' => [
54 [],
59 'content-length' => 345,
60 'content-type' => 'image+bitmap/jpeg',
61 'content-disposition' => 'inline',
62 'content-duration' => 35.6363,
63 'content-Custom' => 'hello',
64 'x-content-custom' => 'hello'
67 'content-type' => 'image+bitmap/jpeg',
68 'content-disposition' => 'inline',
69 'content-duration' => 35.6363,
70 'content-custom' => 'hello',
71 'x-content-custom' => 'hello'
76 'content-length' => 345,
77 'content-type' => 'image+bitmap/jpeg',
78 'content-Disposition' => 'inline; filename=xxx; ' . str_repeat( 'o', 1024 ),
79 'content-duration' => 35.6363,
80 'content-custom' => 'hello',
81 'x-content-custom' => 'hello'
84 'content-type' => 'image+bitmap/jpeg',
85 'content-disposition' => 'inline; filename=xxx',
86 'content-duration' => 35.6363,
87 'content-custom' => 'hello',
88 'x-content-custom' => 'hello'
93 'content-length' => 345,
94 'content-type' => 'image+bitmap/jpeg',
95 'content-disposition' => 'filename=' . str_repeat( 'o', 1024 ) . ';inline',
96 'content-duration' => 35.6363,
97 'content-custom' => 'hello',
98 'x-content-custom' => 'hello'
101 'content-type' => 'image+bitmap/jpeg',
102 'content-disposition' => '',
103 'content-duration' => 35.6363,
104 'content-custom' => 'hello',
105 'x-content-custom' => 'hello'
110 'x-delete-at' => 'non numeric',
111 'x-delete-after' => 'non numeric',
112 'x-content-custom' => 'hello'
115 'x-content-custom' => 'hello'
120 'x-delete-at' => '12345',
121 'x-delete-after' => '12345'
124 'x-delete-at' => '12345',
125 'x-delete-after' => '12345'
130 'x-delete-at' => 12345,
131 'x-delete-after' => 12345
134 'x-delete-at' => 12345,
135 'x-delete-after' => 12345
142 * @dataProvider provider_testGetMetadataHeaders
144 public function testGetMetadataHeaders( $raw, $sanitized ) {
145 $hdrs = $this->backend->extractMetadataHeaders( $raw );
147 $this->assertEquals( $sanitized, $hdrs, 'getMetadataHeaders() has unexpected result' );
150 public static function provider_testGetMetadataHeaders() {
151 return [
154 'content-length' => 345,
155 'content-custom' => 'hello',
156 'x-content-custom' => 'hello',
157 'x-object-meta-custom' => 5,
158 'x-object-meta-sha1Base36' => 'a3deadfg...',
161 'x-object-meta-custom' => 5,
162 'x-object-meta-sha1base36' => 'a3deadfg...',
169 * @dataProvider provider_testGetMetadata
171 public function testGetMetadata( $raw, $sanitized ) {
172 $hdrs = $this->backend->getMetadataFromHeaders( $raw );
174 $this->assertEquals( $sanitized, $hdrs, 'getMetadata() has unexpected result' );
177 public static function provider_testGetMetadata() {
178 return [
181 'content-length' => 345,
182 'content-custom' => 'hello',
183 'x-content-custom' => 'hello',
184 'x-object-meta-custom' => 5,
185 'x-object-meta-sha1Base36' => 'a3deadfg...',
188 'custom' => 5,
189 'sha1base36' => 'a3deadfg...',
195 private function setupAuthFailure() {
196 $this->backend->authErrorTimestamp = time();
197 $this->backend->http = null;
200 public function testGetFileStatAuthFail() {
201 $this->setupAuthFailure();
202 $result = $this->backend->getFileStat( [
203 'src' => 'mwstore://local-swift-testing/c/test.txt'
204 ] );
205 $this->assertSame( FileBackend::STAT_ERROR, $result );
208 public function testGetFileContentsAuthFail() {
209 $this->setupAuthFailure();
210 $result = $this->backend->getFileContents( [
211 'src' => 'mwstore://local-swift-testing/c/test.txt'
212 ] );
213 $this->assertFalse( $result );
216 public function testGetLocalCopyAuthFail() {
217 $this->setupAuthFailure();
218 $result = $this->backend->getLocalCopy( [
219 'src' => 'mwstore://local-swift-testing/c/test.txt'
220 ] );
221 $this->assertNull( $result );
224 public function testCreateAuthFail() {
225 $this->setupAuthFailure();
226 $status = $this->backend->create( [
227 'dst' => 'mwstore://local-swift-testing/c/test.txt',
228 'content' => '',
229 ] );
230 // Ideally it would fail with backend-fail-connect, but preloadFileStat()
231 // fails without any way to propagate error details.
232 $this->assertStatusError( 'backend-fail-internal', $status );
235 public function testSecureAuthFail() {
236 $this->setupAuthFailure();
237 $status = $this->backend->secure( [
238 'dir' => 'mwstore://local-swift-testing/c',
239 'noAccess' => true,
240 ] );
241 $this->assertStatusError( 'backend-fail-internal', $status );
244 public function testPrepareAuthFail() {
245 $this->setupAuthFailure();
246 $status = $this->backend->prepare( [
247 'dir' => 'mwstore://local-swift-testing/c',
248 'noAccess' => true,
249 ] );
250 $this->assertStatusError( 'backend-fail-internal', $status );
253 public function testCleanAuthFail() {
254 $this->setupAuthFailure();
255 $status = $this->backend->clean( [
256 'dir' => 'mwstore://local-swift-testing/c',
257 ] );
258 $this->assertStatusError( 'backend-fail-internal', $status );
261 public function testGetFileListAuthFail() {
262 $this->setupAuthFailure();
263 $result = $this->backend->getFileList( [
264 'dir' => 'mwstore://local-swift-testing/c',
265 ] );
266 $this->expectException( FileBackendError::class );
267 iterator_to_array( $result );