| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Page\PageIdentity; |
| 4 |
use MediaWiki\Page\PageIdentityValue; |
| 5 |
use MediaWiki\Page\PageStoreRecord; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
use MediaWiki\Title\TitleFactory; |
| 8 |
use PHPUnit\Framework\MockObject\MockObject; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
trait MockTitleTrait { |
| 15 |
|
| 16 |
|
| 17 |
private $pageIdCounter = 0; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private function makeMockTitle( $text, array $props = [] ) { |
| 35 |
$ns = $props['namespace'] ?? 0; |
| 36 |
if ( $ns < 0 ) { |
| 37 |
$id = 0; |
| 38 |
} else { |
| 39 |
$id = $props['id'] ?? ++$this->pageIdCounter; |
| 40 |
} |
| 41 |
$nsName = $ns ? "ns$ns:" : ''; |
| 42 |
|
| 43 |
$preText = $text; |
| 44 |
$text = preg_replace( '/^[\w ]*?:/', '', $text ); |
| 45 |
|
| 46 |
|
| 47 |
if ( $preText == $text && $ns ) { |
| 48 |
$preText = $nsName . $text; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
$title = $this->createMock( Title::class ); |
| 53 |
|
| 54 |
$title->method( 'getText' )->willReturn( str_replace( '_', ' ', $text ) ); |
| 55 |
$title->method( 'getDBkey' )->willReturn( str_replace( ' ', '_', $text ) ); |
| 56 |
|
| 57 |
$title->method( 'getPrefixedText' )->willReturn( str_replace( '_', ' ', $preText ) ); |
| 58 |
$title->method( 'getPrefixedDBkey' )->willReturn( str_replace( ' ', '_', $preText ) ); |
| 59 |
|
| 60 |
$title->method( 'getArticleID' )->willReturn( $id ); |
| 61 |
$title->method( 'getId' )->willReturn( $id ); |
| 62 |
$title->method( 'getNamespace' )->willReturn( $ns ); |
| 63 |
$title->method( 'inNamespace' )->willReturnCallback( static function ( $namespace ) use ( $ns ) { |
| 64 |
return $namespace === $ns; |
| 65 |
} ); |
| 66 |
$title->method( 'getFragment' )->willReturn( $props['fragment'] ?? '' ); |
| 67 |
$title->method( 'hasFragment' )->willReturn( !empty( $props['fragment'] ) ); |
| 68 |
$title->method( 'getInterwiki' )->willReturn( $props['interwiki'] ?? '' ); |
| 69 |
$title->method( 'exists' )->willReturn( $id > 0 ); |
| 70 |
$title->method( 'isRedirect' )->willReturn( $props['redirect'] ?? false ); |
| 71 |
$title->method( 'isValidRedirectTarget' )->willReturn( $props['validRedirect'] ?? true ); |
| 72 |
$title->method( 'getTouched' )->willReturn( $id ? '20200101223344' : false ); |
| 73 |
|
| 74 |
|
| 75 |
$title->method( 'getPageLanguage' )->willReturn( $props['language'] ?? 'qqx' ); |
| 76 |
$contentModel = $props['contentModel'] ?? CONTENT_MODEL_WIKITEXT; |
| 77 |
$title->method( 'getContentModel' )->willReturn( $contentModel ); |
| 78 |
$title->method( 'hasContentModel' )->willReturnCallback( |
| 79 |
static fn ( $id ) => $id === $contentModel ); |
| 80 |
$title->method( 'getTitleProtection' )->willReturn( false ); |
| 81 |
$title->method( 'canExist' ) |
| 82 |
->willReturn( $ns >= 0 && empty( $props['interwiki'] ) && $text !== '' ); |
| 83 |
$title->method( 'getWikiId' )->willReturn( Title::LOCAL ); |
| 84 |
if ( isset( $props['revision'] ) ) { |
| 85 |
$title->method( 'getLatestRevId' )->willReturn( $props['revision'] ); |
| 86 |
} else { |
| 87 |
$title->method( 'getLatestRevId' )->willReturn( $id === 0 ? 0 : 43 ); |
| 88 |
} |
| 89 |
$title->method( 'isContentPage' )->willReturn( true ); |
| 90 |
$title->method( 'isSamePageAs' )->willReturnCallback( static function ( $other ) use ( $id ) { |
| 91 |
return $other && $id === $other->getArticleId(); |
| 92 |
} ); |
| 93 |
$title->method( 'isSameLinkAs' )->willReturnCallback( static function ( $other ) use ( $ns, $text ) { |
| 94 |
return $other && $text === $other->getDBkey() && $ns === $other->getNamespace(); |
| 95 |
} ); |
| 96 |
$title->method( 'equals' )->willReturnCallback( static function ( $other ) use ( $preText ) { |
| 97 |
return $other->getNamespace() ? 'ns' . $other->getNamespace() . ':' : '' . $other->getDBkey() === |
| 98 |
str_replace( ' ', '_', $preText ); |
| 99 |
} ); |
| 100 |
$title->method( '__toString' )->willReturn( "MockTitle:{$preText}" ); |
| 101 |
|
| 102 |
$title->method( 'toPageIdentity' )->willReturnCallback( static function () use ( $title ) { |
| 103 |
return new PageIdentityValue( |
| 104 |
$title->getId(), |
| 105 |
$title->getNamespace(), |
| 106 |
$title->getDBkey(), |
| 107 |
PageIdentity::LOCAL |
| 108 |
); |
| 109 |
} ); |
| 110 |
|
| 111 |
$title->method( 'toPageRecord' )->willReturnCallback( static function () use ( $title ) { |
| 112 |
return new PageStoreRecord( |
| 113 |
(object)[ |
| 114 |
'page_id' => $title->getArticleID(), |
| 115 |
'page_namespace' => $title->getNamespace(), |
| 116 |
'page_title' => $title->getDBkey(), |
| 117 |
'page_wiki_id' => $title->getWikiId(), |
| 118 |
'page_latest' => $title->getLatestRevID(), |
| 119 |
'page_is_new' => $title->isNewPage(), |
| 120 |
'page_is_redirect' => $title->isRedirect(), |
| 121 |
'page_touched' => $title->getTouched(), |
| 122 |
'page_lang' => $title->getPageLanguage() ?: null, |
| 123 |
], |
| 124 |
PageIdentity::LOCAL |
| 125 |
); |
| 126 |
} ); |
| 127 |
|
| 128 |
return $title; |
| 129 |
} |
| 130 |
|
| 131 |
private function makeMockTitleFactory(): TitleFactory { |
| 132 |
$factory = $this->createNoOpMock( |
| 133 |
TitleFactory::class, |
| 134 |
[ 'newFromText' ] |
| 135 |
); |
| 136 |
|
| 137 |
$factory->method( 'newFromText' )->willReturnCallback( |
| 138 |
function ( $text ) { |
| 139 |
return $this->makeMockTitle( $text ); |
| 140 |
} |
| 141 |
); |
| 142 |
|
| 143 |
return $factory; |
| 144 |
} |
| 145 |
} |