MaintenanceTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. // It would be great if we were able to use PHPUnit's getMockForAbstractClass
  3. // instead of the MaintenanceFixup hack below. However, we cannot do
  4. // without changing the visibility and without working around hacks in
  5. // Maintenance.php
  6. // For the same reason, we cannot just use FakeMaintenance.
  7. /**
  8. * makes parts of the API of Maintenance that is hidden by protected visibily
  9. * visible for testing, and makes up for a stream closing hack in Maintenance.php.
  10. *
  11. * This class is solely used for being able to test Maintenance right now
  12. * without having to apply major refactorings to fix some design issues in
  13. * Maintenance.php. Before adding more functions here, please consider whether
  14. * this approach is correct, or a refactoring Maintenance to separate concers
  15. * is more appropriate.
  16. *
  17. * Upon refactoring, keep in mind that besides the maintenance scrits themselves
  18. * and tests right here, also at least Extension:Maintenance make use of
  19. * Maintenance.
  20. *
  21. * Due to a hack in Maintenance.php using register_shutdown_function, be sure to
  22. * finally call simulateShutdown on MaintenanceFixup instance before a test
  23. * ends.
  24. *
  25. */
  26. class MaintenanceFixup extends Maintenance {
  27. // --- Making up for the register_shutdown_function hack in Maintenance.php
  28. /**
  29. * The test case that generated this instance.
  30. *
  31. * This member is motivated by allowing the destructor to check whether or not
  32. * the test failed, in order to avoid unnecessary nags about omitted shutdown
  33. * simulation.
  34. * But as it is already available, we also usi it to flagging tests as failed
  35. *
  36. * @var MediaWikiTestCase
  37. */
  38. private $testCase;
  39. /**
  40. * shutdownSimulated === true if simulateShutdown has done it's work
  41. *
  42. * @var bool
  43. */
  44. private $shutdownSimulated = false;
  45. /**
  46. * Simulates what Maintenance wants to happen at script's end.
  47. */
  48. public function simulateShutdown() {
  49. if ( $this->shutdownSimulated ) {
  50. $this->testCase->fail( __METHOD__ . " called more than once" );
  51. }
  52. // The cleanup action.
  53. $this->outputChanneled( false );
  54. // Bookkeeping that we simulated the clean up.
  55. $this->shutdownSimulated = true;
  56. }
  57. // Note that the "public" here does not change visibility
  58. public function outputChanneled( $msg, $channel = null ) {
  59. if ( $this->shutdownSimulated ) {
  60. if ( $msg !== false ) {
  61. $this->testCase->fail( "Already past simulated shutdown, but msg is "
  62. . "not false. Did the hack in Maintenance.php change? Please "
  63. . "adapt the test case or Maintenance.php" );
  64. }
  65. // The current call is the one registered via register_shutdown_function.
  66. // We can safely ignore it, as we simulated this one via simulateShutdown
  67. // before (if we did not, the destructor of this instance will warn about
  68. // it)
  69. return;
  70. }
  71. call_user_func_array( [ "parent", __FUNCTION__ ], func_get_args() );
  72. }
  73. /**
  74. * Safety net around register_shutdown_function of Maintenance.php
  75. */
  76. public function __destruct() {
  77. if ( !$this->shutdownSimulated ) {
  78. // Someone generated a MaintenanceFixup instance without calling
  79. // simulateShutdown. We'd have to raise a PHPUnit exception to correctly
  80. // flag this illegal usage. However, we are already in a destruktor, which
  81. // would trigger undefined behavior. Hence, we can only report to the
  82. // error output :( Hopefully people read the PHPUnit output.
  83. $name = $this->testCase->getName();
  84. fwrite( STDERR, "ERROR! Instance of " . __CLASS__ . " for test $name "
  85. . "destructed without calling simulateShutdown method. Call "
  86. . "simulateShutdown on the instance before it gets destructed." );
  87. }
  88. // The following guard is required, as PHP does not offer default destructors :(
  89. if ( is_callable( "parent::__destruct" ) ) {
  90. parent::__destruct();
  91. }
  92. }
  93. public function __construct( MediaWikiTestCase $testCase ) {
  94. parent::__construct();
  95. $this->testCase = $testCase;
  96. }
  97. // --- Making protected functions visible for test
  98. public function output( $out, $channel = null ) {
  99. // Just to make PHP not nag about signature mismatches, we copied
  100. // Maintenance::output signature. However, we do not use (or rely on)
  101. // those variables. Instead we pass to Maintenance::output whatever we
  102. // receive at runtime.
  103. return call_user_func_array( [ "parent", __FUNCTION__ ], func_get_args() );
  104. }
  105. public function addOption( $name, $description, $required = false,
  106. $withArg = false, $shortName = false, $multiOccurance = false
  107. ) {
  108. return call_user_func_array( [ "parent", __FUNCTION__ ], func_get_args() );
  109. }
  110. public function getOption( $name, $default = null ) {
  111. return call_user_func_array( [ "parent", __FUNCTION__ ], func_get_args() );
  112. }
  113. // --- Requirements for getting instance of abstract class
  114. public function execute() {
  115. $this->testCase->fail( __METHOD__ . " called unexpectedly" );
  116. }
  117. }
  118. /**
  119. * @covers Maintenance
  120. */
  121. class MaintenanceTest extends MediaWikiTestCase {
  122. /**
  123. * The main Maintenance instance that is used for testing.
  124. *
  125. * @var MaintenanceFixup
  126. */
  127. private $m;
  128. protected function setUp() {
  129. parent::setUp();
  130. $this->m = new MaintenanceFixup( $this );
  131. }
  132. protected function tearDown() {
  133. if ( $this->m ) {
  134. $this->m->simulateShutdown();
  135. $this->m = null;
  136. }
  137. parent::tearDown();
  138. }
  139. /**
  140. * asserts the output before and after simulating shutdown
  141. *
  142. * This function simulates shutdown of self::m.
  143. *
  144. * @param string $preShutdownOutput Expected output before simulating shutdown
  145. * @param bool $expectNLAppending Whether or not shutdown simulation is expected
  146. * to add a newline to the output. If false, $preShutdownOutput is the
  147. * expected output after shutdown simulation. Otherwise,
  148. * $preShutdownOutput with an appended newline is the expected output
  149. * after shutdown simulation.
  150. */
  151. private function assertOutputPrePostShutdown( $preShutdownOutput, $expectNLAppending ) {
  152. $this->assertEquals( $preShutdownOutput, $this->getActualOutput(),
  153. "Output before shutdown simulation" );
  154. $this->m->simulateShutdown();
  155. $this->m = null;
  156. $postShutdownOutput = $preShutdownOutput . ( $expectNLAppending ? "\n" : "" );
  157. $this->expectOutputString( $postShutdownOutput );
  158. }
  159. // Although the following tests do not seem to be too consistent (compare for
  160. // example the newlines within the test.*StringString tests, or the
  161. // test.*Intermittent.* tests), the objective of these tests is not to describe
  162. // consistent behavior, but rather currently existing behavior.
  163. function testOutputEmpty() {
  164. $this->m->output( "" );
  165. $this->assertOutputPrePostShutdown( "", false );
  166. }
  167. function testOutputString() {
  168. $this->m->output( "foo" );
  169. $this->assertOutputPrePostShutdown( "foo", false );
  170. }
  171. function testOutputStringString() {
  172. $this->m->output( "foo" );
  173. $this->m->output( "bar" );
  174. $this->assertOutputPrePostShutdown( "foobar", false );
  175. }
  176. function testOutputStringNL() {
  177. $this->m->output( "foo\n" );
  178. $this->assertOutputPrePostShutdown( "foo\n", false );
  179. }
  180. function testOutputStringNLNL() {
  181. $this->m->output( "foo\n\n" );
  182. $this->assertOutputPrePostShutdown( "foo\n\n", false );
  183. }
  184. function testOutputStringNLString() {
  185. $this->m->output( "foo\nbar" );
  186. $this->assertOutputPrePostShutdown( "foo\nbar", false );
  187. }
  188. function testOutputStringNLStringNL() {
  189. $this->m->output( "foo\nbar\n" );
  190. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  191. }
  192. function testOutputStringNLStringNLLinewise() {
  193. $this->m->output( "foo\n" );
  194. $this->m->output( "bar\n" );
  195. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  196. }
  197. function testOutputStringNLStringNLArbitrary() {
  198. $this->m->output( "" );
  199. $this->m->output( "foo" );
  200. $this->m->output( "" );
  201. $this->m->output( "\n" );
  202. $this->m->output( "ba" );
  203. $this->m->output( "" );
  204. $this->m->output( "r\n" );
  205. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  206. }
  207. function testOutputStringNLStringNLArbitraryAgain() {
  208. $this->m->output( "" );
  209. $this->m->output( "foo" );
  210. $this->m->output( "" );
  211. $this->m->output( "\nb" );
  212. $this->m->output( "a" );
  213. $this->m->output( "" );
  214. $this->m->output( "r\n" );
  215. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  216. }
  217. function testOutputWNullChannelEmpty() {
  218. $this->m->output( "", null );
  219. $this->assertOutputPrePostShutdown( "", false );
  220. }
  221. function testOutputWNullChannelString() {
  222. $this->m->output( "foo", null );
  223. $this->assertOutputPrePostShutdown( "foo", false );
  224. }
  225. function testOutputWNullChannelStringString() {
  226. $this->m->output( "foo", null );
  227. $this->m->output( "bar", null );
  228. $this->assertOutputPrePostShutdown( "foobar", false );
  229. }
  230. function testOutputWNullChannelStringNL() {
  231. $this->m->output( "foo\n", null );
  232. $this->assertOutputPrePostShutdown( "foo\n", false );
  233. }
  234. function testOutputWNullChannelStringNLNL() {
  235. $this->m->output( "foo\n\n", null );
  236. $this->assertOutputPrePostShutdown( "foo\n\n", false );
  237. }
  238. function testOutputWNullChannelStringNLString() {
  239. $this->m->output( "foo\nbar", null );
  240. $this->assertOutputPrePostShutdown( "foo\nbar", false );
  241. }
  242. function testOutputWNullChannelStringNLStringNL() {
  243. $this->m->output( "foo\nbar\n", null );
  244. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  245. }
  246. function testOutputWNullChannelStringNLStringNLLinewise() {
  247. $this->m->output( "foo\n", null );
  248. $this->m->output( "bar\n", null );
  249. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  250. }
  251. function testOutputWNullChannelStringNLStringNLArbitrary() {
  252. $this->m->output( "", null );
  253. $this->m->output( "foo", null );
  254. $this->m->output( "", null );
  255. $this->m->output( "\n", null );
  256. $this->m->output( "ba", null );
  257. $this->m->output( "", null );
  258. $this->m->output( "r\n", null );
  259. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  260. }
  261. function testOutputWNullChannelStringNLStringNLArbitraryAgain() {
  262. $this->m->output( "", null );
  263. $this->m->output( "foo", null );
  264. $this->m->output( "", null );
  265. $this->m->output( "\nb", null );
  266. $this->m->output( "a", null );
  267. $this->m->output( "", null );
  268. $this->m->output( "r\n", null );
  269. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  270. }
  271. function testOutputWChannelString() {
  272. $this->m->output( "foo", "bazChannel" );
  273. $this->assertOutputPrePostShutdown( "foo", true );
  274. }
  275. function testOutputWChannelStringNL() {
  276. $this->m->output( "foo\n", "bazChannel" );
  277. $this->assertOutputPrePostShutdown( "foo", true );
  278. }
  279. function testOutputWChannelStringNLNL() {
  280. // If this test fails, note that output takes strings with double line
  281. // endings (although output's implementation in this situation calls
  282. // outputChanneled with a string ending in a nl ... which is not allowed
  283. // according to the documentation of outputChanneled)
  284. $this->m->output( "foo\n\n", "bazChannel" );
  285. $this->assertOutputPrePostShutdown( "foo\n", true );
  286. }
  287. function testOutputWChannelStringNLString() {
  288. $this->m->output( "foo\nbar", "bazChannel" );
  289. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  290. }
  291. function testOutputWChannelStringNLStringNL() {
  292. $this->m->output( "foo\nbar\n", "bazChannel" );
  293. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  294. }
  295. function testOutputWChannelStringNLStringNLLinewise() {
  296. $this->m->output( "foo\n", "bazChannel" );
  297. $this->m->output( "bar\n", "bazChannel" );
  298. $this->assertOutputPrePostShutdown( "foobar", true );
  299. }
  300. function testOutputWChannelStringNLStringNLArbitrary() {
  301. $this->m->output( "", "bazChannel" );
  302. $this->m->output( "foo", "bazChannel" );
  303. $this->m->output( "", "bazChannel" );
  304. $this->m->output( "\n", "bazChannel" );
  305. $this->m->output( "ba", "bazChannel" );
  306. $this->m->output( "", "bazChannel" );
  307. $this->m->output( "r\n", "bazChannel" );
  308. $this->assertOutputPrePostShutdown( "foobar", true );
  309. }
  310. function testOutputWChannelStringNLStringNLArbitraryAgain() {
  311. $this->m->output( "", "bazChannel" );
  312. $this->m->output( "foo", "bazChannel" );
  313. $this->m->output( "", "bazChannel" );
  314. $this->m->output( "\nb", "bazChannel" );
  315. $this->m->output( "a", "bazChannel" );
  316. $this->m->output( "", "bazChannel" );
  317. $this->m->output( "r\n", "bazChannel" );
  318. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  319. }
  320. function testOutputWMultipleChannelsChannelChange() {
  321. $this->m->output( "foo", "bazChannel" );
  322. $this->m->output( "bar", "bazChannel" );
  323. $this->m->output( "qux", "quuxChannel" );
  324. $this->m->output( "corge", "bazChannel" );
  325. $this->assertOutputPrePostShutdown( "foobar\nqux\ncorge", true );
  326. }
  327. function testOutputWMultipleChannelsChannelChangeNL() {
  328. $this->m->output( "foo", "bazChannel" );
  329. $this->m->output( "bar\n", "bazChannel" );
  330. $this->m->output( "qux\n", "quuxChannel" );
  331. $this->m->output( "corge", "bazChannel" );
  332. $this->assertOutputPrePostShutdown( "foobar\nqux\ncorge", true );
  333. }
  334. function testOutputWAndWOChannelStringStartWO() {
  335. $this->m->output( "foo" );
  336. $this->m->output( "bar", "bazChannel" );
  337. $this->m->output( "qux" );
  338. $this->m->output( "quux", "bazChannel" );
  339. $this->assertOutputPrePostShutdown( "foobar\nquxquux", true );
  340. }
  341. function testOutputWAndWOChannelStringStartW() {
  342. $this->m->output( "foo", "bazChannel" );
  343. $this->m->output( "bar" );
  344. $this->m->output( "qux", "bazChannel" );
  345. $this->m->output( "quux" );
  346. $this->assertOutputPrePostShutdown( "foo\nbarqux\nquux", false );
  347. }
  348. function testOutputWChannelTypeSwitch() {
  349. $this->m->output( "foo", 1 );
  350. $this->m->output( "bar", 1.0 );
  351. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  352. }
  353. function testOutputIntermittentEmpty() {
  354. $this->m->output( "foo" );
  355. $this->m->output( "" );
  356. $this->m->output( "bar" );
  357. $this->assertOutputPrePostShutdown( "foobar", false );
  358. }
  359. function testOutputIntermittentFalse() {
  360. $this->m->output( "foo" );
  361. $this->m->output( false );
  362. $this->m->output( "bar" );
  363. $this->assertOutputPrePostShutdown( "foobar", false );
  364. }
  365. function testOutputIntermittentFalseAfterOtherChannel() {
  366. $this->m->output( "qux", "quuxChannel" );
  367. $this->m->output( "foo" );
  368. $this->m->output( false );
  369. $this->m->output( "bar" );
  370. $this->assertOutputPrePostShutdown( "qux\nfoobar", false );
  371. }
  372. function testOutputWNullChannelIntermittentEmpty() {
  373. $this->m->output( "foo", null );
  374. $this->m->output( "", null );
  375. $this->m->output( "bar", null );
  376. $this->assertOutputPrePostShutdown( "foobar", false );
  377. }
  378. function testOutputWNullChannelIntermittentFalse() {
  379. $this->m->output( "foo", null );
  380. $this->m->output( false, null );
  381. $this->m->output( "bar", null );
  382. $this->assertOutputPrePostShutdown( "foobar", false );
  383. }
  384. function testOutputWChannelIntermittentEmpty() {
  385. $this->m->output( "foo", "bazChannel" );
  386. $this->m->output( "", "bazChannel" );
  387. $this->m->output( "bar", "bazChannel" );
  388. $this->assertOutputPrePostShutdown( "foobar", true );
  389. }
  390. function testOutputWChannelIntermittentFalse() {
  391. $this->m->output( "foo", "bazChannel" );
  392. $this->m->output( false, "bazChannel" );
  393. $this->m->output( "bar", "bazChannel" );
  394. $this->assertOutputPrePostShutdown( "foobar", true );
  395. }
  396. // Note that (per documentation) outputChanneled does take strings that end
  397. // in \n, hence we do not test such strings.
  398. function testOutputChanneledEmpty() {
  399. $this->m->outputChanneled( "" );
  400. $this->assertOutputPrePostShutdown( "\n", false );
  401. }
  402. function testOutputChanneledString() {
  403. $this->m->outputChanneled( "foo" );
  404. $this->assertOutputPrePostShutdown( "foo\n", false );
  405. }
  406. function testOutputChanneledStringString() {
  407. $this->m->outputChanneled( "foo" );
  408. $this->m->outputChanneled( "bar" );
  409. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  410. }
  411. function testOutputChanneledStringNLString() {
  412. $this->m->outputChanneled( "foo\nbar" );
  413. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  414. }
  415. function testOutputChanneledStringNLStringNLArbitraryAgain() {
  416. $this->m->outputChanneled( "" );
  417. $this->m->outputChanneled( "foo" );
  418. $this->m->outputChanneled( "" );
  419. $this->m->outputChanneled( "\nb" );
  420. $this->m->outputChanneled( "a" );
  421. $this->m->outputChanneled( "" );
  422. $this->m->outputChanneled( "r" );
  423. $this->assertOutputPrePostShutdown( "\nfoo\n\n\nb\na\n\nr\n", false );
  424. }
  425. function testOutputChanneledWNullChannelEmpty() {
  426. $this->m->outputChanneled( "", null );
  427. $this->assertOutputPrePostShutdown( "\n", false );
  428. }
  429. function testOutputChanneledWNullChannelString() {
  430. $this->m->outputChanneled( "foo", null );
  431. $this->assertOutputPrePostShutdown( "foo\n", false );
  432. }
  433. function testOutputChanneledWNullChannelStringString() {
  434. $this->m->outputChanneled( "foo", null );
  435. $this->m->outputChanneled( "bar", null );
  436. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  437. }
  438. function testOutputChanneledWNullChannelStringNLString() {
  439. $this->m->outputChanneled( "foo\nbar", null );
  440. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  441. }
  442. function testOutputChanneledWNullChannelStringNLStringNLArbitraryAgain() {
  443. $this->m->outputChanneled( "", null );
  444. $this->m->outputChanneled( "foo", null );
  445. $this->m->outputChanneled( "", null );
  446. $this->m->outputChanneled( "\nb", null );
  447. $this->m->outputChanneled( "a", null );
  448. $this->m->outputChanneled( "", null );
  449. $this->m->outputChanneled( "r", null );
  450. $this->assertOutputPrePostShutdown( "\nfoo\n\n\nb\na\n\nr\n", false );
  451. }
  452. function testOutputChanneledWChannelString() {
  453. $this->m->outputChanneled( "foo", "bazChannel" );
  454. $this->assertOutputPrePostShutdown( "foo", true );
  455. }
  456. function testOutputChanneledWChannelStringNLString() {
  457. $this->m->outputChanneled( "foo\nbar", "bazChannel" );
  458. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  459. }
  460. function testOutputChanneledWChannelStringString() {
  461. $this->m->outputChanneled( "foo", "bazChannel" );
  462. $this->m->outputChanneled( "bar", "bazChannel" );
  463. $this->assertOutputPrePostShutdown( "foobar", true );
  464. }
  465. function testOutputChanneledWChannelStringNLStringNLArbitraryAgain() {
  466. $this->m->outputChanneled( "", "bazChannel" );
  467. $this->m->outputChanneled( "foo", "bazChannel" );
  468. $this->m->outputChanneled( "", "bazChannel" );
  469. $this->m->outputChanneled( "\nb", "bazChannel" );
  470. $this->m->outputChanneled( "a", "bazChannel" );
  471. $this->m->outputChanneled( "", "bazChannel" );
  472. $this->m->outputChanneled( "r", "bazChannel" );
  473. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  474. }
  475. function testOutputChanneledWMultipleChannelsChannelChange() {
  476. $this->m->outputChanneled( "foo", "bazChannel" );
  477. $this->m->outputChanneled( "bar", "bazChannel" );
  478. $this->m->outputChanneled( "qux", "quuxChannel" );
  479. $this->m->outputChanneled( "corge", "bazChannel" );
  480. $this->assertOutputPrePostShutdown( "foobar\nqux\ncorge", true );
  481. }
  482. function testOutputChanneledWMultipleChannelsChannelChangeEnclosedNull() {
  483. $this->m->outputChanneled( "foo", "bazChannel" );
  484. $this->m->outputChanneled( "bar", null );
  485. $this->m->outputChanneled( "qux", null );
  486. $this->m->outputChanneled( "corge", "bazChannel" );
  487. $this->assertOutputPrePostShutdown( "foo\nbar\nqux\ncorge", true );
  488. }
  489. function testOutputChanneledWMultipleChannelsChannelAfterNullChange() {
  490. $this->m->outputChanneled( "foo", "bazChannel" );
  491. $this->m->outputChanneled( "bar", null );
  492. $this->m->outputChanneled( "qux", null );
  493. $this->m->outputChanneled( "corge", "quuxChannel" );
  494. $this->assertOutputPrePostShutdown( "foo\nbar\nqux\ncorge", true );
  495. }
  496. function testOutputChanneledWAndWOChannelStringStartWO() {
  497. $this->m->outputChanneled( "foo" );
  498. $this->m->outputChanneled( "bar", "bazChannel" );
  499. $this->m->outputChanneled( "qux" );
  500. $this->m->outputChanneled( "quux", "bazChannel" );
  501. $this->assertOutputPrePostShutdown( "foo\nbar\nqux\nquux", true );
  502. }
  503. function testOutputChanneledWAndWOChannelStringStartW() {
  504. $this->m->outputChanneled( "foo", "bazChannel" );
  505. $this->m->outputChanneled( "bar" );
  506. $this->m->outputChanneled( "qux", "bazChannel" );
  507. $this->m->outputChanneled( "quux" );
  508. $this->assertOutputPrePostShutdown( "foo\nbar\nqux\nquux\n", false );
  509. }
  510. function testOutputChanneledWChannelTypeSwitch() {
  511. $this->m->outputChanneled( "foo", 1 );
  512. $this->m->outputChanneled( "bar", 1.0 );
  513. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  514. }
  515. function testOutputChanneledWOChannelIntermittentEmpty() {
  516. $this->m->outputChanneled( "foo" );
  517. $this->m->outputChanneled( "" );
  518. $this->m->outputChanneled( "bar" );
  519. $this->assertOutputPrePostShutdown( "foo\n\nbar\n", false );
  520. }
  521. function testOutputChanneledWOChannelIntermittentFalse() {
  522. $this->m->outputChanneled( "foo" );
  523. $this->m->outputChanneled( false );
  524. $this->m->outputChanneled( "bar" );
  525. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  526. }
  527. function testOutputChanneledWNullChannelIntermittentEmpty() {
  528. $this->m->outputChanneled( "foo", null );
  529. $this->m->outputChanneled( "", null );
  530. $this->m->outputChanneled( "bar", null );
  531. $this->assertOutputPrePostShutdown( "foo\n\nbar\n", false );
  532. }
  533. function testOutputChanneledWNullChannelIntermittentFalse() {
  534. $this->m->outputChanneled( "foo", null );
  535. $this->m->outputChanneled( false, null );
  536. $this->m->outputChanneled( "bar", null );
  537. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  538. }
  539. function testOutputChanneledWChannelIntermittentEmpty() {
  540. $this->m->outputChanneled( "foo", "bazChannel" );
  541. $this->m->outputChanneled( "", "bazChannel" );
  542. $this->m->outputChanneled( "bar", "bazChannel" );
  543. $this->assertOutputPrePostShutdown( "foobar", true );
  544. }
  545. function testOutputChanneledWChannelIntermittentFalse() {
  546. $this->m->outputChanneled( "foo", "bazChannel" );
  547. $this->m->outputChanneled( false, "bazChannel" );
  548. $this->m->outputChanneled( "bar", "bazChannel" );
  549. $this->assertOutputPrePostShutdown( "foo\nbar", true );
  550. }
  551. function testCleanupChanneledClean() {
  552. $this->m->cleanupChanneled();
  553. $this->assertOutputPrePostShutdown( "", false );
  554. }
  555. function testCleanupChanneledAfterOutput() {
  556. $this->m->output( "foo" );
  557. $this->m->cleanupChanneled();
  558. $this->assertOutputPrePostShutdown( "foo", false );
  559. }
  560. function testCleanupChanneledAfterOutputWNullChannel() {
  561. $this->m->output( "foo", null );
  562. $this->m->cleanupChanneled();
  563. $this->assertOutputPrePostShutdown( "foo", false );
  564. }
  565. function testCleanupChanneledAfterOutputWChannel() {
  566. $this->m->output( "foo", "bazChannel" );
  567. $this->m->cleanupChanneled();
  568. $this->assertOutputPrePostShutdown( "foo\n", false );
  569. }
  570. function testCleanupChanneledAfterNLOutput() {
  571. $this->m->output( "foo\n" );
  572. $this->m->cleanupChanneled();
  573. $this->assertOutputPrePostShutdown( "foo\n", false );
  574. }
  575. function testCleanupChanneledAfterNLOutputWNullChannel() {
  576. $this->m->output( "foo\n", null );
  577. $this->m->cleanupChanneled();
  578. $this->assertOutputPrePostShutdown( "foo\n", false );
  579. }
  580. function testCleanupChanneledAfterNLOutputWChannel() {
  581. $this->m->output( "foo\n", "bazChannel" );
  582. $this->m->cleanupChanneled();
  583. $this->assertOutputPrePostShutdown( "foo\n", false );
  584. }
  585. function testCleanupChanneledAfterOutputChanneledWOChannel() {
  586. $this->m->outputChanneled( "foo" );
  587. $this->m->cleanupChanneled();
  588. $this->assertOutputPrePostShutdown( "foo\n", false );
  589. }
  590. function testCleanupChanneledAfterOutputChanneledWNullChannel() {
  591. $this->m->outputChanneled( "foo", null );
  592. $this->m->cleanupChanneled();
  593. $this->assertOutputPrePostShutdown( "foo\n", false );
  594. }
  595. function testCleanupChanneledAfterOutputChanneledWChannel() {
  596. $this->m->outputChanneled( "foo", "bazChannel" );
  597. $this->m->cleanupChanneled();
  598. $this->assertOutputPrePostShutdown( "foo\n", false );
  599. }
  600. function testMultipleMaintenanceObjectsInteractionOutput() {
  601. $m2 = new MaintenanceFixup( $this );
  602. $this->m->output( "foo" );
  603. $m2->output( "bar" );
  604. $this->assertEquals( "foobar", $this->getActualOutput(),
  605. "Output before shutdown simulation (m2)" );
  606. $m2->simulateShutdown();
  607. $this->assertOutputPrePostShutdown( "foobar", false );
  608. }
  609. function testMultipleMaintenanceObjectsInteractionOutputWNullChannel() {
  610. $m2 = new MaintenanceFixup( $this );
  611. $this->m->output( "foo", null );
  612. $m2->output( "bar", null );
  613. $this->assertEquals( "foobar", $this->getActualOutput(),
  614. "Output before shutdown simulation (m2)" );
  615. $m2->simulateShutdown();
  616. $this->assertOutputPrePostShutdown( "foobar", false );
  617. }
  618. function testMultipleMaintenanceObjectsInteractionOutputWChannel() {
  619. $m2 = new MaintenanceFixup( $this );
  620. $this->m->output( "foo", "bazChannel" );
  621. $m2->output( "bar", "bazChannel" );
  622. $this->assertEquals( "foobar", $this->getActualOutput(),
  623. "Output before shutdown simulation (m2)" );
  624. $m2->simulateShutdown();
  625. $this->assertOutputPrePostShutdown( "foobar\n", true );
  626. }
  627. function testMultipleMaintenanceObjectsInteractionOutputWNullChannelNL() {
  628. $m2 = new MaintenanceFixup( $this );
  629. $this->m->output( "foo\n", null );
  630. $m2->output( "bar\n", null );
  631. $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
  632. "Output before shutdown simulation (m2)" );
  633. $m2->simulateShutdown();
  634. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  635. }
  636. function testMultipleMaintenanceObjectsInteractionOutputWChannelNL() {
  637. $m2 = new MaintenanceFixup( $this );
  638. $this->m->output( "foo\n", "bazChannel" );
  639. $m2->output( "bar\n", "bazChannel" );
  640. $this->assertEquals( "foobar", $this->getActualOutput(),
  641. "Output before shutdown simulation (m2)" );
  642. $m2->simulateShutdown();
  643. $this->assertOutputPrePostShutdown( "foobar\n", true );
  644. }
  645. function testMultipleMaintenanceObjectsInteractionOutputChanneled() {
  646. $m2 = new MaintenanceFixup( $this );
  647. $this->m->outputChanneled( "foo" );
  648. $m2->outputChanneled( "bar" );
  649. $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
  650. "Output before shutdown simulation (m2)" );
  651. $m2->simulateShutdown();
  652. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  653. }
  654. function testMultipleMaintenanceObjectsInteractionOutputChanneledWNullChannel() {
  655. $m2 = new MaintenanceFixup( $this );
  656. $this->m->outputChanneled( "foo", null );
  657. $m2->outputChanneled( "bar", null );
  658. $this->assertEquals( "foo\nbar\n", $this->getActualOutput(),
  659. "Output before shutdown simulation (m2)" );
  660. $m2->simulateShutdown();
  661. $this->assertOutputPrePostShutdown( "foo\nbar\n", false );
  662. }
  663. function testMultipleMaintenanceObjectsInteractionOutputChanneledWChannel() {
  664. $m2 = new MaintenanceFixup( $this );
  665. $this->m->outputChanneled( "foo", "bazChannel" );
  666. $m2->outputChanneled( "bar", "bazChannel" );
  667. $this->assertEquals( "foobar", $this->getActualOutput(),
  668. "Output before shutdown simulation (m2)" );
  669. $m2->simulateShutdown();
  670. $this->assertOutputPrePostShutdown( "foobar\n", true );
  671. }
  672. function testMultipleMaintenanceObjectsInteractionCleanupChanneledWChannel() {
  673. $m2 = new MaintenanceFixup( $this );
  674. $this->m->outputChanneled( "foo", "bazChannel" );
  675. $m2->outputChanneled( "bar", "bazChannel" );
  676. $this->assertEquals( "foobar", $this->getActualOutput(),
  677. "Output before first cleanup" );
  678. $this->m->cleanupChanneled();
  679. $this->assertEquals( "foobar\n", $this->getActualOutput(),
  680. "Output after first cleanup" );
  681. $m2->cleanupChanneled();
  682. $this->assertEquals( "foobar\n\n", $this->getActualOutput(),
  683. "Output after second cleanup" );
  684. $m2->simulateShutdown();
  685. $this->assertOutputPrePostShutdown( "foobar\n\n", false );
  686. }
  687. /**
  688. * @covers Maintenance::getConfig
  689. */
  690. public function testGetConfig() {
  691. $this->assertInstanceOf( 'Config', $this->m->getConfig() );
  692. $this->assertSame(
  693. ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
  694. $this->m->getConfig()
  695. );
  696. }
  697. /**
  698. * @covers Maintenance::setConfig
  699. */
  700. public function testSetConfig() {
  701. $conf = $this->getMock( 'Config' );
  702. $this->m->setConfig( $conf );
  703. $this->assertSame( $conf, $this->m->getConfig() );
  704. }
  705. function testParseArgs() {
  706. $m2 = new MaintenanceFixup( $this );
  707. // Create an option with an argument allowed to be specified multiple times
  708. $m2->addOption( 'multi', 'This option does stuff', false, true, false, true );
  709. $m2->loadWithArgv( [ '--multi', 'this1', '--multi', 'this2' ] );
  710. $this->assertEquals( [ 'this1', 'this2' ], $m2->getOption( 'multi' ) );
  711. $this->assertEquals( [ [ 'multi', 'this1' ], [ 'multi', 'this2' ] ],
  712. $m2->orderedOptions );
  713. $m2->simulateShutdown();
  714. $m2 = new MaintenanceFixup( $this );
  715. $m2->addOption( 'multi', 'This option does stuff', false, false, false, true );
  716. $m2->loadWithArgv( [ '--multi', '--multi' ] );
  717. $this->assertEquals( [ 1, 1 ], $m2->getOption( 'multi' ) );
  718. $this->assertEquals( [ [ 'multi', 1 ], [ 'multi', 1 ] ], $m2->orderedOptions );
  719. $m2->simulateShutdown();
  720. $m2 = new MaintenanceFixup( $this );
  721. // Create an option with an argument allowed to be specified multiple times
  722. $m2->addOption( 'multi', 'This option doesn\'t actually support multiple occurrences' );
  723. $m2->loadWithArgv( [ '--multi=yo' ] );
  724. $this->assertEquals( 'yo', $m2->getOption( 'multi' ) );
  725. $this->assertEquals( [ [ 'multi', 'yo' ] ], $m2->orderedOptions );
  726. $m2->simulateShutdown();
  727. }
  728. }