summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/tests
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/tests/phpunit/tests')
-rw-r--r--www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema1Test.php51
-rw-r--r--www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema2Test.php48
-rw-r--r--www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchemaTest.sql18
-rw-r--r--www/wiki/tests/phpunit/tests/MediaWikiTestCaseTest.php184
4 files changed, 301 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema1Test.php b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema1Test.php
new file mode 100644
index 00000000..d794d131
--- /dev/null
+++ b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema1Test.php
@@ -0,0 +1,51 @@
+<?php
+use Wikimedia\Rdbms\IMaintainableDatabase;
+
+/**
+ * @covers MediaWikiTestCase
+ *
+ * @group Database
+ * @group MediaWikiTestCaseTest
+ */
+class MediaWikiTestCaseSchema1Test extends MediaWikiTestCase {
+
+ public static $hasRun = false;
+
+ public function getSchemaOverrides( IMaintainableDatabase $db ) {
+ return [
+ 'create' => [ 'MediaWikiTestCaseTestTable', 'imagelinks' ],
+ 'drop' => [ 'oldimage' ],
+ 'alter' => [ 'pagelinks' ],
+ 'scripts' => [ __DIR__ . '/MediaWikiTestCaseSchemaTest.sql' ]
+ ];
+ }
+
+ public function testMediaWikiTestCaseSchemaTestOrder() {
+ // The test must be run before the second test
+ self::$hasRun = true;
+ $this->assertTrue( self::$hasRun );
+ }
+
+ public function testTableWasCreated() {
+ // Make sure MediaWikiTestCaseTestTable was created.
+ $this->assertTrue( $this->db->tableExists( 'MediaWikiTestCaseTestTable' ) );
+ }
+
+ public function testTableWasDropped() {
+ // Make sure oldimage was dropped
+ $this->assertFalse( $this->db->tableExists( 'oldimage' ) );
+ }
+
+ public function testTableWasOverriden() {
+ // Make sure imagelinks was overwritten
+ $this->assertTrue( $this->db->tableExists( 'imagelinks' ) );
+ $this->assertTrue( $this->db->fieldExists( 'imagelinks', 'il_frobnitz' ) );
+ }
+
+ public function testTableWasAltered() {
+ // Make sure pagelinks was altered
+ $this->assertTrue( $this->db->tableExists( 'pagelinks' ) );
+ $this->assertTrue( $this->db->fieldExists( 'pagelinks', 'pl_frobnitz' ) );
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema2Test.php b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema2Test.php
new file mode 100644
index 00000000..5464dc43
--- /dev/null
+++ b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchema2Test.php
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * @covers MediaWikiTestCase
+ *
+ * @group Database
+ * @group MediaWikiTestCaseTest
+ *
+ * This test is intended to be executed AFTER MediaWikiTestCaseSchema1Test to ensure
+ * that any schema modifications have been cleaned up between test cases.
+ * As there seems to be no way to force execution order, we currently rely on
+ * test classes getting run in anpha-numerical order.
+ * Order is checked by the testMediaWikiTestCaseSchemaTestOrder test in both classes.
+ */
+class MediaWikiTestCaseSchema2Test extends MediaWikiTestCase {
+
+ public function testMediaWikiTestCaseSchemaTestOrder() {
+ // The first test must have run before this one
+ $this->assertTrue( MediaWikiTestCaseSchema1Test::$hasRun );
+ }
+
+ public function testCreatedTableWasRemoved() {
+ // Make sure MediaWikiTestCaseTestTable created by MediaWikiTestCaseSchema1Test
+ // was dropped before executing MediaWikiTestCaseSchema2Test.
+ $this->assertFalse( $this->db->tableExists( 'MediaWikiTestCaseTestTable' ) );
+ }
+
+ public function testDroppedTableWasRestored() {
+ // Make sure oldimage that was dropped by MediaWikiTestCaseSchema1Test
+ // was restored before executing MediaWikiTestCaseSchema2Test.
+ $this->assertTrue( $this->db->tableExists( 'oldimage' ) );
+ }
+
+ public function testOverridenTableWasRestored() {
+ // Make sure imagelinks overwritten by MediaWikiTestCaseSchema1Test
+ // was restored to the original schema before executing MediaWikiTestCaseSchema2Test.
+ $this->assertTrue( $this->db->tableExists( 'imagelinks' ) );
+ $this->assertFalse( $this->db->fieldExists( 'imagelinks', 'il_frobnitz' ) );
+ }
+
+ public function testAlteredTableWasRestored() {
+ // Make sure pagelinks altered by MediaWikiTestCaseSchema1Test
+ // was restored to the original schema before executing MediaWikiTestCaseSchema2Test.
+ $this->assertTrue( $this->db->tableExists( 'pagelinks' ) );
+ $this->assertFalse( $this->db->fieldExists( 'pagelinks', 'pl_frobnitz' ) );
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchemaTest.sql b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchemaTest.sql
new file mode 100644
index 00000000..e2818b55
--- /dev/null
+++ b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseSchemaTest.sql
@@ -0,0 +1,18 @@
+CREATE TABLE /*_*/MediaWikiTestCaseTestTable (
+ id INT NOT NULL,
+ name VARCHAR(20) NOT NULL,
+ PRIMARY KEY (id)
+) /*$wgDBTableOptions*/;
+
+CREATE TABLE /*_*/imagelinks (
+ il_from int NOT NULL DEFAULT 0,
+ il_from_namespace int NOT NULL DEFAULT 0,
+ il_to varchar(127) NOT NULL DEFAULT '',
+ il_frobnitz varchar(127) NOT NULL DEFAULT 'FROB',
+ PRIMARY KEY (il_from,il_to)
+) /*$wgDBTableOptions*/;
+
+ALTER TABLE /*_*/pagelinks
+ADD pl_frobnitz varchar(127) NOT NULL DEFAULT 'FROB';
+
+DROP TABLE /*_*/oldimage;
diff --git a/www/wiki/tests/phpunit/tests/MediaWikiTestCaseTest.php b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseTest.php
new file mode 100644
index 00000000..1850f6fe
--- /dev/null
+++ b/www/wiki/tests/phpunit/tests/MediaWikiTestCaseTest.php
@@ -0,0 +1,184 @@
+<?php
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
+use Psr\Log\LoggerInterface;
+use Wikimedia\Rdbms\LoadBalancer;
+
+/**
+ * @covers MediaWikiTestCase
+ * @group MediaWikiTestCaseTest
+ *
+ * @author Addshore
+ */
+class MediaWikiTestCaseTest extends MediaWikiTestCase {
+
+ private static $startGlobals = [
+ 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
+ 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
+ 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
+ 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
+ ];
+
+ public static function setUpBeforeClass() {
+ parent::setUpBeforeClass();
+ foreach ( self::$startGlobals as $key => $value ) {
+ $GLOBALS[$key] = $value;
+ }
+ }
+
+ public static function tearDownAfterClass() {
+ parent::tearDownAfterClass();
+ foreach ( self::$startGlobals as $key => $value ) {
+ unset( $GLOBALS[$key] );
+ }
+ }
+
+ public function provideExistingKeysAndNewValues() {
+ $providedArray = [];
+ foreach ( array_keys( self::$startGlobals ) as $key ) {
+ $providedArray[] = [ $key, 'newValue' ];
+ $providedArray[] = [ $key, [ 'newValue' ] ];
+ }
+ return $providedArray;
+ }
+
+ /**
+ * @dataProvider provideExistingKeysAndNewValues
+ *
+ * @covers MediaWikiTestCase::setMwGlobals
+ * @covers MediaWikiTestCase::tearDown
+ */
+ public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
+ $this->setMwGlobals( $globalKey, $newValue );
+ $this->assertEquals(
+ $newValue,
+ $GLOBALS[$globalKey],
+ 'Global failed to correctly set'
+ );
+
+ $this->tearDown();
+
+ $this->assertEquals(
+ self::$startGlobals[$globalKey],
+ $GLOBALS[$globalKey],
+ 'Global failed to be restored on tearDown'
+ );
+ }
+
+ /**
+ * @dataProvider provideExistingKeysAndNewValues
+ *
+ * @covers MediaWikiTestCase::stashMwGlobals
+ * @covers MediaWikiTestCase::tearDown
+ */
+ public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
+ $this->stashMwGlobals( $globalKey );
+ $GLOBALS[$globalKey] = $newValue;
+ $this->assertEquals(
+ $newValue,
+ $GLOBALS[$globalKey],
+ 'Global failed to correctly set'
+ );
+
+ $this->tearDown();
+
+ $this->assertEquals(
+ self::$startGlobals[$globalKey],
+ $GLOBALS[$globalKey],
+ 'Global failed to be restored on tearDown'
+ );
+ }
+
+ /**
+ * @covers MediaWikiTestCase::stashMwGlobals
+ * @covers MediaWikiTestCase::tearDown
+ */
+ public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
+ $globalKey = 'abcdefg1234567';
+ $this->setMwGlobals( $globalKey, true );
+ $this->assertTrue(
+ $GLOBALS[$globalKey],
+ 'Global failed to correctly set'
+ );
+
+ $this->tearDown();
+
+ $this->assertFalse(
+ isset( $GLOBALS[$globalKey] ),
+ 'Global failed to be correctly unset'
+ );
+ }
+
+ public function testOverrideMwServices() {
+ $initialServices = MediaWikiServices::getInstance();
+
+ $this->overrideMwServices();
+ $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
+
+ $this->tearDown();
+ $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
+ }
+
+ public function testSetService() {
+ $initialServices = MediaWikiServices::getInstance();
+ $initialService = $initialServices->getDBLoadBalancer();
+ $mockService = $this->getMockBuilder( LoadBalancer::class )
+ ->disableOriginalConstructor()->getMock();
+
+ $this->setService( 'DBLoadBalancer', $mockService );
+ $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
+ $this->assertNotSame(
+ $initialService,
+ MediaWikiServices::getInstance()->getDBLoadBalancer()
+ );
+ $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
+
+ $this->tearDown();
+ $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
+ $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
+ $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
+ }
+
+ /**
+ * @covers MediaWikiTestCase::setLogger
+ * @covers MediaWikiTestCase::restoreLoggers
+ */
+ public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
+ $logger1 = LoggerFactory::getInstance( 'foo' );
+ $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
+ $logger2 = LoggerFactory::getInstance( 'foo' );
+ $this->tearDown();
+ $logger3 = LoggerFactory::getInstance( 'foo' );
+
+ $this->assertSame( $logger1, $logger3 );
+ $this->assertNotSame( $logger1, $logger2 );
+ }
+
+ /**
+ * @covers MediaWikiTestCase::setLogger
+ * @covers MediaWikiTestCase::restoreLoggers
+ */
+ public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
+ $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
+ $logger1 = LoggerFactory::getInstance( 'foo' );
+ $this->tearDown();
+ $logger2 = LoggerFactory::getInstance( 'foo' );
+
+ $this->assertNotSame( $logger1, $logger2 );
+ $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
+ }
+
+ /**
+ * @covers MediaWikiTestCase::setLogger
+ * @covers MediaWikiTestCase::restoreLoggers
+ */
+ public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
+ $logger1 = LoggerFactory::getInstance( 'baz' );
+ $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
+ $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
+ $this->tearDown();
+ $logger2 = LoggerFactory::getInstance( 'baz' );
+
+ $this->assertSame( $logger1, $logger2 );
+ }
+}