X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Ffilebackend%2FFileBackendTest.php;h=e115d17c157b7482e5a36545f102ba7372a74449;hb=fbab4fecef8dcd4010d5c81736cccb565bf218da;hp=2e4942f0d838df655dc2920e6bdf6d998ec08be2;hpb=c6bcef30ae0e56d379d34cf9d96e6593238a0341;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/filebackend/FileBackendTest.php b/tests/phpunit/includes/filebackend/FileBackendTest.php index 2e4942f0d8..e115d17c15 100644 --- a/tests/phpunit/includes/filebackend/FileBackendTest.php +++ b/tests/phpunit/includes/filebackend/FileBackendTest.php @@ -18,7 +18,6 @@ class FileBackendTest extends MediaWikiTestCase { protected function setUp() { global $wgFileBackends; parent::setUp(); - $uniqueId = time() . '-' . mt_rand(); $tmpDir = $this->getNewTempDirectory(); if ( $this->getCliArg( 'use-filebackend' ) ) { if ( self::$backendToUse ) { @@ -58,7 +57,7 @@ class FileBackendTest extends MediaWikiTestCase { 'name' => 'localtesting', 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ), 'parallelize' => 'implicit', - 'wikiId' => wfWikiId() . $uniqueId, + 'wikiId' => wfWikiId() . wfRandomString(), 'backends' => array( array( 'name' => 'localmultitesting1', @@ -80,6 +79,11 @@ class FileBackendTest extends MediaWikiTestCase { ) ); } + protected function tearDown() { + parent::tearDown(); + DeferredUpdates::forceDeferral( false ); + } + private static function baseStorePath() { return 'mwstore://localtesting'; } @@ -1517,7 +1521,7 @@ class FileBackendTest extends MediaWikiTestCase { array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ), array( "$base/unittest-cont2/a/z/some_file2.txt", true ), # Specific to FS backend with no basePath field set - #array( "$base/unittest-cont3/a/z/some_file3.txt", false ), + # array( "$base/unittest-cont3/a/z/some_file3.txt", false ), ); } @@ -1711,7 +1715,7 @@ class FileBackendTest extends MediaWikiTestCase { $this->assertEquals( strlen( $fileBContents ), $this->backend->getFileSize( array( 'src' => $fileC ) ), "Correct file size of $fileC" ); - $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ), + $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ), $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ), "Correct file SHA-1 of $fileC" ); } @@ -1810,7 +1814,7 @@ class FileBackendTest extends MediaWikiTestCase { $this->assertEquals( strlen( $fileBContents ), $this->backend->getFileSize( array( 'src' => $fileC ) ), "Correct file size of $fileC" ); - $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ), + $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ), $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ), "Correct file SHA-1 of $fileC" ); } @@ -1887,7 +1891,7 @@ class FileBackendTest extends MediaWikiTestCase { $this->assertEquals( strlen( $fileBContents ), $this->backend->getFileSize( array( 'src' => $fileA ) ), "Correct file size of $fileA" ); - $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ), + $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ), $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ), "Correct file SHA-1 of $fileA" ); } @@ -2347,7 +2351,7 @@ class FileBackendTest extends MediaWikiTestCase { $this->assertEquals( true, $status->isOK(), "Locking of files succeeded with OK status ($backendName) ($i)." ); - ## Flip the acquire/release ordering around ## + # # Flip the acquire/release ordering around ## $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH ); $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ), @@ -2392,6 +2396,42 @@ class FileBackendTest extends MediaWikiTestCase { "Scoped unlocking of files succeeded with OK status ($backendName)." ); } + /** + * @dataProvider provider_testGetContentType + */ + public function testGetContentType( $mimeCallback, $mimeFromString ) { + global $IP; + + $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend( + array( + 'name' => 'testing', + 'class' => 'MemoryFileBackend', + 'wikiId' => 'meow', + 'mimeCallback' => $mimeCallback + ) + ) ); + + $dst = 'mwstore://testing/container/path/to/file_no_ext'; + $src = "$IP/tests/phpunit/data/media/srgb.jpg"; + $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) ); + $this->assertEquals( + $mimeFromString ? 'image/jpeg' : 'unknown/unknown', + $be->getContentType( $dst, file_get_contents( $src ), null ) ); + + $src = "$IP/tests/phpunit/data/media/Png-native-test.png"; + $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) ); + $this->assertEquals( + $mimeFromString ? 'image/png' : 'unknown/unknown', + $be->getContentType( $dst, file_get_contents( $src ), null ) ); + } + + public static function provider_testGetContentType() { + return array( + array( null, false ), + array( array( FileBackendGroup::singleton(), 'guessMimeInternal' ), true ) + ); + } + public function testReadAffinity() { $be = TestingAccessWrapper::newFromObject( new FileBackendMultiWrite( array( @@ -2442,6 +2482,85 @@ class FileBackendTest extends MediaWikiTestCase { ); } + public function testAsyncWrites() { + $be = TestingAccessWrapper::newFromObject( + new FileBackendMultiWrite( array( + 'name' => 'localtesting', + 'wikiId' => wfWikiId() . mt_rand(), + 'backends' => array( + array( // backend 0 + 'name' => 'multitesting0', + 'class' => 'MemoryFileBackend', + 'isMultiMaster' => false + ), + array( // backend 1 + 'name' => 'multitesting1', + 'class' => 'MemoryFileBackend', + 'isMultiMaster' => true + ) + ), + 'replication' => 'async' + ) ) + ); + + DeferredUpdates::forceDeferral( true ); + + $p = 'container/test-cont/file.txt'; + $be->quickCreate( array( + 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ) ); + + $this->assertEquals( + false, + $be->backends[0]->getFileContents( array( 'src' => "mwstore://multitesting0/$p" ) ), + "File not yet written to backend 0" + ); + $this->assertEquals( + 'cattitude', + $be->backends[1]->getFileContents( array( 'src' => "mwstore://multitesting1/$p" ) ), + "File already written to backend 1" + ); + + DeferredUpdates::doUpdates(); + DeferredUpdates::forceDeferral( false ); + + $this->assertEquals( + 'cattitude', + $be->backends[0]->getFileContents( array( 'src' => "mwstore://multitesting0/$p" ) ), + "File now written to backend 0" + ); + } + + public function testSanitizeOpHeaders() { + $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend( array( + 'name' => 'localtesting', + 'wikiId' => wfWikiID() + ) ) ); + + $name = wfRandomString( 300 ); + + $input = array( + 'headers' => array( + 'content-Disposition' => FileBackend::makeContentDisposition( 'inline', $name ), + 'Content-dUration' => 25.6, + 'X-LONG-VALUE' => str_pad( '0', 300 ), + 'CONTENT-LENGTH' => 855055, + ) + ); + $expected = array( + 'headers' => array( + 'content-disposition' => FileBackend::makeContentDisposition( 'inline', $name ), + 'content-duration' => 25.6, + 'content-length' => 855055 + ) + ); + + MediaWiki\suppressWarnings(); + $actual = $be->sanitizeOpHeaders( $input ); + MediaWiki\restoreWarnings(); + + $this->assertEquals( $expected, $actual, "Header sanitized properly" ); + } + // helper function private function listToArray( $iter ) { return is_array( $iter ) ? $iter : iterator_to_array( $iter );