3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\Context\RequestContext
;
7 use MediaWiki\Revision\RevisionRecord
;
8 use MediaWiki\Revision\SlotRecord
;
12 * @covers \GetTextMaint
16 class GetTextMaintTest
extends MaintenanceBaseTestCase
{
18 protected function getMaintenanceClass() {
19 return GetTextMaint
::class;
22 public function testExecute() {
23 $testPage = $this->getExistingTestPage();
25 $this->maintenance
->setArg( 'title', $testPage );
26 $this->maintenance
->execute();
27 // Verify that the content of the last revision of the page was outputted.
28 $expectedOutput = $testPage->getContent( RevisionRecord
::RAW
)->serialize();
29 if ( stream_isatty( STDOUT
) ) {
30 $expectedOutput .= "\n";
32 $this->expectOutputString( $expectedOutput );
35 public function testExecuteForOldRevision() {
36 // Get a test page with two revisions
37 $testPage = $this->getExistingTestPage();
38 $firstRevContent = $testPage->getContent( RevisionRecord
::RAW
)->serialize();
39 $this->editPage( $testPage, 'testing1234' );
41 $this->maintenance
->setArg( 'title', $testPage );
42 $this->maintenance
->setOption(
43 'revision', $this->getServiceContainer()->getRevisionLookup()->getFirstRevision( $testPage )->getId()
45 $this->maintenance
->execute();
46 // Verify that the content of the first revision of the page was outputted.
47 $expectedOutput = $firstRevContent;
48 if ( stream_isatty( STDOUT
) ) {
49 $expectedOutput .= "\n";
51 $this->expectOutputString( $expectedOutput );
54 /** @dataProvider provideInvalidTitleArgumentValues */
55 public function testExecuteForInvalidTitleArgument( $title, $expectedOutputRegex ) {
56 $this->expectCallToFatalError();
57 $this->expectOutputRegex( $expectedOutputRegex );
58 $this->maintenance
->setArg( 'title', $title );
59 $this->maintenance
->execute();
62 public static function provideInvalidTitleArgumentValues() {
64 'Invalid title' => [ ':::', '/::: is not a valid title/' ],
65 'Title for a non-existent page' => [
66 'Non-existing-test-page-1234', '/Non-existing-test-page-1234 does not exist/',
71 public function testExecuteForMissingRevId() {
72 $testPage = $this->getExistingTestPage();
73 // Call ::execute with an existing test page, but for a revision ID which does not exist.
74 $this->maintenance
->setArg( 'title', $testPage );
75 $this->maintenance
->setOption( 'revision', 123456 );
76 $this->expectCallToFatalError();
77 $this->expectOutputRegex( "/Could not load revision 123456 of $testPage/" );
78 $this->maintenance
->execute();
81 private function getPageWithSuppressedFirstRevision() {
82 // Get a page with two revisions
83 $testPage = $this->getExistingTestPage();
84 $firstRevId = $testPage->getRevisionRecord()->getId();
85 $this->editPage( $testPage, 'testing123456' );
86 // Hide the first revision associated with the test page
87 RequestContext
::getMain()->setUser( $this->getTestUser()->getUser() );
88 $list = RevisionDeleter
::createList( 'revision', RequestContext
::getMain(), $testPage, [ $firstRevId ] );
89 $list->setVisibility( [ 'value' => [ RevisionRecord
::DELETED_TEXT
=> 1 ], 'comment' => 'Bye-bye' ] );
90 return [ 'page' => $testPage, 'revId' => $firstRevId ];
93 public function testExecuteForSuppressedContent() {
94 [ 'page' => $testPage, 'revId' => $firstRevId ] = $this->getPageWithSuppressedFirstRevision();
95 // Call ::execute for the suppressed revision, expecting that the script will fail to find the content.
96 $this->maintenance
->setArg( 'title', $testPage );
97 $this->maintenance
->setOption( 'revision', $firstRevId );
98 $this->expectCallToFatalError();
99 $this->expectOutputRegex( "/Couldn't extract the text from $testPage/" );
100 $this->maintenance
->execute();
103 public function testExecuteForSuppressedContentWithShowPrivateSet() {
104 [ 'page' => $testPage, 'revId' => $firstRevId ] = $this->getPageWithSuppressedFirstRevision();
105 // Call ::execute for the suppressed revision and the --show-private option, expecting that the script
107 $this->maintenance
->setArg( 'title', $testPage );
108 $this->maintenance
->setOption( 'revision', $firstRevId );
109 $this->maintenance
->setOption( 'show-private', 1 );
110 $this->maintenance
->execute();
111 // Verify that the content of the first revision of the page was outputted.
112 $expectedOutput = $this->getServiceContainer()->getRevisionStore()
113 ->getRevisionById( $firstRevId )
114 ->getContent( SlotRecord
::MAIN
, RevisionRecord
::RAW
)
115 ->getWikitextForTransclusion();
116 if ( stream_isatty( STDOUT
) ) {
117 $expectedOutput .= "\n";
119 $this->expectOutputString( $expectedOutput );