| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use GetTextMaint; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\Revision\RevisionRecord; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use RevisionDeleter; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class GetTextMaintTest extends MaintenanceBaseTestCase { |
| 17 |
|
| 18 |
protected function getMaintenanceClass() { |
| 19 |
return GetTextMaint::class; |
| 20 |
} |
| 21 |
|
| 22 |
public function testExecute() { |
| 23 |
$testPage = $this->getExistingTestPage(); |
| 24 |
|
| 25 |
$this->maintenance->setArg( 'title', $testPage ); |
| 26 |
$this->maintenance->execute(); |
| 27 |
|
| 28 |
$expectedOutput = $testPage->getContent( RevisionRecord::RAW )->serialize(); |
| 29 |
if ( stream_isatty( STDOUT ) ) { |
| 30 |
$expectedOutput .= "\n"; |
| 31 |
} |
| 32 |
$this->expectOutputString( $expectedOutput ); |
| 33 |
} |
| 34 |
|
| 35 |
public function testExecuteForOldRevision() { |
| 36 |
|
| 37 |
$testPage = $this->getExistingTestPage(); |
| 38 |
$firstRevContent = $testPage->getContent( RevisionRecord::RAW )->serialize(); |
| 39 |
$this->editPage( $testPage, 'testing1234' ); |
| 40 |
|
| 41 |
$this->maintenance->setArg( 'title', $testPage ); |
| 42 |
$this->maintenance->setOption( |
| 43 |
'revision', $this->getServiceContainer()->getRevisionLookup()->getFirstRevision( $testPage )->getId() |
| 44 |
); |
| 45 |
$this->maintenance->execute(); |
| 46 |
|
| 47 |
$expectedOutput = $firstRevContent; |
| 48 |
if ( stream_isatty( STDOUT ) ) { |
| 49 |
$expectedOutput .= "\n"; |
| 50 |
} |
| 51 |
$this->expectOutputString( $expectedOutput ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public function testExecuteForInvalidTitleArgument( $title, $expectedOutputRegex ) { |
| 56 |
$this->expectCallToFatalError(); |
| 57 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 58 |
$this->maintenance->setArg( 'title', $title ); |
| 59 |
$this->maintenance->execute(); |
| 60 |
} |
| 61 |
|
| 62 |
public static function provideInvalidTitleArgumentValues() { |
| 63 |
return [ |
| 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/', |
| 67 |
], |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
public function testExecuteForMissingRevId() { |
| 72 |
$testPage = $this->getExistingTestPage(); |
| 73 |
|
| 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(); |
| 79 |
} |
| 80 |
|
| 81 |
private function getPageWithSuppressedFirstRevision() { |
| 82 |
|
| 83 |
$testPage = $this->getExistingTestPage(); |
| 84 |
$firstRevId = $testPage->getRevisionRecord()->getId(); |
| 85 |
$this->editPage( $testPage, 'testing123456' ); |
| 86 |
|
| 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 ]; |
| 91 |
} |
| 92 |
|
| 93 |
public function testExecuteForSuppressedContent() { |
| 94 |
[ 'page' => $testPage, 'revId' => $firstRevId ] = $this->getPageWithSuppressedFirstRevision(); |
| 95 |
|
| 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(); |
| 101 |
} |
| 102 |
|
| 103 |
public function testExecuteForSuppressedContentWithShowPrivateSet() { |
| 104 |
[ 'page' => $testPage, 'revId' => $firstRevId ] = $this->getPageWithSuppressedFirstRevision(); |
| 105 |
|
| 106 |
|
| 107 |
$this->maintenance->setArg( 'title', $testPage ); |
| 108 |
$this->maintenance->setOption( 'revision', $firstRevId ); |
| 109 |
$this->maintenance->setOption( 'show-private', 1 ); |
| 110 |
$this->maintenance->execute(); |
| 111 |
|
| 112 |
$expectedOutput = $this->getServiceContainer()->getRevisionStore() |
| 113 |
->getRevisionById( $firstRevId ) |
| 114 |
->getContent( SlotRecord::MAIN, RevisionRecord::RAW ) |
| 115 |
->getWikitextForTransclusion(); |
| 116 |
if ( stream_isatty( STDOUT ) ) { |
| 117 |
$expectedOutput .= "\n"; |
| 118 |
} |
| 119 |
$this->expectOutputString( $expectedOutput ); |
| 120 |
} |
| 121 |
} |