| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FetchText; |
| 7 |
use MediaWiki\Content\ContentHandler; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
use PHPUnit\Framework\ExpectationFailedException; |
| 12 |
use RuntimeException; |
| 13 |
use WikiPage; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class SemiMockedFetchText extends FetchText { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private $mockStdinText = null; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private $mockSetUp = false; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private $mockInvocations = [ 'getStdin' => 0 ]; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function mockStdin( $stdin ) { |
| 44 |
$this->mockStdinText = $stdin; |
| 45 |
$this->mockSetUp = true; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function mockGetInvocations() { |
| 55 |
return $this->mockInvocations; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getStdin( $len = null ) { |
| 62 |
$this->mockInvocations['getStdin']++; |
| 63 |
if ( $len !== null ) { |
| 64 |
throw new ExpectationFailedException( |
| 65 |
"Tried to get stdin with non null parameter" ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( !$this->mockSetUp ) { |
| 69 |
throw new ExpectationFailedException( |
| 70 |
"Tried to get stdin before setting up rerouting" ); |
| 71 |
} |
| 72 |
|
| 73 |
return fopen( 'data://text/plain,' . $this->mockStdinText, 'r' ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
class FetchTextTest extends MediaWikiIntegrationTestCase { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected static $textId1; |
| 90 |
|
| 91 |
protected static $textId2; |
| 92 |
|
| 93 |
protected static $textId3; |
| 94 |
|
| 95 |
protected static $textId4; |
| 96 |
|
| 97 |
protected static $textId5; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected static $exceptionFromAddDBDataOnce; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private $fetchText; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private function addRevision( $page, $text, $summary ) { |
| 122 |
$status = $page->doUserEditContent( |
| 123 |
ContentHandler::makeContent( $text, $page->getTitle() ), |
| 124 |
$this->getTestSysop()->getUser(), |
| 125 |
$summary |
| 126 |
); |
| 127 |
|
| 128 |
if ( $status->isGood() ) { |
| 129 |
$revision = $status->getNewRevision(); |
| 130 |
$address = $revision->getSlot( SlotRecord::MAIN )->getAddress(); |
| 131 |
return $address; |
| 132 |
} |
| 133 |
|
| 134 |
throw new RuntimeException( "Could not create revision" ); |
| 135 |
} |
| 136 |
|
| 137 |
public function addDBDataOnce() { |
| 138 |
$wikitextNamespace = $this->getDefaultWikitextNS(); |
| 139 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 140 |
|
| 141 |
try { |
| 142 |
$title = Title::makeTitle( $wikitextNamespace, 'FetchTextTestPage1' ); |
| 143 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 144 |
self::$textId1 = $this->addRevision( |
| 145 |
$page, |
| 146 |
"FetchTextTestPage1Text1", |
| 147 |
"FetchTextTestPage1Summary1" |
| 148 |
); |
| 149 |
|
| 150 |
$title = Title::makeTitle( $wikitextNamespace, 'FetchTextTestPage2' ); |
| 151 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 152 |
self::$textId2 = $this->addRevision( |
| 153 |
$page, |
| 154 |
"FetchTextTestPage2Text1", |
| 155 |
"FetchTextTestPage2Summary1" |
| 156 |
); |
| 157 |
self::$textId3 = $this->addRevision( |
| 158 |
$page, |
| 159 |
"FetchTextTestPage2Text2", |
| 160 |
"FetchTextTestPage2Summary2" |
| 161 |
); |
| 162 |
self::$textId4 = $this->addRevision( |
| 163 |
$page, |
| 164 |
"FetchTextTestPage2Text3", |
| 165 |
"FetchTextTestPage2Summary3" |
| 166 |
); |
| 167 |
self::$textId5 = $this->addRevision( |
| 168 |
$page, |
| 169 |
"FetchTextTestPage2Text4 some additional Text ", |
| 170 |
"FetchTextTestPage2Summary4 extra " |
| 171 |
); |
| 172 |
} catch ( Exception $e ) { |
| 173 |
|
| 174 |
|
| 175 |
self::$exceptionFromAddDBDataOnce = $e; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
protected function setUp(): void { |
| 180 |
parent::setUp(); |
| 181 |
|
| 182 |
|
| 183 |
if ( self::$exceptionFromAddDBDataOnce !== null ) { |
| 184 |
throw self::$exceptionFromAddDBDataOnce; |
| 185 |
} |
| 186 |
|
| 187 |
$this->fetchText = new SemiMockedFetchText(); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function assertFilter( $input, $expectedOutput ) { |
| 196 |
$this->fetchText->mockStdin( $input ); |
| 197 |
$this->fetchText->execute(); |
| 198 |
$invocations = $this->fetchText->mockGetInvocations(); |
| 199 |
$this->assertSame( 1, $invocations['getStdin'], |
| 200 |
"getStdin invocation counter" ); |
| 201 |
$this->expectOutputString( $expectedOutput ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function testExistingSimple() { |
| 209 |
$this->assertFilter( self::$textId2, |
| 210 |
self::$textId2 . "\n23\nFetchTextTestPage2Text1" ); |
| 211 |
} |
| 212 |
|
| 213 |
public function testExistingSimpleWithNewline() { |
| 214 |
$this->assertFilter( self::$textId2 . "\n", |
| 215 |
self::$textId2 . "\n23\nFetchTextTestPage2Text1" ); |
| 216 |
} |
| 217 |
|
| 218 |
public function testExistingInteger() { |
| 219 |
$this->assertFilter( (int)preg_replace( '/^tt:/', '', self::$textId2 ), |
| 220 |
self::$textId2 . "\n23\nFetchTextTestPage2Text1" ); |
| 221 |
} |
| 222 |
|
| 223 |
public function testExistingSeveral() { |
| 224 |
$this->assertFilter( |
| 225 |
implode( "\n", [ |
| 226 |
self::$textId1, |
| 227 |
self::$textId5, |
| 228 |
self::$textId3, |
| 229 |
self::$textId3, |
| 230 |
] ), |
| 231 |
implode( '', [ |
| 232 |
self::$textId1 . "\n23\nFetchTextTestPage1Text1", |
| 233 |
self::$textId5 . "\n44\nFetchTextTestPage2Text4 " |
| 234 |
. "some additional Text", |
| 235 |
self::$textId3 . "\n23\nFetchTextTestPage2Text2", |
| 236 |
self::$textId3 . "\n23\nFetchTextTestPage2Text2" |
| 237 |
] ) ); |
| 238 |
} |
| 239 |
|
| 240 |
public function testEmpty() { |
| 241 |
$this->assertFilter( "", "" ); |
| 242 |
} |
| 243 |
|
| 244 |
public function testNonExisting() { |
| 245 |
@$this->assertFilter( 'tt:77889911', 'tt:77889911' . "\n-1\n" ); |
| 246 |
} |
| 247 |
|
| 248 |
public function testNonExistingInteger() { |
| 249 |
@$this->assertFilter( '77889911', 'tt:77889911' . "\n-1\n" ); |
| 250 |
} |
| 251 |
|
| 252 |
public function testBadBlobAddressWithColon() { |
| 253 |
$this->assertFilter( 'foo:bar', 'foo:bar' . "\n-1\n" ); |
| 254 |
} |
| 255 |
|
| 256 |
public function testNegativeInteger() { |
| 257 |
$this->assertFilter( "-42", "tt:-42\n-1\n" ); |
| 258 |
} |
| 259 |
|
| 260 |
public function testFloatingPointNumberExisting() { |
| 261 |
|
| 262 |
$id = intval( preg_replace( '/^tt:/', '', self::$textId3 ) ) + 0.14159; |
| 263 |
$this->assertFilter( 'tt:' . intval( $id ), |
| 264 |
self::$textId3 . "\n23\nFetchTextTestPage2Text2" ); |
| 265 |
} |
| 266 |
|
| 267 |
public function testFloatingPointNumberNonExisting() { |
| 268 |
$id = intval( preg_replace( '/^tt:/', '', self::$textId5 ) ) + 3.14159; |
| 269 |
@$this->assertFilter( $id, 'tt:' . intval( $id ) . "\n-1\n" ); |
| 270 |
} |
| 271 |
|
| 272 |
public function testCharacters() { |
| 273 |
$this->assertFilter( "abc", "abc\n-1\n" ); |
| 274 |
} |
| 275 |
|
| 276 |
public function testMix() { |
| 277 |
$this->assertFilter( "ab\n" . self::$textId4 . ".5cd\n\nefg\nfoo:bar\n" . self::$textId2 |
| 278 |
. "\n" . self::$textId3, |
| 279 |
implode( "", [ |
| 280 |
"ab\n-1\n", |
| 281 |
self::$textId4 . ".5cd\n-1\n", |
| 282 |
"\n-1\n", |
| 283 |
"efg\n-1\n", |
| 284 |
"foo:bar\n-1\n", |
| 285 |
self::$textId2 . "\n23\nFetchTextTestPage2Text1", |
| 286 |
self::$textId3 . "\n23\nFetchTextTestPage2Text2" |
| 287 |
] ) ); |
| 288 |
} |
| 289 |
} |