rdbms: add another null db/schema sanity check to DatabaseDomain
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseDomainTest.php
index b2e7155..b1d4fad 100644 (file)
@@ -13,7 +13,7 @@ class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
        public static function provideConstruct() {
                return [
                        'All strings' =>
-                               [ 'foo', 'bar', 'baz', 'foo-bar-baz' ],
+                               [ 'foo', 'bar', 'baz_', 'foo-bar-baz_' ],
                        'Nothing' =>
                                [ null, null, '', '' ],
                        'Invalid $database' =>
@@ -23,9 +23,9 @@ class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
                        'Invalid $prefix' =>
                                [ 'foo', 'bar', 0, '', true ],
                        'Dash' =>
-                               [ 'foo-bar', 'baz', 'baa', 'foo?hbar-baz-baa' ],
+                               [ 'foo-bar', 'baz', 'baa_', 'foo?hbar-baz-baa_' ],
                        'Question mark' =>
-                               [ 'foo?bar', 'baz', 'baa', 'foo??bar-baz-baa' ],
+                               [ 'foo?bar', 'baz', 'baa_', 'foo??bar-baz-baa_' ],
                ];
        }
 
@@ -53,17 +53,17 @@ class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
                        'Basic' =>
                                [ 'foo', 'foo', null, '' ],
                        'db+prefix' =>
-                               [ 'foo-bar', 'foo', null, 'bar' ],
+                               [ 'foo-bar_', 'foo', null, 'bar_' ],
                        'db+schema+prefix' =>
-                               [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
+                               [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
                        '?h -> -' =>
-                               [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
+                               [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
                        '?? -> ?' =>
-                               [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
+                               [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
                        '? is left alone' =>
-                               [ 'foo?bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
+                               [ 'foo?bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
                        'too many parts' =>
-                               [ 'foo-bar-baz-baa', '', '', '', true ],
+                               [ 'foo-bar-baz-baa_', '', '', '', true ],
                        'from instance' =>
                                [ DatabaseDomain::newUnspecified(), null, null, '' ],
                ];
@@ -90,13 +90,13 @@ class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
                        'Basic' =>
                                [ 'foo', 'foo', null, '' ],
                        'db+prefix' =>
-                               [ 'foo-bar', 'foo', null, 'bar' ],
+                               [ 'foo-bar_', 'foo', null, 'bar_' ],
                        'db+schema+prefix' =>
-                               [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
+                               [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
                        '?h -> -' =>
-                               [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
+                               [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
                        '?? -> ?' =>
-                               [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
+                               [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
                        'Nothing' =>
                                [ '', null, null, '' ],
                ];
@@ -130,4 +130,97 @@ class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
                $this->assertSame( null, $domain->getSchema() );
                $this->assertSame( '', $domain->getTablePrefix() );
        }
+
+       public static function provideIsCompatible() {
+               return [
+                       'Basic' =>
+                               [ 'foo', 'foo', null, '', true ],
+                       'db+prefix' =>
+                               [ 'foo-bar_', 'foo', null, 'bar_', true ],
+                       'db+schema+prefix' =>
+                               [ 'foo-bar-baz_', 'foo', 'bar', 'baz_', true ],
+                       'db+dontcare_schema+prefix' =>
+                               [ 'foo-bar-baz_', 'foo', null, 'baz_', false ],
+                       '?h -> -' =>
+                               [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_', true ],
+                       '?? -> ?' =>
+                               [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_', true ],
+                       'Nothing' =>
+                               [ '', null, null, '', true ],
+                       'dontcaredb+dontcaredbschema+prefix' =>
+                               [ 'mywiki-mediawiki-prefix_', null, null, 'prefix_', false ],
+                       'db+dontcareschema+prefix' =>
+                               [ 'mywiki-schema-prefix_', 'mywiki', null, 'prefix_', false ],
+                       'postgres-db-jobqueue' =>
+                               [ 'postgres-mediawiki-', 'postgres', null, '', false ]
+               ];
+       }
+
+       /**
+        * @dataProvider provideIsCompatible
+        * @covers Wikimedia\Rdbms\DatabaseDomain::isCompatible
+        */
+       public function testIsCompatible( $id, $db, $schema, $prefix, $transitive ) {
+               $compareIdObj = DatabaseDomain::newFromId( $id );
+               $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
+
+               $fromId = new DatabaseDomain( $db, $schema, $prefix );
+
+               $this->assertTrue( $fromId->isCompatible( $id ), 'constructed equals string' );
+               $this->assertTrue( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
+
+               $this->assertEquals( $transitive, $compareIdObj->isCompatible( $fromId ),
+                       'test transitivity of nulls components' );
+       }
+
+       public static function provideIsCompatible2() {
+               return [
+                       'db+schema+prefix' =>
+                               [ 'mywiki-schema-prefix_', 'thatwiki', 'schema', 'prefix_' ],
+                       'dontcaredb+dontcaredbschema+prefix' =>
+                               [ 'thatwiki-mediawiki-otherprefix_', null, null, 'prefix_' ],
+                       'db+dontcareschema+prefix' =>
+                               [ 'notmywiki-schema-prefix_', 'mywiki', null, 'prefix_' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideIsCompatible2
+        * @covers Wikimedia\Rdbms\DatabaseDomain::isCompatible
+        */
+       public function testIsCompatible2( $id, $db, $schema, $prefix ) {
+               $compareIdObj = DatabaseDomain::newFromId( $id );
+               $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
+
+               $fromId = new DatabaseDomain( $db, $schema, $prefix );
+
+               $this->assertFalse( $fromId->isCompatible( $id ), 'constructed equals string' );
+               $this->assertFalse( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
+       }
+
+       /**
+        * @expectedException InvalidArgumentException
+        */
+       public function testSchemaWithNoDB1() {
+               new DatabaseDomain( null, 'schema', '' );
+       }
+
+       /**
+        * @expectedException InvalidArgumentException
+        */
+       public function testSchemaWithNoDB2() {
+               DatabaseDomain::newFromId( '-schema-prefix' );
+       }
+
+       /**
+        * @covers Wikimedia\Rdbms\DatabaseDomain::isUnspecified
+        */
+       public function testIsUnspecified() {
+               $domain = new DatabaseDomain( null, null, '' );
+               $this->assertTrue( $domain->isUnspecified() );
+               $domain = new DatabaseDomain( 'mywiki', null, '' );
+               $this->assertFalse( $domain->isUnspecified() );
+               $domain = new DatabaseDomain( 'mywiki', null, '' );
+               $this->assertFalse( $domain->isUnspecified() );
+       }
 }