| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Tests\Unit; |
| 23 |
|
| 24 |
use Interwiki; |
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\Cache\CacheKeyHelper; |
| 27 |
use MediaWiki\Cache\GenderCache; |
| 28 |
use MediaWiki\CommentFormatter\CommentParser; |
| 29 |
use MediaWiki\CommentFormatter\CommentParserFactory; |
| 30 |
use MediaWiki\CommentStore\CommentStore; |
| 31 |
use MediaWiki\Config\ServiceOptions; |
| 32 |
use MediaWiki\Content\IContentHandlerFactory; |
| 33 |
use MediaWiki\Interwiki\InterwikiLookup; |
| 34 |
use MediaWiki\Language\Language; |
| 35 |
use MediaWiki\Languages\LanguageNameUtils; |
| 36 |
use MediaWiki\Linker\LinkTarget; |
| 37 |
use MediaWiki\MainConfigSchema; |
| 38 |
use MediaWiki\Page\PageReference; |
| 39 |
use MediaWiki\Tests\MockDatabase; |
| 40 |
use MediaWiki\Title\MalformedTitleException; |
| 41 |
use MediaWiki\Title\MediaWikiTitleCodec; |
| 42 |
use MediaWiki\Title\NamespaceInfo; |
| 43 |
use MediaWiki\Title\TitleFormatter; |
| 44 |
use MediaWiki\Title\TitleParser; |
| 45 |
use MediaWiki\User\TempUser\RealTempUserConfig; |
| 46 |
use MediaWiki\User\UserIdentity; |
| 47 |
use MediaWiki\User\UserNameUtils; |
| 48 |
use MediaWiki\Watchlist\WatchedItem; |
| 49 |
use MediaWiki\Watchlist\WatchedItemStore; |
| 50 |
use PHPUnit\Framework\MockObject\MockObject; |
| 51 |
use Psr\Container\ContainerInterface; |
| 52 |
use Psr\Log\NullLogger; |
| 53 |
use Wikimedia\Message\ITextFormatter; |
| 54 |
use Wikimedia\Message\MessageSpecifier; |
| 55 |
use Wikimedia\Message\MessageValue; |
| 56 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 57 |
use Wikimedia\Rdbms\ConfiguredReadOnlyMode; |
| 58 |
use Wikimedia\Rdbms\ILBFactory; |
| 59 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 60 |
use Wikimedia\Rdbms\LBFactory; |
| 61 |
use Wikimedia\Rdbms\LBFactorySingle; |
| 62 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 63 |
use Wikimedia\Services\NoSuchServiceException; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
trait DummyServicesTrait { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private $watchedItemStoreData = []; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private static function getDefaultSettings(): array { |
| 92 |
static $defaultSettings = null; |
| 93 |
if ( $defaultSettings !== null ) { |
| 94 |
return $defaultSettings; |
| 95 |
} |
| 96 |
$defaultSettings = iterator_to_array( MainConfigSchema::listDefaultValues() ); |
| 97 |
return $defaultSettings; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private function getDummyCommentParserFactory( |
| 105 |
CommentParser $parser |
| 106 |
): CommentParserFactory { |
| 107 |
return new class( $parser ) extends CommentParserFactory { |
| 108 |
private $parser; |
| 109 |
|
| 110 |
public function __construct( $parser ) { |
| 111 |
$this->parser = $parser; |
| 112 |
} |
| 113 |
|
| 114 |
public function create() { |
| 115 |
return $this->parser; |
| 116 |
} |
| 117 |
}; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function getDummyContentHandlerFactory( |
| 129 |
array $contentHandlers = [], |
| 130 |
array $allContentFormats = [] |
| 131 |
): IContentHandlerFactory { |
| 132 |
$contentHandlerFactory = $this->createMock( IContentHandlerFactory::class ); |
| 133 |
$contentHandlerFactory->method( 'getContentHandler' ) |
| 134 |
->willReturnCallback( |
| 135 |
static function ( string $modelId ) use ( $contentHandlers ) { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
return $contentHandlers[ $modelId ]; |
| 140 |
} |
| 141 |
); |
| 142 |
$contentHandlerFactory->method( 'getContentModels' ) |
| 143 |
->willReturn( array_keys( $contentHandlers ) ); |
| 144 |
$contentHandlerFactory->method( 'getAllContentFormats' ) |
| 145 |
->willReturn( $allContentFormats ); |
| 146 |
$contentHandlerFactory->method( 'isDefinedModel' ) |
| 147 |
->willReturnCallback( |
| 148 |
static function ( string $modelId ) use ( $contentHandlers ) { |
| 149 |
return array_key_exists( $modelId, $contentHandlers ); |
| 150 |
} |
| 151 |
); |
| 152 |
return $contentHandlerFactory; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private function getDummyDBLoadBalancerFactory( $dbOptions = [] ): LBFactory { |
| 160 |
return LBFactorySingle::newFromConnection( new MockDatabase( $dbOptions ) ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private function getDummyInterwikiLookup( array $interwikis = [] ): InterwikiLookup { |
| 173 |
|
| 174 |
$allInterwikiRows = []; |
| 175 |
$defaultInterwiki = [ |
| 176 |
|
| 177 |
'iw_url' => '', |
| 178 |
'iw_api' => '', |
| 179 |
'iw_wikiid' => '', |
| 180 |
'iw_local' => 0, |
| 181 |
'iw_trans' => 0, |
| 182 |
]; |
| 183 |
foreach ( $interwikis as $validInterwiki ) { |
| 184 |
if ( is_string( $validInterwiki ) ) { |
| 185 |
|
| 186 |
$interwikiRow = [ 'iw_prefix' => $validInterwiki ] + $defaultInterwiki; |
| 187 |
} elseif ( is_array( $validInterwiki ) ) { |
| 188 |
if ( !isset( $validInterwiki['iw_prefix'] ) ) { |
| 189 |
throw new InvalidArgumentException( |
| 190 |
'Cannot save a valid interwiki without a prefix' |
| 191 |
); |
| 192 |
} |
| 193 |
$interwikiRow = $validInterwiki + $defaultInterwiki; |
| 194 |
} else { |
| 195 |
throw new InvalidArgumentException( |
| 196 |
'Interwikis must be in the form of a string or an array' |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
$allInterwikiRows[ $interwikiRow['iw_prefix'] ] = $interwikiRow; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
return new class( $allInterwikiRows ) implements InterwikiLookup { |
| 206 |
private $allInterwikiRows; |
| 207 |
|
| 208 |
public function __construct( $allInterwikiRows ) { |
| 209 |
$this->allInterwikiRows = $allInterwikiRows; |
| 210 |
} |
| 211 |
|
| 212 |
public function isValidInterwiki( $prefix ) { |
| 213 |
return (bool)$this->fetch( $prefix ); |
| 214 |
} |
| 215 |
|
| 216 |
public function fetch( $prefix ) { |
| 217 |
if ( $prefix == '' ) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$prefix = strtolower( $prefix ); |
| 226 |
if ( !isset( $this->allInterwikiRows[ $prefix ] ) ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
$row = $this->allInterwikiRows[ $prefix ]; |
| 231 |
return new Interwiki( |
| 232 |
$row['iw_prefix'], |
| 233 |
$row['iw_url'], |
| 234 |
$row['iw_api'], |
| 235 |
$row['iw_wikiid'], |
| 236 |
$row['iw_local'], |
| 237 |
$row['iw_trans'] |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
public function getAllPrefixes( $local = null ) { |
| 242 |
if ( $local === null ) { |
| 243 |
return array_values( $this->allInterwikiRows ); |
| 244 |
} |
| 245 |
return array_values( |
| 246 |
array_filter( |
| 247 |
$this->allInterwikiRows, |
| 248 |
static function ( $row ) use ( $local ) { |
| 249 |
return $row['iw_local'] == (int)$local; |
| 250 |
} |
| 251 |
) |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
public function invalidateCache( $prefix ) { |
| 256 |
|
| 257 |
} |
| 258 |
}; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private function getDummyLanguageNameUtils( array $options = [] ): LanguageNameUtils { |
| 270 |
|
| 271 |
$serviceOptions = new ServiceOptions( |
| 272 |
LanguageNameUtils::CONSTRUCTOR_OPTIONS, |
| 273 |
$options, |
| 274 |
self::getDefaultSettings() |
| 275 |
); |
| 276 |
return new LanguageNameUtils( |
| 277 |
$serviceOptions, |
| 278 |
$options['hookContainer'] ?? $this->createHookContainer() |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private function getDummyTitleFormatter( array $options = [] ): TitleFormatter { |
| 287 |
return $this->getDummyMediaWikiTitleCodec( $options ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
private function getDummyTitleParser( array $options = [] ): TitleParser { |
| 295 |
return $this->getDummyMediaWikiTitleCodec( $options ); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
private function getDummyMediaWikiTitleCodec( array $options = [] ): MediaWikiTitleCodec { |
| 321 |
$baseConfig = [ |
| 322 |
'validInterwikis' => [], |
| 323 |
'throwMockExceptions' => false, |
| 324 |
]; |
| 325 |
$config = $options + $baseConfig; |
| 326 |
|
| 327 |
$namespaceInfo = $this->getDummyNamespaceInfo( $options ); |
| 328 |
|
| 329 |
|
| 330 |
$language = $this->createMock( Language::class ); |
| 331 |
$language->method( 'ucfirst' )->willReturnCallback( 'ucfirst' ); |
| 332 |
$language->method( 'lc' )->willReturnCallback( |
| 333 |
static function ( $str, $first ) { |
| 334 |
return $first ? lcfirst( $str ) : strtolower( $str ); |
| 335 |
} |
| 336 |
); |
| 337 |
$language->method( 'getNsIndex' )->willReturnCallback( |
| 338 |
static function ( $text ) use ( $namespaceInfo ) { |
| 339 |
$text = strtolower( $text ); |
| 340 |
if ( $text === '' ) { |
| 341 |
return NS_MAIN; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
$index = $namespaceInfo->getCanonicalIndex( $text ); |
| 348 |
|
| 349 |
if ( $index !== null ) { |
| 350 |
return $index; |
| 351 |
} |
| 352 |
$aliases = [ |
| 353 |
'image' => NS_FILE, |
| 354 |
'image_talk' => NS_FILE, |
| 355 |
]; |
| 356 |
return $aliases[$text] ?? false; |
| 357 |
} |
| 358 |
); |
| 359 |
$language->method( 'getNsText' )->willReturnCallback( |
| 360 |
static function ( $index ) use ( $namespaceInfo ) { |
| 361 |
|
| 362 |
|
| 363 |
$namespaces = $namespaceInfo->getCanonicalNamespaces(); |
| 364 |
return $namespaces[$index] ?? false; |
| 365 |
} |
| 366 |
); |
| 367 |
|
| 368 |
|
| 369 |
$language->method( 'needsGenderDistinction' )->willReturn( false ); |
| 370 |
|
| 371 |
|
| 372 |
$genderCache = $this->createMock( GenderCache::class ); |
| 373 |
|
| 374 |
$interwikiLookup = $this->getDummyInterwikiLookup( $config['validInterwikis'] ); |
| 375 |
|
| 376 |
$titleCodec = new MediaWikiTitleCodec( |
| 377 |
$language, |
| 378 |
$genderCache, |
| 379 |
[ 'en' ], |
| 380 |
$interwikiLookup, |
| 381 |
$namespaceInfo |
| 382 |
); |
| 383 |
|
| 384 |
if ( $config['throwMockExceptions'] ) { |
| 385 |
|
| 386 |
|
| 387 |
$titleCodec->overrideCreateMalformedTitleExceptionCallback( |
| 388 |
function ( $errorMessage, $titleText = null, $errorMessageParameters = [] ) { |
| 389 |
return $this->createMock( MalformedTitleException::class ); |
| 390 |
} |
| 391 |
); |
| 392 |
} |
| 393 |
|
| 394 |
return $titleCodec; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
private function getDummyNamespaceInfo( array $options = [] ): NamespaceInfo { |
| 404 |
|
| 405 |
$serviceOptions = new ServiceOptions( |
| 406 |
NamespaceInfo::CONSTRUCTOR_OPTIONS, |
| 407 |
$options, |
| 408 |
self::getDefaultSettings() |
| 409 |
); |
| 410 |
return new NamespaceInfo( |
| 411 |
$serviceOptions, |
| 412 |
$options['hookContainer'] ?? $this->createHookContainer(), |
| 413 |
[], |
| 414 |
[] |
| 415 |
); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
private function getDummyObjectFactory( array $services = [] ): ObjectFactory { |
| 424 |
$container = $this->createMock( ContainerInterface::class ); |
| 425 |
$container->method( 'has' ) |
| 426 |
->willReturnCallback( static function ( $serviceName ) use ( $services ) { |
| 427 |
return array_key_exists( $serviceName, $services ); |
| 428 |
} ); |
| 429 |
$container->method( 'get' ) |
| 430 |
->willReturnCallback( static function ( $serviceName ) use ( $services ) { |
| 431 |
if ( array_key_exists( $serviceName, $services ) ) { |
| 432 |
return $services[$serviceName]; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
throw new NoSuchServiceException( $serviceName ); |
| 438 |
} ); |
| 439 |
return new ObjectFactory( $container ); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
private function getDummyReadOnlyMode( $startingReason ): ReadOnlyMode { |
| 448 |
if ( $startingReason === true ) { |
| 449 |
$startingReason = 'Random reason'; |
| 450 |
} |
| 451 |
$loadBalancer = $this->createMock( ILoadBalancer::class ); |
| 452 |
$loadBalancer->method( 'getReadOnlyReason' )->willReturn( false ); |
| 453 |
$lbFactory = $this->createMock( ILBFactory::class ); |
| 454 |
$lbFactory->method( 'getMainLB' )->willReturn( $loadBalancer ); |
| 455 |
return new ReadOnlyMode( |
| 456 |
new ConfiguredReadOnlyMode( $startingReason, null ), |
| 457 |
$lbFactory |
| 458 |
); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
private function getDummyTextFormatter( bool $dumpMessages = false ): ITextFormatter { |
| 467 |
return new class( $dumpMessages ) implements ITextFormatter { |
| 468 |
private bool $dumpMessages; |
| 469 |
|
| 470 |
public function __construct( bool $dumpMessages ) { |
| 471 |
$this->dumpMessages = $dumpMessages; |
| 472 |
} |
| 473 |
|
| 474 |
public function getLangCode() { |
| 475 |
return 'qqx'; |
| 476 |
} |
| 477 |
|
| 478 |
public function format( MessageSpecifier $message ): string { |
| 479 |
if ( $this->dumpMessages && $message instanceof MessageValue ) { |
| 480 |
return $message->dump(); |
| 481 |
} |
| 482 |
return $message->getKey(); |
| 483 |
} |
| 484 |
}; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
private function getDummyUserNameUtils( array $options = [] ) { |
| 502 |
$serviceOptions = new ServiceOptions( |
| 503 |
UserNameUtils::CONSTRUCTOR_OPTIONS, |
| 504 |
$options, |
| 505 |
self::getDefaultSettings() |
| 506 |
); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
$contentLang = $this->createMock( Language::class ); |
| 513 |
$contentLang->method( 'ucfirst' ) |
| 514 |
->willReturnCallback( 'ucfirst' ); |
| 515 |
$contentLang->method( 'getNsText' )->with( NS_USER ) |
| 516 |
->willReturn( 'User' ); |
| 517 |
|
| 518 |
$logger = $options['logger'] ?? new NullLogger(); |
| 519 |
|
| 520 |
$textFormatter = $options['textFormatter'] ?? $this->getDummyTextFormatter(); |
| 521 |
|
| 522 |
$titleParser = $options['titleParser'] ?? false; |
| 523 |
if ( !$titleParser ) { |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
$titleParser = $this->getDummyTitleParser( |
| 531 |
$options + [ |
| 532 |
'validInterwikis' => [ 'interwiki' ], |
| 533 |
'throwMockExceptions' => true |
| 534 |
] |
| 535 |
); |
| 536 |
} |
| 537 |
|
| 538 |
return new UserNameUtils( |
| 539 |
$serviceOptions, |
| 540 |
$contentLang, |
| 541 |
$logger, |
| 542 |
$titleParser, |
| 543 |
$textFormatter, |
| 544 |
$options['hookContainer'] ?? $this->createHookContainer(), |
| 545 |
$options['tempUserConfig'] ?? new RealTempUserConfig( [ |
| 546 |
'enabled' => true, |
| 547 |
'expireAfterDays' => null, |
| 548 |
'actions' => [ 'edit' ], |
| 549 |
'serialProvider' => [ 'type' => 'local' ], |
| 550 |
'serialMapping' => [ 'type' => 'plain-numeric' ], |
| 551 |
'reservedPattern' => '!$1', |
| 552 |
'matchPattern' => '*$1', |
| 553 |
'genPattern' => '*Unregistered $1' |
| 554 |
] ) |
| 555 |
); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
private function getWatchedItemStoreKey( UserIdentity $user, $page ): string { |
| 564 |
return 'u' . (string)$user->getId() . ':' . CacheKeyHelper::getKeyForPage( $page ); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private function getDummyWatchedItemStore() { |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
$mock = $this->createNoOpMock( |
| 578 |
WatchedItemStore::class, |
| 579 |
[ 'getWatchedItem', 'addWatch', 'removeWatch', 'isWatched', 'isTempWatched' ] |
| 580 |
); |
| 581 |
$mock->method( 'getWatchedItem' )->willReturnCallback( function ( $user, $target ) { |
| 582 |
$dataKey = $this->getWatchedItemStoreKey( $user, $target ); |
| 583 |
if ( isset( $this->watchedItemStoreData[ $dataKey ] ) ) { |
| 584 |
$expiry = $this->watchedItemStoreData[ $dataKey ]; |
| 585 |
|
| 586 |
|
| 587 |
$expiry = ( $expiry === true ? null : $expiry ); |
| 588 |
return new WatchedItem( |
| 589 |
$user, |
| 590 |
$target, |
| 591 |
null, |
| 592 |
$expiry |
| 593 |
); |
| 594 |
} |
| 595 |
return false; |
| 596 |
} ); |
| 597 |
$mock->method( 'addWatch' )->willReturnCallback( function ( $user, $target, $expiry ) { |
| 598 |
if ( !$user->isRegistered() ) { |
| 599 |
return false; |
| 600 |
} |
| 601 |
$dataKey = $this->getWatchedItemStoreKey( $user, $target ); |
| 602 |
$this->watchedItemStoreData[ $dataKey ] = ( $expiry === null ? true : $expiry ); |
| 603 |
return true; |
| 604 |
} ); |
| 605 |
$mock->method( 'removeWatch' )->willReturnCallback( function ( $user, $target ) { |
| 606 |
if ( !$user->isRegistered() ) { |
| 607 |
return false; |
| 608 |
} |
| 609 |
$dataKey = $this->getWatchedItemStoreKey( $user, $target ); |
| 610 |
if ( isset( $this->watchedItemStoreData[ $dataKey ] ) ) { |
| 611 |
unset( $this->watchedItemStoreData[ $dataKey ] ); |
| 612 |
return true; |
| 613 |
} |
| 614 |
return false; |
| 615 |
} ); |
| 616 |
$mock->method( 'isWatched' )->willReturnCallback( function ( $user, $target ) { |
| 617 |
$dataKey = $this->getWatchedItemStoreKey( $user, $target ); |
| 618 |
return isset( $this->watchedItemStoreData[ $dataKey ] ); |
| 619 |
} ); |
| 620 |
$mock->method( 'isTempWatched' )->willReturnCallback( function ( $user, $target ) { |
| 621 |
$dataKey = $this->getWatchedItemStoreKey( $user, $target ); |
| 622 |
return isset( $this->watchedItemStoreData[ $dataKey ] ) && |
| 623 |
$this->watchedItemStoreData[ $dataKey ] !== true; |
| 624 |
} ); |
| 625 |
return $mock; |
| 626 |
} |
| 627 |
|
| 628 |
private function getDummyCommentStore(): CommentStore { |
| 629 |
$mockLang = $this->createNoOpMock( Language::class, |
| 630 |
[ 'truncateForVisual', 'truncateForDatabase' ] ); |
| 631 |
$mockLang->method( $this->logicalOr( 'truncateForDatabase', 'truncateForVisual' ) ) |
| 632 |
->willReturnCallback( |
| 633 |
static function ( string $text, int $limit ): string { |
| 634 |
if ( strlen( $text ) > $limit - 3 ) { |
| 635 |
return substr( $text, 0, $limit - 3 ) . '...'; |
| 636 |
} |
| 637 |
return $text; |
| 638 |
} |
| 639 |
); |
| 640 |
return new CommentStore( $mockLang ); |
| 641 |
} |
| 642 |
} |