123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <?php
- namespace MediaWiki\Tests\Maintenance;
- use Maintenance;
- use MediaWiki\MediaWikiServices;
- use Wikimedia\TestingAccessWrapper;
- /**
- * @covers Maintenance
- */
- class MaintenanceTest extends MaintenanceBaseTestCase {
- /**
- * @see MaintenanceBaseTestCase::getMaintenanceClass
- */
- protected function getMaintenanceClass() {
- return Maintenance::class;
- }
- /**
- * @see MaintenanceBaseTestCase::createMaintenance
- *
- * Note to extension authors looking for a model to follow: This function
- * is normally not needed in a maintenance test, it's only overridden here
- * because Maintenance is abstract.
- */
- protected function createMaintenance() {
- $className = $this->getMaintenanceClass();
- $obj = $this->getMockForAbstractClass( $className );
- return TestingAccessWrapper::newFromObject( $obj );
- }
- // Although the following tests do not seem to be too consistent (compare for
- // example the newlines within the test.*StringString tests, or the
- // test.*Intermittent.* tests), the objective of these tests is not to describe
- // consistent behavior, but rather currently existing behavior.
- /**
- * @dataProvider provideOutputData
- */
- function testOutput( $outputs, $expected, $extraNL ) {
- foreach ( $outputs as $data ) {
- if ( is_array( $data ) ) {
- list( $msg, $channel ) = $data;
- } else {
- $msg = $data;
- $channel = null;
- }
- $this->maintenance->output( $msg, $channel );
- }
- $this->assertOutputPrePostShutdown( $expected, $extraNL );
- }
- public function provideOutputData() {
- return [
- [ [ "" ], "", false ],
- [ [ "foo" ], "foo", false ],
- [ [ "foo", "bar" ], "foobar", false ],
- [ [ "foo\n" ], "foo\n", false ],
- [ [ "foo\n\n" ], "foo\n\n", false ],
- [ [ "foo\nbar" ], "foo\nbar", false ],
- [ [ "foo\nbar\n" ], "foo\nbar\n", false ],
- [ [ "foo\n", "bar\n" ], "foo\nbar\n", false ],
- [ [ "", "foo", "", "\n", "ba", "", "r\n" ], "foo\nbar\n", false ],
- [ [ "", "foo", "", "\nb", "a", "", "r\n" ], "foo\nbar\n", false ],
- [ [ [ "foo", "bazChannel" ] ], "foo", true ],
- [ [ [ "foo\n", "bazChannel" ] ], "foo", true ],
- // If this test fails, note that output takes strings with double line
- // endings (although output's implementation in this situation calls
- // outputChanneled with a string ending in a nl ... which is not allowed
- // according to the documentation of outputChanneled)
- [ [ [ "foo\n\n", "bazChannel" ] ], "foo\n", true ],
- [ [ [ "foo\nbar", "bazChannel" ] ], "foo\nbar", true ],
- [ [ [ "foo\nbar\n", "bazChannel" ] ], "foo\nbar", true ],
- [
- [
- [ "foo\n", "bazChannel" ],
- [ "bar\n", "bazChannel" ],
- ],
- "foobar",
- true
- ],
- [
- [
- [ "", "bazChannel" ],
- [ "foo", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "\n", "bazChannel" ],
- [ "ba", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "r\n", "bazChannel" ],
- ],
- "foobar",
- true
- ],
- [
- [
- [ "", "bazChannel" ],
- [ "foo", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "\nb", "bazChannel" ],
- [ "a", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "r\n", "bazChannel" ],
- ],
- "foo\nbar",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", "bazChannel" ],
- [ "qux", "quuxChannel" ],
- [ "corge", "bazChannel" ],
- ],
- "foobar\nqux\ncorge",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar\n", "bazChannel" ],
- [ "qux\n", "quuxChannel" ],
- [ "corge", "bazChannel" ],
- ],
- "foobar\nqux\ncorge",
- true
- ],
- [
- [
- [ "foo", null ],
- [ "bar", "bazChannel" ],
- [ "qux", null ],
- [ "quux", "bazChannel" ],
- ],
- "foobar\nquxquux",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", null ],
- [ "qux", "bazChannel" ],
- [ "quux", null ],
- ],
- "foo\nbarqux\nquux",
- false
- ],
- [
- [
- [ "foo", 1 ],
- [ "bar", 1.0 ],
- ],
- "foo\nbar",
- true
- ],
- [ [ "foo", "", "bar" ], "foobar", false ],
- [ [ "foo", false, "bar" ], "foobar", false ],
- [
- [
- [ "qux", "quuxChannel" ],
- "foo",
- false,
- "bar"
- ],
- "qux\nfoobar",
- false
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "bar", "bazChannel" ],
- ],
- "foobar",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ false, "bazChannel" ],
- [ "bar", "bazChannel" ],
- ],
- "foobar",
- true
- ],
- ];
- }
- /**
- * @dataProvider provideOutputChanneledData
- */
- function testOutputChanneled( $outputs, $expected, $extraNL ) {
- foreach ( $outputs as $data ) {
- if ( is_array( $data ) ) {
- list( $msg, $channel ) = $data;
- } else {
- $msg = $data;
- $channel = null;
- }
- $this->maintenance->outputChanneled( $msg, $channel );
- }
- $this->assertOutputPrePostShutdown( $expected, $extraNL );
- }
- public function provideOutputChanneledData() {
- return [
- [ [ "" ], "\n", false ],
- [ [ "foo" ], "foo\n", false ],
- [ [ "foo", "bar" ], "foo\nbar\n", false ],
- [ [ "foo\nbar" ], "foo\nbar\n", false ],
- [ [ "", "foo", "", "\nb", "a", "", "r" ], "\nfoo\n\n\nb\na\n\nr\n", false ],
- [ [ [ "foo", "bazChannel" ] ], "foo", true ],
- [
- [
- [ "foo\nbar", "bazChannel" ]
- ],
- "foo\nbar",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", "bazChannel" ],
- ],
- "foobar",
- true
- ],
- [
- [
- [ "", "bazChannel" ],
- [ "foo", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "\nb", "bazChannel" ],
- [ "a", "bazChannel" ],
- [ "", "bazChannel" ],
- [ "r", "bazChannel" ],
- ],
- "foo\nbar",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", "bazChannel" ],
- [ "qux", "quuxChannel" ],
- [ "corge", "bazChannel" ],
- ],
- "foobar\nqux\ncorge",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", "bazChannel" ],
- [ "qux", "quuxChannel" ],
- [ "corge", "bazChannel" ],
- ],
- "foobar\nqux\ncorge",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", null ],
- [ "qux", null ],
- [ "corge", "bazChannel" ],
- ],
- "foo\nbar\nqux\ncorge",
- true
- ],
- [
- [
- [ "foo", null ],
- [ "bar", "bazChannel" ],
- [ "qux", null ],
- [ "quux", "bazChannel" ],
- ],
- "foo\nbar\nqux\nquux",
- true
- ],
- [
- [
- [ "foo", "bazChannel" ],
- [ "bar", null ],
- [ "qux", "bazChannel" ],
- [ "quux", null ],
- ],
- "foo\nbar\nqux\nquux\n",
- false
- ],
- [
- [
- [ "foo", 1 ],
- [ "bar", 1.0 ],
- ],
- "foo\nbar",
- true
- ],
- [ [ "foo", "", "bar" ], "foo\n\nbar\n", false ],
- [ [ "foo", false, "bar" ], "foo\nbar\n", false ],
- ];
- }
- function testCleanupChanneledClean() {
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "", false );
- }
- function testCleanupChanneledAfterOutput() {
- $this->maintenance->output( "foo" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo", false );
- }
- function testCleanupChanneledAfterOutputWNullChannel() {
- $this->maintenance->output( "foo", null );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo", false );
- }
- function testCleanupChanneledAfterOutputWChannel() {
- $this->maintenance->output( "foo", "bazChannel" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterNLOutput() {
- $this->maintenance->output( "foo\n" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterNLOutputWNullChannel() {
- $this->maintenance->output( "foo\n", null );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterNLOutputWChannel() {
- $this->maintenance->output( "foo\n", "bazChannel" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterOutputChanneledWOChannel() {
- $this->maintenance->outputChanneled( "foo" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterOutputChanneledWNullChannel() {
- $this->maintenance->outputChanneled( "foo", null );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testCleanupChanneledAfterOutputChanneledWChannel() {
- $this->maintenance->outputChanneled( "foo", "bazChannel" );
- $this->maintenance->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\n", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutput() {
- $m2 = $this->createMaintenance();
- $this->maintenance->output( "foo" );
- $m2->output( "bar" );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutputWNullChannel() {
- $m2 = $this->createMaintenance();
- $this->maintenance->output( "foo", null );
- $m2->output( "bar", null );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutputWChannel() {
- $m2 = $this->createMaintenance();
- $this->maintenance->output( "foo", "bazChannel" );
- $m2->output( "bar", "bazChannel" );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar\n", true );
- }
- function testMultipleMaintenanceObjectsInteractionOutputWNullChannelNL() {
- $m2 = $this->createMaintenance();
- $this->maintenance->output( "foo\n", null );
- $m2->output( "bar\n", null );
- $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutputWChannelNL() {
- $m2 = $this->createMaintenance();
- $this->maintenance->output( "foo\n", "bazChannel" );
- $m2->output( "bar\n", "bazChannel" );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar\n", true );
- }
- function testMultipleMaintenanceObjectsInteractionOutputChanneled() {
- $m2 = $this->createMaintenance();
- $this->maintenance->outputChanneled( "foo" );
- $m2->outputChanneled( "bar" );
- $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutputChanneledWNullChannel() {
- $m2 = $this->createMaintenance();
- $this->maintenance->outputChanneled( "foo", null );
- $m2->outputChanneled( "bar", null );
- $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
- }
- function testMultipleMaintenanceObjectsInteractionOutputChanneledWChannel() {
- $m2 = $this->createMaintenance();
- $this->maintenance->outputChanneled( "foo", "bazChannel" );
- $m2->outputChanneled( "bar", "bazChannel" );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before shutdown simulation (m2)" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar\n", true );
- }
- function testMultipleMaintenanceObjectsInteractionCleanupChanneledWChannel() {
- $m2 = $this->createMaintenance();
- $this->maintenance->outputChanneled( "foo", "bazChannel" );
- $m2->outputChanneled( "bar", "bazChannel" );
- $this->assertEquals( "foobar", $this->getActualOutput(),
- "Output before first cleanup" );
- $this->maintenance->cleanupChanneled();
- $this->assertEquals( "foobar\n", $this->getActualOutput(),
- "Output after first cleanup" );
- $m2->cleanupChanneled();
- $this->assertEquals( "foobar\n\n", $this->getActualOutput(),
- "Output after second cleanup" );
- $m2->cleanupChanneled();
- $this->assertOutputPrePostShutdown( "foobar\n\n", false );
- }
- /**
- * @covers Maintenance::getConfig
- */
- public function testGetConfig() {
- $this->assertInstanceOf( 'Config', $this->maintenance->getConfig() );
- $this->assertSame(
- MediaWikiServices::getInstance()->getMainConfig(),
- $this->maintenance->getConfig()
- );
- }
- /**
- * @covers Maintenance::setConfig
- */
- public function testSetConfig() {
- $conf = $this->createMock( 'Config' );
- $this->maintenance->setConfig( $conf );
- $this->assertSame( $conf, $this->maintenance->getConfig() );
- }
- function testParseArgs() {
- $m2 = $this->createMaintenance();
- // Create an option with an argument allowed to be specified multiple times
- $m2->addOption( 'multi', 'This option does stuff', false, true, false, true );
- $m2->loadWithArgv( [ '--multi', 'this1', '--multi', 'this2' ] );
- $this->assertEquals( [ 'this1', 'this2' ], $m2->getOption( 'multi' ) );
- $this->assertEquals( [ [ 'multi', 'this1' ], [ 'multi', 'this2' ] ],
- $m2->orderedOptions );
- $m2->cleanupChanneled();
- $m2 = $this->createMaintenance();
- $m2->addOption( 'multi', 'This option does stuff', false, false, false, true );
- $m2->loadWithArgv( [ '--multi', '--multi' ] );
- $this->assertEquals( [ 1, 1 ], $m2->getOption( 'multi' ) );
- $this->assertEquals( [ [ 'multi', 1 ], [ 'multi', 1 ] ], $m2->orderedOptions );
- $m2->cleanupChanneled();
- $m2 = $this->createMaintenance();
- // Create an option with an argument allowed to be specified multiple times
- $m2->addOption( 'multi', 'This option doesn\'t actually support multiple occurrences' );
- $m2->loadWithArgv( [ '--multi=yo' ] );
- $this->assertEquals( 'yo', $m2->getOption( 'multi' ) );
- $this->assertEquals( [ [ 'multi', 'yo' ] ], $m2->orderedOptions );
- $m2->cleanupChanneled();
- }
- }
|