Merge "rdbms: Replace func_get_args() in SQLPlatform::buildLike()"
[mediawiki.git] / tests / phpunit / mocks / MockTitleTrait.php
blob2fbd8ad86827ce56c1547e48eb0a759c700587f6
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( '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'] );
87 } else {
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();
93 } );
94 $title->method( 'isSameLinkAs' )->willReturnCallback( static function ( $other ) use ( $ns, $text ) {
95 return $other && $text === $other->getDBkey() && $ns === $other->getNamespace();
96 } );
97 $title->method( 'equals' )->willReturnCallback( static function ( $other ) use ( $preText ) {
98 return $other->getNamespace() ? 'ns' . $other->getNamespace() . ':' : '' . $other->getDBkey() ===
99 str_replace( ' ', '_', $preText );
100 } );
101 $title->method( '__toString' )->willReturn( "MockTitle:{$preText}" );
103 $title->method( 'toPageIdentity' )->willReturnCallback( static function () use ( $title ) {
104 return new PageIdentityValue(
105 $title->getId(),
106 $title->getNamespace(),
107 $title->getDBkey(),
108 PageIdentity::LOCAL
110 } );
112 $title->method( 'toPageRecord' )->willReturnCallback( static function () use ( $title ) {
113 return new PageStoreRecord(
114 (object)[
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,
125 PageIdentity::LOCAL
127 } );
129 return $title;
132 private function makeMockTitleFactory(): TitleFactory {
133 $factory = $this->createNoOpMock(
134 TitleFactory::class,
135 [ 'newFromText' ]
138 $factory->method( 'newFromText' )->willReturnCallback(
139 function ( $text ) {
140 return $this->makeMockTitle( $text );
144 return $factory;