Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / mocks / MockTitleTrait.php
blob9898cbfdcc6da9a93a156b8945d1b797171d3c6b
1 <?php
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;
10 /**
11 * Creates semi-sane Title mocks for tests.
12 * @stable to use since 1.41
14 trait MockTitleTrait {
16 /** @var int */
17 private $pageIdCounter = 0;
19 /**
20 * @param string $text
21 * @param array $props Additional properties to set. Supported keys:
22 * - id: int
23 * - namespace: int
24 * - fragment: string
25 * - interwiki: string
26 * - redirect: bool
27 * - language: Language
28 * - contentModel: string
29 * - revision: int
30 * - validRedirect: bool
32 * @return Title|MockObject
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;
41 $nsName = $ns ? "ns$ns:" : '';
43 $preText = $text;
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;
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 );
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'] );
86 } else {
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();
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}" );
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
109 } );
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,
124 PageIdentity::LOCAL
126 } );
128 return $title;
131 private function makeMockTitleFactory(): TitleFactory {
132 $factory = $this->createNoOpMock(
133 TitleFactory::class,
134 [ 'newFromText' ]
137 $factory->method( 'newFromText' )->willReturnCallback(
138 function ( $text ) {
139 return $this->makeMockTitle( $text );
143 return $factory;