1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
use MediaWiki\Interwiki\InterwikiLookup;
use PHPUnit\Framework\MockObject\MockObject;
trait MockTitleTrait {
/** @var int */
private $pageIdCounter = 0;
/**
* @param string $text
* @param array $props Additional properties to set. Supported keys:
* - id: int
* - namespace: int
* - language: Language
* - contentModel: string
*
* @return Title|MockObject
*/
private function makeMockTitle( $text, array $props = [] ) {
$id = $props['id'] ?? ++$this->pageIdCounter;
$ns = $props['namespace'] ?? 0;
$nsName = $ns ? "ns$ns:" : '';
$preText = $text;
$text = preg_replace( '/^[\w ]*?:/', '', $text );
// If no namespace prefix was given, add one if needed.
if ( $preText == $text && $ns ) {
$preText = $nsName . $text;
}
/** @var Title|MockObject $title */
$title = $this->createMock( Title::class );
$title->method( 'getText' )->willReturn( str_replace( '_', ' ', $text ) );
$title->method( 'getDBkey' )->willReturn( str_replace( ' ', '_', $text ) );
$title->method( 'getPrefixedText' )->willReturn( str_replace( '_', ' ', $preText ) );
$title->method( 'getPrefixedDBkey' )->willReturn( str_replace( ' ', '_', $preText ) );
$title->method( 'getArticleID' )->willReturn( $id );
$title->method( 'getId' )->willReturn( $id );
$title->method( 'getNamespace' )->willReturn( $ns );
$title->method( 'exists' )->willReturn( $id > 0 );
$title->method( 'getTouched' )->willReturn( $id ? '20200101223344' : false );
$title->method( 'getPageLanguage' )->willReturn( $props['language'] ?? 'qqx' );
$title->method( 'getContentModel' )
->willReturn( $props['contentModel'] ?? CONTENT_MODEL_WIKITEXT );
$title->method( 'getRestrictions' )->willReturn( [] );
$title->method( 'getTitleProtection' )->willReturn( false );
$title->method( 'canExist' )->willReturn( true );
$title->method( 'getWikiId' )->willReturn( Title::LOCAL );
if ( isset( $props['revision'] ) ) {
$title->method( 'getLatestRevId' )->willReturn( $props['revision'] );
} else {
$title->method( 'getLatestRevId' )->willReturn( $id === 0 ? 0 : 43 );
}
$title->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$title->method( 'isContentPage' )->willReturn( true );
$title->method( 'isSamePageAs' )->willReturnCallback( static function ( $other ) use ( $id ) {
return $other && $id === $other->getArticleId();
} );
$title->method( 'isSameLinkAs' )->willReturnCallback( static function ( $other ) use ( $ns, $text ) {
return $other && $text === $other->getDBkey() && $ns === $other->getNamespace();
} );
$title->method( 'equals' )->willReturnCallback( static function ( $other ) use ( $preText ) {
return $other->getPrefixedDBkey() === str_replace( ' ', '_', $preText );
} );
$title->method( '__toString' )->willReturn( "MockTitle:{$preText}" );
return $title;
}
/**
* @return MediaWikiTitleCodec
*/
private function makeMockTitleCodec() {
/** @var Language|MockObject $language */
$language = $this->createNoOpMock( Language::class, [ 'ucfirst' ] );
$language->method( 'ucfirst' )->willReturnCallback( 'ucfirst' );
/** @var GenderCache|MockObject $genderCache */
$genderCache = $this->createNoOpMock( GenderCache::class );
/** @var InterwikiLookup|MockObject $interwikiLookup */
$interwikiLookup = $this->createNoOpMock( InterwikiLookup::class );
/** @var NamespaceInfo|MockObject $namespaceInfo */
$namespaceInfo = $this->createNoOpMock( NamespaceInfo::class, [ 'isCapitalized' ] );
$namespaceInfo->method( 'isCapitalized' )->willReturn( true );
$titleCodec = new MediaWikiTitleCodec(
$language,
$genderCache,
[ 'en' ],
$interwikiLookup,
$namespaceInfo
);
return $titleCodec;
}
/**
* Expected to be provided by the class, probably inherited from TestCase.
*
* @param string $originalClassName
*
* @return MockObject
*/
abstract protected function createMock( $originalClassName ): MockObject;
/**
* Expected to be provided by the class, probably MediaWikiTestCaseTrait.
*
* @param string $type
* @param string[] $allow methods to allow
*
* @return MockObject
*/
abstract protected function createNoOpMock( $type, $allow = [] );
}
|