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( '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 );
74 // TODO getPageLanguage should return a Language object, 'qqx' is a string
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'] );
87 $title->method( 'getLatestRevId' )->willReturn( $id === 0 ?
0 : 43 );
89 $title->method( 'isContentPage' )->willReturn( true );
90 $title->method( 'isSamePageAs' )->willReturnCallback( static function ( $other ) use ( $id ) {
91 return $other && $id === $other->getArticleId();
93 $title->method( 'isSameLinkAs' )->willReturnCallback( static function ( $other ) use ( $ns, $text ) {
94 return $other && $text === $other->getDBkey() && $ns === $other->getNamespace();
96 $title->method( 'equals' )->willReturnCallback( static function ( $other ) use ( $preText ) {
97 return $other->getNamespace() ?
'ns' . $other->getNamespace() . ':' : '' . $other->getDBkey() ===
98 str_replace( ' ', '_', $preText );
100 $title->method( '__toString' )->willReturn( "MockTitle:{$preText}" );
102 $title->method( 'toPageIdentity' )->willReturnCallback( static function () use ( $title ) {
103 return new PageIdentityValue(
105 $title->getNamespace(),
111 $title->method( 'toPageRecord' )->willReturnCallback( static function () use ( $title ) {
112 return new PageStoreRecord(
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,
131 private function makeMockTitleFactory(): TitleFactory
{
132 $factory = $this->createNoOpMock(
137 $factory->method( 'newFromText' )->willReturnCallback(
139 return $this->makeMockTitle( $text );