Add editing own JSON to editmyoptions grant
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / SqlBlobStoreTest.php
index 12d8119..a40f09c 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace MediaWiki\Tests\Storage;
 
+use InvalidArgumentException;
 use Language;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Storage\SqlBlobStore;
@@ -37,8 +38,8 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::getCompressBlobs()
-        * @covers SqlBlobStore::setCompressBlobs()
+        * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
+        * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
         */
        public function testGetSetCompressRevisions() {
                $store = $this->getBlobStore();
@@ -48,9 +49,9 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::getLegacyEncoding()
-        * @covers SqlBlobStore::getLegacyEncodingConversionLang()
-        * @covers SqlBlobStore::setLegacyEncoding()
+        * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
+        * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
+        * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
         */
        public function testGetSetLegacyEncoding() {
                $store = $this->getBlobStore();
@@ -63,8 +64,8 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::getCacheExpiry()
-        * @covers SqlBlobStore::setCacheExpiry()
+        * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
+        * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
         */
        public function testGetSetCacheExpiry() {
                $store = $this->getBlobStore();
@@ -74,8 +75,8 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::getUseExternalStore()
-        * @covers SqlBlobStore::setUseExternalStore()
+        * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
+        * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
         */
        public function testGetSetUseExternalStore() {
                $store = $this->getBlobStore();
@@ -85,9 +86,9 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        public function provideDecompress() {
-               yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
                yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
                yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
+               yield '(no legacy encoding), error flag -> false' => [ false, 'X', [ 'error' ], false ];
                yield '(no legacy encoding), string in with gzip flag returns string' => [
                        // gzip string below generated with gzdeflate( 'AAAABBAAA' )
                        false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
@@ -112,33 +113,45 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
                ];
                yield '(ISO-8859-1 encoding), string in string out' => [
                        'ISO-8859-1',
-                       iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
+                       iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
                        [],
                        '1®Àþ1',
                ];
                yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
                        'ISO-8859-1',
-                       gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
+                       gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
                        [ 'gzip' ],
                        '4®Àþ4',
                ];
                yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
                        'ISO-8859-1',
-                       serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
+                       serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
                        [ 'object' ],
                        '3®Àþ3',
                ];
                yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
                        'ISO-8859-1',
-                       gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
+                       gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
                        [ 'gzip', 'object' ],
                        '2®Àþ2',
                ];
+               yield 'T184749 (windows-1252 encoding), string in string out' => [
+                       'windows-1252',
+                       iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
+                       [],
+                       'sammansättningar',
+               ];
+               yield 'T184749 (windows-1252 encoding), string in string out with gzip' => [
+                       'windows-1252',
+                       gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
+                       [ 'gzip' ],
+                       'sammansättningar',
+               ];
        }
 
        /**
         * @dataProvider provideDecompress
-        * @covers SqlBlobStore::decompressData
+        * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
         *
         * @param string|bool $legacyEncoding
         * @param mixed $data
@@ -154,7 +167,17 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::compressData
+        * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
+        */
+       public function testDecompressData_InvalidArgumentException() {
+               $store = $this->getBlobStore();
+
+               $this->setExpectedException( InvalidArgumentException::class );
+               $store->decompressData( false, [] );
+       }
+
+       /**
+        * @covers \MediaWiki\Storage\SqlBlobStore::compressData
         */
        public function testCompressRevisionTextUtf8() {
                $store = $this->getBlobStore();
@@ -170,7 +193,7 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers SqlBlobStore::compressData
+        * @covers \MediaWiki\Storage\SqlBlobStore::compressData
         */
        public function testCompressRevisionTextUtf8Gzip() {
                $store = $this->getBlobStore( false, true );
@@ -190,12 +213,13 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
        public function provideBlobs() {
                yield [ '' ];
                yield [ 'someText' ];
+               yield [ "sammansättningar" ];
        }
 
        /**
         * @dataProvider provideBlobs
-        * @covers SqlBlobStore::storeBlob
-        * @covers SqlBlobStore::getBlob
+        * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
+        * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
         */
        public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
                $store = $this->getBlobStore();
@@ -203,4 +227,64 @@ class SqlBlobStoreTest extends MediaWikiTestCase {
                $this->assertSame( $blob, $store->getBlob( $address ) );
        }
 
+       /**
+        * @dataProvider provideBlobs
+        * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
+        * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
+        */
+       public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
+               $store = $this->getBlobStore( 'windows-1252' );
+               $address = $store->storeBlob( $blob );
+               $this->assertSame( $blob, $store->getBlob( $address ) );
+       }
+
+       /**
+        * @dataProvider provideBlobs
+        * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
+        * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
+        */
+       public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
+               // FIXME: fails under postgres
+               $this->markTestSkippedIfDbType( 'postgres' );
+               $store = $this->getBlobStore( 'windows-1252', true );
+               $address = $store->storeBlob( $blob );
+               $this->assertSame( $blob, $store->getBlob( $address ) );
+       }
+
+       public function provideGetTextIdFromAddress() {
+               yield [ 'tt:17', 17 ];
+               yield [ 'xy:17', null ];
+               yield [ 'xy:xyzzy', null ];
+       }
+
+       /**
+        * @dataProvider provideGetTextIdFromAddress
+        */
+       public function testGetTextIdFromAddress( $address, $textId ) {
+               $store = $this->getBlobStore();
+               $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
+       }
+
+       public function provideGetTextIdFromAddressInvalidArgumentException() {
+               yield [ 'tt:-17' ];
+               yield [ 'tt:xy' ];
+               yield [ 'tt:0' ];
+               yield [ 'tt:' ];
+               yield [ 'xy' ];
+               yield [ '' ];
+       }
+
+       /**
+        * @dataProvider provideGetTextIdFromAddressInvalidArgumentException
+        */
+       public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
+               $this->setExpectedException( InvalidArgumentException::class );
+               $store = $this->getBlobStore();
+               $store->getTextIdFromAddress( $address );
+       }
+
+       public function testMakeAddressFromTextId() {
+               $this->assertSame( 'tt:17', SqlBlobStore::makeAddressFromTextId( 17 ) );
+       }
+
 }