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
;
11 * Creates semi-sane Title mocks for tests.
12 * @stable to use since 1.41
14 trait MockTitleTrait
{
17 private $pageIdCounter = 0;
21 * @param array $props Additional properties to set. Supported keys:
27 * - language: Language
28 * - contentModel: string
30 * - validRedirect: bool
32 * @return Title|MockObject
34 private function makeMockTitle( $text, array $props = [] ) {
35 $ns = $props['namespace'] ??
0;
39 $id = $props['id'] ?? ++
$this->pageIdCounter
;
41 $nsName = $ns ?
"ns$ns:" : '';
44 $text = preg_replace( '/^[\w ]*?:/', '', $text );
46 // If no namespace prefix was given, add one if needed.
47 if ( $preText == $text && $ns ) {
48 $preText = $nsName . $text;
51 /** @var Title|MockObject $title */
52 $title = $this->createMock( Title
::class );
54 $title->method( 'getText' )->willReturn( str_replace( '_', ' ', $text ) );
55 $title->method( 'getDBkey' )->willReturn( str_replace( ' ', '_', $text ) );
57 $title->method( 'getPrefixedText' )->willReturn( str_replace( '_', ' ', $preText ) );
58 $title->method( 'getPrefixedDBkey' )->willReturn( str_replace( ' ', '_', $preText ) );
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;
66 $title->method( 'isSpecialPage' )->willReturn( $ns === NS_SPECIAL
);
67 $title->method( 'getFragment' )->willReturn( $props['fragment'] ??
'' );
68 $title->method( 'hasFragment' )->willReturn( !empty( $props['fragment'] ) );
69 $title->method( 'getInterwiki' )->willReturn( $props['interwiki'] ??
'' );
70 $title->method( 'exists' )->willReturn( $id > 0 );
71 $title->method( 'isRedirect' )->willReturn( $props['redirect'] ??
false );
72 $title->method( 'isValidRedirectTarget' )->willReturn( $props['validRedirect'] ??
true );
73 $title->method( 'getTouched' )->willReturn( $id ?
'20200101223344' : false );
75 // TODO getPageLanguage should return a Language object, 'qqx' is a string
76 $title->method( 'getPageLanguage' )->willReturn( $props['language'] ??
'qqx' );
77 $contentModel = $props['contentModel'] ?? CONTENT_MODEL_WIKITEXT
;
78 $title->method( 'getContentModel' )->willReturn( $contentModel );
79 $title->method( 'hasContentModel' )->willReturnCallback(
80 static fn ( $id ) => $id === $contentModel );
81 $title->method( 'getTitleProtection' )->willReturn( false );
82 $title->method( 'canExist' )
83 ->willReturn( $ns >= 0 && empty( $props['interwiki'] ) && $text !== '' );
84 $title->method( 'getWikiId' )->willReturn( Title
::LOCAL
);
85 if ( isset( $props['revision'] ) ) {
86 $title->method( 'getLatestRevId' )->willReturn( $props['revision'] );
88 $title->method( 'getLatestRevId' )->willReturn( $id === 0 ?
0 : 43 );
90 $title->method( 'isContentPage' )->willReturn( true );
91 $title->method( 'isSamePageAs' )->willReturnCallback( static function ( $other ) use ( $id ) {
92 return $other && $id === $other->getArticleId();
94 $title->method( 'isSameLinkAs' )->willReturnCallback( static function ( $other ) use ( $ns, $text ) {
95 return $other && $text === $other->getDBkey() && $ns === $other->getNamespace();
97 $title->method( 'equals' )->willReturnCallback( static function ( $other ) use ( $preText ) {
98 return $other->getNamespace() ?
'ns' . $other->getNamespace() . ':' : '' . $other->getDBkey() ===
99 str_replace( ' ', '_', $preText );
101 $title->method( '__toString' )->willReturn( "MockTitle:{$preText}" );
103 $title->method( 'toPageIdentity' )->willReturnCallback( static function () use ( $title ) {
104 return new PageIdentityValue(
106 $title->getNamespace(),
112 $title->method( 'toPageRecord' )->willReturnCallback( static function () use ( $title ) {
113 return new PageStoreRecord(
115 'page_id' => $title->getArticleID(),
116 'page_namespace' => $title->getNamespace(),
117 'page_title' => $title->getDBkey(),
118 'page_wiki_id' => $title->getWikiId(),
119 'page_latest' => $title->getLatestRevID(),
120 'page_is_new' => $title->isNewPage(),
121 'page_is_redirect' => $title->isRedirect(),
122 'page_touched' => $title->getTouched(),
123 'page_lang' => $title->getPageLanguage() ?
: null,
132 private function makeMockTitleFactory(): TitleFactory
{
133 $factory = $this->createNoOpMock(
138 $factory->method( 'newFromText' )->willReturnCallback(
140 return $this->makeMockTitle( $text );