| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance\Includes; |
| 4 |
|
| 5 |
use MediaWiki\Config\Config; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Maintenance\Maintenance; |
| 8 |
use MediaWiki\MediaWikiServices; |
| 9 |
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase; |
| 10 |
use PHPUnit\Framework\Assert; |
| 11 |
use Wikimedia\TestingAccessWrapper; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class MaintenanceTest extends MaintenanceBaseTestCase { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected function getMaintenanceClass() { |
| 24 |
return Maintenance::class; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected function createMaintenance() { |
| 36 |
$className = $this->getMaintenanceClass(); |
| 37 |
$obj = $this->getMockForAbstractClass( $className ); |
| 38 |
|
| 39 |
return TestingAccessWrapper::newFromObject( $obj ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function testOutput( $outputs, $expected, $extraNL ) { |
| 51 |
foreach ( $outputs as $data ) { |
| 52 |
if ( is_array( $data ) ) { |
| 53 |
[ $msg, $channel ] = $data; |
| 54 |
} else { |
| 55 |
$msg = $data; |
| 56 |
$channel = null; |
| 57 |
} |
| 58 |
$this->maintenance->output( $msg, $channel ); |
| 59 |
} |
| 60 |
$this->assertOutputPrePostShutdown( $expected, $extraNL ); |
| 61 |
} |
| 62 |
|
| 63 |
public static function provideOutputData() { |
| 64 |
return [ |
| 65 |
[ [ "" ], "", false ], |
| 66 |
[ [ "foo" ], "foo", false ], |
| 67 |
[ [ "foo", "bar" ], "foobar", false ], |
| 68 |
[ [ "foo\n" ], "foo\n", false ], |
| 69 |
[ [ "foo\n\n" ], "foo\n\n", false ], |
| 70 |
[ [ "foo\nbar" ], "foo\nbar", false ], |
| 71 |
[ [ "foo\nbar\n" ], "foo\nbar\n", false ], |
| 72 |
[ [ "foo\n", "bar\n" ], "foo\nbar\n", false ], |
| 73 |
[ [ "", "foo", "", "\n", "ba", "", "r\n" ], "foo\nbar\n", false ], |
| 74 |
[ [ "", "foo", "", "\nb", "a", "", "r\n" ], "foo\nbar\n", false ], |
| 75 |
[ [ [ "foo", "bazChannel" ] ], "foo", true ], |
| 76 |
[ [ [ "foo\n", "bazChannel" ] ], "foo", true ], |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
[ [ [ "foo\n\n", "bazChannel" ] ], "foo\n", true ], |
| 83 |
[ [ [ "foo\nbar", "bazChannel" ] ], "foo\nbar", true ], |
| 84 |
[ [ [ "foo\nbar\n", "bazChannel" ] ], "foo\nbar", true ], |
| 85 |
[ |
| 86 |
[ |
| 87 |
[ "foo\n", "bazChannel" ], |
| 88 |
[ "bar\n", "bazChannel" ], |
| 89 |
], |
| 90 |
"foobar", |
| 91 |
true |
| 92 |
], |
| 93 |
[ |
| 94 |
[ |
| 95 |
[ "", "bazChannel" ], |
| 96 |
[ "foo", "bazChannel" ], |
| 97 |
[ "", "bazChannel" ], |
| 98 |
[ "\n", "bazChannel" ], |
| 99 |
[ "ba", "bazChannel" ], |
| 100 |
[ "", "bazChannel" ], |
| 101 |
[ "r\n", "bazChannel" ], |
| 102 |
], |
| 103 |
"foobar", |
| 104 |
true |
| 105 |
], |
| 106 |
[ |
| 107 |
[ |
| 108 |
[ "", "bazChannel" ], |
| 109 |
[ "foo", "bazChannel" ], |
| 110 |
[ "", "bazChannel" ], |
| 111 |
[ "\nb", "bazChannel" ], |
| 112 |
[ "a", "bazChannel" ], |
| 113 |
[ "", "bazChannel" ], |
| 114 |
[ "r\n", "bazChannel" ], |
| 115 |
], |
| 116 |
"foo\nbar", |
| 117 |
true |
| 118 |
], |
| 119 |
[ |
| 120 |
[ |
| 121 |
[ "foo", "bazChannel" ], |
| 122 |
[ "bar", "bazChannel" ], |
| 123 |
[ "qux", "quuxChannel" ], |
| 124 |
[ "corge", "bazChannel" ], |
| 125 |
], |
| 126 |
"foobar\nqux\ncorge", |
| 127 |
true |
| 128 |
], |
| 129 |
[ |
| 130 |
[ |
| 131 |
[ "foo", "bazChannel" ], |
| 132 |
[ "bar\n", "bazChannel" ], |
| 133 |
[ "qux\n", "quuxChannel" ], |
| 134 |
[ "corge", "bazChannel" ], |
| 135 |
], |
| 136 |
"foobar\nqux\ncorge", |
| 137 |
true |
| 138 |
], |
| 139 |
[ |
| 140 |
[ |
| 141 |
[ "foo", null ], |
| 142 |
[ "bar", "bazChannel" ], |
| 143 |
[ "qux", null ], |
| 144 |
[ "quux", "bazChannel" ], |
| 145 |
], |
| 146 |
"foobar\nquxquux", |
| 147 |
true |
| 148 |
], |
| 149 |
[ |
| 150 |
[ |
| 151 |
[ "foo", "bazChannel" ], |
| 152 |
[ "bar", null ], |
| 153 |
[ "qux", "bazChannel" ], |
| 154 |
[ "quux", null ], |
| 155 |
], |
| 156 |
"foo\nbarqux\nquux", |
| 157 |
false |
| 158 |
], |
| 159 |
[ |
| 160 |
[ |
| 161 |
[ "foo", 1 ], |
| 162 |
[ "bar", 1.0 ], |
| 163 |
], |
| 164 |
"foo\nbar", |
| 165 |
true |
| 166 |
], |
| 167 |
[ [ "foo", "", "bar" ], "foobar", false ], |
| 168 |
[ [ "foo", false, "bar" ], "foobar", false ], |
| 169 |
[ |
| 170 |
[ |
| 171 |
[ "qux", "quuxChannel" ], |
| 172 |
"foo", |
| 173 |
false, |
| 174 |
"bar" |
| 175 |
], |
| 176 |
"qux\nfoobar", |
| 177 |
false |
| 178 |
], |
| 179 |
[ |
| 180 |
[ |
| 181 |
[ "foo", "bazChannel" ], |
| 182 |
[ "", "bazChannel" ], |
| 183 |
[ "bar", "bazChannel" ], |
| 184 |
], |
| 185 |
"foobar", |
| 186 |
true |
| 187 |
], |
| 188 |
[ |
| 189 |
[ |
| 190 |
[ "foo", "bazChannel" ], |
| 191 |
[ false, "bazChannel" ], |
| 192 |
[ "bar", "bazChannel" ], |
| 193 |
], |
| 194 |
"foobar", |
| 195 |
true |
| 196 |
], |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function testOutputChanneled( $outputs, $expected, $extraNL ) { |
| 204 |
foreach ( $outputs as $data ) { |
| 205 |
if ( is_array( $data ) ) { |
| 206 |
[ $msg, $channel ] = $data; |
| 207 |
} else { |
| 208 |
$msg = $data; |
| 209 |
$channel = null; |
| 210 |
} |
| 211 |
$this->maintenance->outputChanneled( $msg, $channel ); |
| 212 |
} |
| 213 |
$this->assertOutputPrePostShutdown( $expected, $extraNL ); |
| 214 |
} |
| 215 |
|
| 216 |
public static function provideOutputChanneledData() { |
| 217 |
return [ |
| 218 |
[ [ "" ], "\n", false ], |
| 219 |
[ [ "foo" ], "foo\n", false ], |
| 220 |
[ [ "foo", "bar" ], "foo\nbar\n", false ], |
| 221 |
[ [ "foo\nbar" ], "foo\nbar\n", false ], |
| 222 |
[ [ "", "foo", "", "\nb", "a", "", "r" ], "\nfoo\n\n\nb\na\n\nr\n", false ], |
| 223 |
[ [ [ "foo", "bazChannel" ] ], "foo", true ], |
| 224 |
[ |
| 225 |
[ |
| 226 |
[ "foo\nbar", "bazChannel" ] |
| 227 |
], |
| 228 |
"foo\nbar", |
| 229 |
true |
| 230 |
], |
| 231 |
[ |
| 232 |
[ |
| 233 |
[ "foo", "bazChannel" ], |
| 234 |
[ "bar", "bazChannel" ], |
| 235 |
], |
| 236 |
"foobar", |
| 237 |
true |
| 238 |
], |
| 239 |
[ |
| 240 |
[ |
| 241 |
[ "", "bazChannel" ], |
| 242 |
[ "foo", "bazChannel" ], |
| 243 |
[ "", "bazChannel" ], |
| 244 |
[ "\nb", "bazChannel" ], |
| 245 |
[ "a", "bazChannel" ], |
| 246 |
[ "", "bazChannel" ], |
| 247 |
[ "r", "bazChannel" ], |
| 248 |
], |
| 249 |
"foo\nbar", |
| 250 |
true |
| 251 |
], |
| 252 |
[ |
| 253 |
[ |
| 254 |
[ "foo", "bazChannel" ], |
| 255 |
[ "bar", "bazChannel" ], |
| 256 |
[ "qux", "quuxChannel" ], |
| 257 |
[ "corge", "bazChannel" ], |
| 258 |
], |
| 259 |
"foobar\nqux\ncorge", |
| 260 |
true |
| 261 |
], |
| 262 |
[ |
| 263 |
[ |
| 264 |
[ "foo", "bazChannel" ], |
| 265 |
[ "bar", "bazChannel" ], |
| 266 |
[ "qux", "quuxChannel" ], |
| 267 |
[ "corge", "bazChannel" ], |
| 268 |
], |
| 269 |
"foobar\nqux\ncorge", |
| 270 |
true |
| 271 |
], |
| 272 |
[ |
| 273 |
[ |
| 274 |
[ "foo", "bazChannel" ], |
| 275 |
[ "bar", null ], |
| 276 |
[ "qux", null ], |
| 277 |
[ "corge", "bazChannel" ], |
| 278 |
], |
| 279 |
"foo\nbar\nqux\ncorge", |
| 280 |
true |
| 281 |
], |
| 282 |
[ |
| 283 |
[ |
| 284 |
[ "foo", null ], |
| 285 |
[ "bar", "bazChannel" ], |
| 286 |
[ "qux", null ], |
| 287 |
[ "quux", "bazChannel" ], |
| 288 |
], |
| 289 |
"foo\nbar\nqux\nquux", |
| 290 |
true |
| 291 |
], |
| 292 |
[ |
| 293 |
[ |
| 294 |
[ "foo", "bazChannel" ], |
| 295 |
[ "bar", null ], |
| 296 |
[ "qux", "bazChannel" ], |
| 297 |
[ "quux", null ], |
| 298 |
], |
| 299 |
"foo\nbar\nqux\nquux\n", |
| 300 |
false |
| 301 |
], |
| 302 |
[ |
| 303 |
[ |
| 304 |
[ "foo", 1 ], |
| 305 |
[ "bar", 1.0 ], |
| 306 |
], |
| 307 |
"foo\nbar", |
| 308 |
true |
| 309 |
], |
| 310 |
[ [ "foo", "", "bar" ], "foo\n\nbar\n", false ], |
| 311 |
[ [ "foo", false, "bar" ], "foo\nbar\n", false ], |
| 312 |
]; |
| 313 |
} |
| 314 |
|
| 315 |
public function testCleanupChanneledClean() { |
| 316 |
$this->maintenance->cleanupChanneled(); |
| 317 |
$this->assertOutputPrePostShutdown( "", false ); |
| 318 |
} |
| 319 |
|
| 320 |
public function testCleanupChanneledAfterOutput() { |
| 321 |
$this->maintenance->output( "foo" ); |
| 322 |
$this->maintenance->cleanupChanneled(); |
| 323 |
$this->assertOutputPrePostShutdown( "foo", false ); |
| 324 |
} |
| 325 |
|
| 326 |
public function testCleanupChanneledAfterOutputWNullChannel() { |
| 327 |
$this->maintenance->output( "foo", null ); |
| 328 |
$this->maintenance->cleanupChanneled(); |
| 329 |
$this->assertOutputPrePostShutdown( "foo", false ); |
| 330 |
} |
| 331 |
|
| 332 |
public function testCleanupChanneledAfterOutputWChannel() { |
| 333 |
$this->maintenance->output( "foo", "bazChannel" ); |
| 334 |
$this->maintenance->cleanupChanneled(); |
| 335 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 336 |
} |
| 337 |
|
| 338 |
public function testCleanupChanneledAfterNLOutput() { |
| 339 |
$this->maintenance->output( "foo\n" ); |
| 340 |
$this->maintenance->cleanupChanneled(); |
| 341 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 342 |
} |
| 343 |
|
| 344 |
public function testCleanupChanneledAfterNLOutputWNullChannel() { |
| 345 |
$this->maintenance->output( "foo\n", null ); |
| 346 |
$this->maintenance->cleanupChanneled(); |
| 347 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 348 |
} |
| 349 |
|
| 350 |
public function testCleanupChanneledAfterNLOutputWChannel() { |
| 351 |
$this->maintenance->output( "foo\n", "bazChannel" ); |
| 352 |
$this->maintenance->cleanupChanneled(); |
| 353 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 354 |
} |
| 355 |
|
| 356 |
public function testCleanupChanneledAfterOutputChanneledWOChannel() { |
| 357 |
$this->maintenance->outputChanneled( "foo" ); |
| 358 |
$this->maintenance->cleanupChanneled(); |
| 359 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 360 |
} |
| 361 |
|
| 362 |
public function testCleanupChanneledAfterOutputChanneledWNullChannel() { |
| 363 |
$this->maintenance->outputChanneled( "foo", null ); |
| 364 |
$this->maintenance->cleanupChanneled(); |
| 365 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 366 |
} |
| 367 |
|
| 368 |
public function testCleanupChanneledAfterOutputChanneledWChannel() { |
| 369 |
$this->maintenance->outputChanneled( "foo", "bazChannel" ); |
| 370 |
$this->maintenance->cleanupChanneled(); |
| 371 |
$this->assertOutputPrePostShutdown( "foo\n", false ); |
| 372 |
} |
| 373 |
|
| 374 |
public function testMultipleMaintenanceObjectsInteractionOutput() { |
| 375 |
$m2 = $this->createMaintenance(); |
| 376 |
|
| 377 |
$this->maintenance->output( "foo" ); |
| 378 |
$m2->output( "bar" ); |
| 379 |
|
| 380 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 381 |
"Output before shutdown simulation (m2)" ); |
| 382 |
$m2->cleanupChanneled(); |
| 383 |
$this->assertOutputPrePostShutdown( "foobar", false ); |
| 384 |
} |
| 385 |
|
| 386 |
public function testMultipleMaintenanceObjectsInteractionOutputWNullChannel() { |
| 387 |
$m2 = $this->createMaintenance(); |
| 388 |
|
| 389 |
$this->maintenance->output( "foo", null ); |
| 390 |
$m2->output( "bar", null ); |
| 391 |
|
| 392 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 393 |
"Output before shutdown simulation (m2)" ); |
| 394 |
$m2->cleanupChanneled(); |
| 395 |
$this->assertOutputPrePostShutdown( "foobar", false ); |
| 396 |
} |
| 397 |
|
| 398 |
public function testMultipleMaintenanceObjectsInteractionOutputWChannel() { |
| 399 |
$m2 = $this->createMaintenance(); |
| 400 |
|
| 401 |
$this->maintenance->output( "foo", "bazChannel" ); |
| 402 |
$m2->output( "bar", "bazChannel" ); |
| 403 |
|
| 404 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 405 |
"Output before shutdown simulation (m2)" ); |
| 406 |
$m2->cleanupChanneled(); |
| 407 |
$this->assertOutputPrePostShutdown( "foobar\n", true ); |
| 408 |
} |
| 409 |
|
| 410 |
public function testMultipleMaintenanceObjectsInteractionOutputWNullChannelNL() { |
| 411 |
$m2 = $this->createMaintenance(); |
| 412 |
|
| 413 |
$this->maintenance->output( "foo\n", null ); |
| 414 |
$m2->output( "bar\n", null ); |
| 415 |
|
| 416 |
$this->assertEquals( "foo\nbar\n", $this->getActualOutput(), |
| 417 |
"Output before shutdown simulation (m2)" ); |
| 418 |
$m2->cleanupChanneled(); |
| 419 |
$this->assertOutputPrePostShutdown( "foo\nbar\n", false ); |
| 420 |
} |
| 421 |
|
| 422 |
public function testMultipleMaintenanceObjectsInteractionOutputWChannelNL() { |
| 423 |
$m2 = $this->createMaintenance(); |
| 424 |
|
| 425 |
$this->maintenance->output( "foo\n", "bazChannel" ); |
| 426 |
$m2->output( "bar\n", "bazChannel" ); |
| 427 |
|
| 428 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 429 |
"Output before shutdown simulation (m2)" ); |
| 430 |
$m2->cleanupChanneled(); |
| 431 |
$this->assertOutputPrePostShutdown( "foobar\n", true ); |
| 432 |
} |
| 433 |
|
| 434 |
public function testMultipleMaintenanceObjectsInteractionOutputChanneled() { |
| 435 |
$m2 = $this->createMaintenance(); |
| 436 |
|
| 437 |
$this->maintenance->outputChanneled( "foo" ); |
| 438 |
$m2->outputChanneled( "bar" ); |
| 439 |
|
| 440 |
$this->assertEquals( "foo\nbar\n", $this->getActualOutput(), |
| 441 |
"Output before shutdown simulation (m2)" ); |
| 442 |
$m2->cleanupChanneled(); |
| 443 |
$this->assertOutputPrePostShutdown( "foo\nbar\n", false ); |
| 444 |
} |
| 445 |
|
| 446 |
public function testMultipleMaintenanceObjectsInteractionOutputChanneledWNullChannel() { |
| 447 |
$m2 = $this->createMaintenance(); |
| 448 |
|
| 449 |
$this->maintenance->outputChanneled( "foo", null ); |
| 450 |
$m2->outputChanneled( "bar", null ); |
| 451 |
|
| 452 |
$this->assertEquals( "foo\nbar\n", $this->getActualOutput(), |
| 453 |
"Output before shutdown simulation (m2)" ); |
| 454 |
$m2->cleanupChanneled(); |
| 455 |
$this->assertOutputPrePostShutdown( "foo\nbar\n", false ); |
| 456 |
} |
| 457 |
|
| 458 |
public function testMultipleMaintenanceObjectsInteractionOutputChanneledWChannel() { |
| 459 |
$m2 = $this->createMaintenance(); |
| 460 |
|
| 461 |
$this->maintenance->outputChanneled( "foo", "bazChannel" ); |
| 462 |
$m2->outputChanneled( "bar", "bazChannel" ); |
| 463 |
|
| 464 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 465 |
"Output before shutdown simulation (m2)" ); |
| 466 |
$m2->cleanupChanneled(); |
| 467 |
$this->assertOutputPrePostShutdown( "foobar\n", true ); |
| 468 |
} |
| 469 |
|
| 470 |
public function testMultipleMaintenanceObjectsInteractionCleanupChanneledWChannel() { |
| 471 |
$m2 = $this->createMaintenance(); |
| 472 |
|
| 473 |
$this->maintenance->outputChanneled( "foo", "bazChannel" ); |
| 474 |
$m2->outputChanneled( "bar", "bazChannel" ); |
| 475 |
|
| 476 |
$this->assertEquals( "foobar", $this->getActualOutput(), |
| 477 |
"Output before first cleanup" ); |
| 478 |
$this->maintenance->cleanupChanneled(); |
| 479 |
$this->assertEquals( "foobar\n", $this->getActualOutput(), |
| 480 |
"Output after first cleanup" ); |
| 481 |
$m2->cleanupChanneled(); |
| 482 |
$this->assertEquals( "foobar\n\n", $this->getActualOutput(), |
| 483 |
"Output after second cleanup" ); |
| 484 |
|
| 485 |
$m2->cleanupChanneled(); |
| 486 |
$this->assertOutputPrePostShutdown( "foobar\n\n", false ); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function testGetConfig() { |
| 493 |
$this->assertInstanceOf( Config::class, $this->maintenance->getConfig() ); |
| 494 |
$this->assertSame( |
| 495 |
MediaWikiServices::getInstance()->getMainConfig(), |
| 496 |
$this->maintenance->getConfig() |
| 497 |
); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public function testSetConfig() { |
| 504 |
$conf = new HashConfig(); |
| 505 |
$this->maintenance->setConfig( $conf ); |
| 506 |
$this->assertSame( $conf, $this->maintenance->getConfig() ); |
| 507 |
} |
| 508 |
|
| 509 |
public function testParseWithMultiArgs() { |
| 510 |
|
| 511 |
$this->maintenance->addOption( 'multi', 'This option does stuff', false, true, false, true ); |
| 512 |
$this->maintenance->loadWithArgv( [ '--multi', 'this1', '--multi', 'this2' ] ); |
| 513 |
|
| 514 |
$this->assertEquals( [ 'this1', 'this2' ], $this->maintenance->getOption( 'multi' ) ); |
| 515 |
$this->assertEquals( [ [ 'multi', 'this1' ], [ 'multi', 'this2' ] ], |
| 516 |
$this->maintenance->orderedOptions ); |
| 517 |
} |
| 518 |
|
| 519 |
public function testParseMultiOption() { |
| 520 |
$this->maintenance->addOption( 'multi', 'This option does stuff', false, false, false, true ); |
| 521 |
$this->maintenance->loadWithArgv( [ '--multi', '--multi' ] ); |
| 522 |
|
| 523 |
$this->assertEquals( [ 1, 1 ], $this->maintenance->getOption( 'multi' ) ); |
| 524 |
$this->assertEquals( [ [ 'multi', 1 ], [ 'multi', 1 ] ], $this->maintenance->orderedOptions ); |
| 525 |
} |
| 526 |
|
| 527 |
public function testParseArgs() { |
| 528 |
$this->maintenance->addOption( 'multi', 'This option doesn\'t actually support multiple occurrences' ); |
| 529 |
$this->maintenance->loadWithArgv( [ '--multi=yo' ] ); |
| 530 |
|
| 531 |
$this->assertEquals( 'yo', $this->maintenance->getOption( 'multi' ) ); |
| 532 |
$this->assertEquals( [ [ 'multi', 'yo' ] ], $this->maintenance->orderedOptions ); |
| 533 |
} |
| 534 |
|
| 535 |
public function testOptionGetters() { |
| 536 |
$this->assertSame( |
| 537 |
false, |
| 538 |
$this->maintenance->hasOption( 'somearg' ), |
| 539 |
'Non existent option not found' |
| 540 |
); |
| 541 |
$this->assertSame( |
| 542 |
'default', |
| 543 |
$this->maintenance->getOption( 'somearg', 'default' ), |
| 544 |
'Non existent option falls back to default' |
| 545 |
); |
| 546 |
$this->assertSame( |
| 547 |
false, |
| 548 |
$this->maintenance->hasOption( 'somearg' ), |
| 549 |
'Non existent option not found after getting' |
| 550 |
); |
| 551 |
$this->assertSame( |
| 552 |
'newdefault', |
| 553 |
$this->maintenance->getOption( 'somearg', 'newdefault' ), |
| 554 |
'Non existent option falls back to a new default' |
| 555 |
); |
| 556 |
} |
| 557 |
|
| 558 |
public function testLegacyOptionsAccess() { |
| 559 |
$maintenance = new class () extends Maintenance { |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public function execute() { |
| 566 |
$this->setOption( 'test', 'foo' ); |
| 567 |
Assert::assertSame( 'foo', $this->getOption( 'test' ) ); |
| 568 |
Assert::assertSame( 'foo', $this->mOptions['test'] ); |
| 569 |
|
| 570 |
$this->mOptions['test'] = 'bar'; |
| 571 |
Assert::assertSame( 'bar', $this->getOption( 'test' ) ); |
| 572 |
|
| 573 |
$this->setArg( 1, 'foo' ); |
| 574 |
Assert::assertSame( 'foo', $this->getArg( 1 ) ); |
| 575 |
Assert::assertSame( 'foo', $this->mArgs[1] ); |
| 576 |
|
| 577 |
$this->mArgs[1] = 'bar'; |
| 578 |
Assert::assertSame( 'bar', $this->getArg( 1 ) ); |
| 579 |
} |
| 580 |
}; |
| 581 |
|
| 582 |
$maintenance->execute(); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
public function testFatalError( $msg, $errorCode ) { |
| 587 |
$this->expectCallToFatalError( $errorCode ); |
| 588 |
$this->expectOutputString( $msg . "\n" ); |
| 589 |
$this->maintenance->fatalError( $msg, $errorCode ); |
| 590 |
} |
| 591 |
|
| 592 |
public static function provideFatalError() { |
| 593 |
return [ |
| 594 |
'No error message, code as 1' => [ '', 1 ], |
| 595 |
'Defined error message, code as 3' => [ 'Testing error message', 3 ], |
| 596 |
]; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
public function testCheckRequiredExtensionForMissingExtension( $requiredExtensions, $expectedOutputRegex ) { |
| 601 |
$this->expectCallToFatalError(); |
| 602 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 603 |
foreach ( $requiredExtensions as $extension ) { |
| 604 |
$this->maintenance->requireExtension( $extension ); |
| 605 |
} |
| 606 |
$this->maintenance->checkRequiredExtensions(); |
| 607 |
} |
| 608 |
|
| 609 |
public static function provideRequiredButMissingExtensions() { |
| 610 |
return [ |
| 611 |
'One missing extension' => [ |
| 612 |
[ 'FakeExtensionForMaintenanceTest' ], |
| 613 |
'/The "FakeExtensionForMaintenanceTest" extension must be installed.*' . |
| 614 |
'Please enable it and then try again/' |
| 615 |
], |
| 616 |
'Two missing extensions' => [ |
| 617 |
[ 'FakeExtensionForMaintenanceTest', 'MissingExtensionTest2' ], |
| 618 |
'/The following extensions must be installed.*FakeExtensionForMaintenanceTest.*MissingExtensionTest2' . |
| 619 |
'.*Please enable them and then try again/' |
| 620 |
], |
| 621 |
]; |
| 622 |
} |
| 623 |
|
| 624 |
public function testValidateUserOptionForMissingArguments() { |
| 625 |
$this->expectCallToFatalError(); |
| 626 |
$this->expectOutputRegex( '/Test error message/' ); |
| 627 |
$this->maintenance->validateUserOption( 'Test error message' ); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
public function testValidateUserOptionForInvalidUserOption( $options, $expectedOutputRegex ) { |
| 632 |
$this->expectCallToFatalError(); |
| 633 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 634 |
foreach ( $options as $name => $value ) { |
| 635 |
$this->maintenance->setOption( $name, $value ); |
| 636 |
} |
| 637 |
$this->maintenance->validateUserOption( 'unused' ); |
| 638 |
} |
| 639 |
|
| 640 |
public static function provideInvalidUserOptions() { |
| 641 |
return [ |
| 642 |
'Invalid --user option' => [ |
| 643 |
[ 'user' => 'Non-existent-test-user' ], '/No such user.*Non-existent-test-user/', |
| 644 |
], |
| 645 |
'Invalid --userid option' => [ [ 'userid' => 0 ], '/No such user id.*0/' ], |
| 646 |
]; |
| 647 |
} |
| 648 |
|
| 649 |
public function testValidateUserOptionForValidUser() { |
| 650 |
$testUser = $this->getTestUser()->getUserIdentity(); |
| 651 |
$this->maintenance->setOption( 'userid', $testUser->getId() ); |
| 652 |
$this->assertTrue( $testUser->equals( $this->maintenance->validateUserOption( "unused" ) ) ); |
| 653 |
} |
| 654 |
|
| 655 |
public function testRunChildForNonExistentClass() { |
| 656 |
$this->expectCallToFatalError(); |
| 657 |
$this->expectOutputRegex( '/Cannot spawn child.*NonExistingTestClassForMaintenanceTest/' ); |
| 658 |
$this->maintenance->runChild( 'NonExistingTestClassForMaintenanceTest' ); |
| 659 |
} |
| 660 |
|
| 661 |
public function testSetAllowUnregisteredOptions() { |
| 662 |
$this->maintenance->setOption( 'abcdef', 'abc' ); |
| 663 |
$this->maintenance->setAllowUnregisteredOptions( true ); |
| 664 |
$this->assertTrue( $this->maintenance->getParameters()->validate() ); |
| 665 |
$this->maintenance->setAllowUnregisteredOptions( false ); |
| 666 |
$this->assertFalse( $this->maintenance->getParameters()->validate() ); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
public function testSetBatchSize( $batchSize, $shouldHaveBatchSizeOption ) { |
| 671 |
$this->maintenance->setBatchSize( $batchSize ); |
| 672 |
$this->assertSame( $batchSize, $this->maintenance->getBatchSize() ); |
| 673 |
$this->assertSame( |
| 674 |
$shouldHaveBatchSizeOption, |
| 675 |
$this->maintenance->supportsOption( 'batch-size' ) |
| 676 |
); |
| 677 |
} |
| 678 |
|
| 679 |
public static function provideSetBatchSize() { |
| 680 |
return [ |
| 681 |
'Batch size as 0' => [ 0, false ], |
| 682 |
'Batch size as 150' => [ 150, true ], |
| 683 |
]; |
| 684 |
} |
| 685 |
|
| 686 |
public function testPurgeRedundantTextWhenNoPagesExist() { |
| 687 |
|
| 688 |
$this->maintenance->purgeRedundantText(); |
| 689 |
$this->expectOutputRegex( '/0 inactive items found[\s\S]*(?!Deleting)/' ); |
| 690 |
} |
| 691 |
|
| 692 |
public function testDeleteOptionLoop() { |
| 693 |
$this->maintenance->addOption( 'test-for-deletion', 'testing' ); |
| 694 |
$this->assertTrue( $this->maintenance->getParameters()->supportsOption( 'test-for-deletion' ) ); |
| 695 |
$this->maintenance->deleteOption( 'test-for-deletion' ); |
| 696 |
$this->assertFalse( $this->maintenance->getParameters()->supportsOption( 'test-for-deletion' ) ); |
| 697 |
} |
| 698 |
|
| 699 |
public function testAddDescription() { |
| 700 |
$this->maintenance->addDescription( 'testing-description abcdef' ); |
| 701 |
$this->expectCallToFatalError(); |
| 702 |
$this->expectOutputRegex( '/testing-description abcdef/' ); |
| 703 |
$this->maintenance->getParameters()->setName( 'test.php' ); |
| 704 |
$this->maintenance->maybeHelp( true ); |
| 705 |
} |
| 706 |
|
| 707 |
public function testGetArgName() { |
| 708 |
$this->maintenance->addArg( 'testing', 'test' ); |
| 709 |
$this->assertSame( 'testing', $this->maintenance->getArgName( 0 ) ); |
| 710 |
} |
| 711 |
|
| 712 |
public function testHasArg() { |
| 713 |
$this->maintenance->addArg( 'testing', 'test' ); |
| 714 |
$this->maintenance->setArg( 'testing', 'abc' ); |
| 715 |
$this->assertTrue( $this->maintenance->hasArg( 0 ) ); |
| 716 |
$this->assertTrue( $this->maintenance->hasArg( 'testing' ) ); |
| 717 |
$this->assertSame( [ 'abc' ], $this->maintenance->getArgs() ); |
| 718 |
} |
| 719 |
|
| 720 |
public function testSetName() { |
| 721 |
$this->maintenance->setName( 'test.php' ); |
| 722 |
$this->assertSame( 'test.php', $this->maintenance->getName() ); |
| 723 |
$this->assertSame( 'test.php', $this->maintenance->getParameters()->getName() ); |
| 724 |
} |
| 725 |
|
| 726 |
public function testGetDir() { |
| 727 |
$this->assertSame( realpath( MW_INSTALL_PATH . '/maintenance' ), realpath( $this->maintenance->getDir() ) ); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
public function testParseIntList( $text, $expected ) { |
| 732 |
$this->assertArrayEquals( $expected, $this->maintenance->parseIntList( $text ) ); |
| 733 |
} |
| 734 |
|
| 735 |
public static function provideParseIntList() { |
| 736 |
return [ |
| 737 |
'Integers separated by ","' => [ '1,2,3,3', [ 1, 2, 3, 3 ] ], |
| 738 |
'Integers separated by "|"' => [ '1|2|3|4|4', [ 1, 2, 3, 4, 4 ] ], |
| 739 |
]; |
| 740 |
} |
| 741 |
} |