3 use MediaWiki\Cache\GenderCache
;
4 use MediaWiki\Cache\LinkCache
;
5 use MediaWiki\Config\ServiceOptions
;
6 use MediaWiki\Context\RequestContext
;
7 use MediaWiki\Linker\LinkRenderer
;
8 use MediaWiki\Linker\LinkTarget
;
9 use MediaWiki\Page\ExistingPageRecord
;
10 use MediaWiki\Page\PageStore
;
11 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
12 use MediaWiki\User\UserFactory
;
13 use Wikimedia\IPUtils
;
14 use Wikimedia\Rdbms\ILoadBalancer
;
15 use Wikimedia\Stats\StatsFactory
;
16 use Wikimedia\TestingAccessWrapper
;
21 abstract class LogFormatterTestCase
extends MediaWikiLangTestCase
{
22 use MockAuthorityTrait
;
24 public function doTestLogFormatter( $row, $extra, $userGroups = [] ) {
25 RequestContext
::resetMain();
26 $row = $this->expandDatabaseRow( $row, $this->isLegacy( $extra ) );
28 $services = $this->getServiceContainer();
29 $userGroups = (array)$userGroups;
30 $userRights = $services->getGroupPermissionsLookup()->getGroupPermissions( $userGroups );
31 $context = new RequestContext();
32 $authority = $this->mockRegisteredAuthorityWithPermissions( $userRights );
33 $context->setAuthority( $authority );
34 $context->setLanguage( 'en' );
36 $formatter = $services->getLogFormatterFactory()->newFromRow( $row );
37 $formatter->setContext( $context );
39 // Create a LinkRenderer without LinkCache to avoid DB access
40 $realLinkRenderer = new LinkRenderer(
41 $services->getTitleFormatter(),
42 $this->createMock( LinkCache
::class ),
43 $services->getSpecialPageFactory(),
44 $services->getHookContainer(),
46 LinkRenderer
::CONSTRUCTOR_OPTIONS
,
47 $services->getMainConfig(),
48 [ 'renderForComment' => false ]
51 // Then create a mock LinkRenderer that proxies makeLink calls to the original LinkRenderer, but assumes
52 // that all links are known to bypass DB access in Title::exists().
53 $linkRenderer = $this->createMock( LinkRenderer
::class );
54 $linkRenderer->method( 'makeLink' )
56 static function ( $target, $text = null, $extra = [], $query = [] ) use ( $realLinkRenderer ) {
57 return $realLinkRenderer->makeKnownLink( $target, $text, $extra, $query );
60 $formatter->setLinkRenderer( $linkRenderer );
61 $this->setService( 'LinkRenderer', $linkRenderer );
63 // Create a mock PageStore where all pages are existing, in case any calls to Title::exists are not
64 // caught by the mocks above.
65 $pageStore = $this->getMockBuilder( PageStore
::class )
66 ->onlyMethods( [ 'getPageByName' ] )
67 ->setConstructorArgs( [
68 new ServiceOptions( PageStore
::CONSTRUCTOR_OPTIONS
, $services->getMainConfig() ),
69 $this->createNoOpMock( ILoadBalancer
::class ),
70 $services->getNamespaceInfo(),
71 $services->getTitleParser(),
73 StatsFactory
::newNull()
76 $pageStore->method( 'getPageByName' )
77 ->willReturn( $this->createMock( ExistingPageRecord
::class ) );
78 $this->setService( 'PageStore', $pageStore );
80 // Create a mock UserFactory where all registered users are created with ID and name and where loading of
81 // other fields is prevented, to avoid DB access.
82 $origUserFactory = $services->getUserFactory();
83 $userFactory = $this->createMock( UserFactory
::class );
84 $userFactory->method( 'newFromName' )
85 ->willReturnCallback( static function ( $name, $validation ) use ( $origUserFactory ) {
86 $ret = $origUserFactory->newFromName( $name, $validation );
90 $userID = IPUtils
::isIPAddress( $name ) ?
0 : 42;
91 $ret = TestingAccessWrapper
::newFromObject( $ret );
93 $ret->mLoadedItems
= true;
96 $userFactory->method( 'newFromId' )->willReturnCallback( [ $origUserFactory, 'newFromId' ] );
97 $userFactory->method( 'newAnonymous' )->willReturnCallback( [ $origUserFactory, 'newAnonymous' ] );
98 $userFactory->method( 'newFromUserIdentity' )
99 ->willReturnCallback( [ $origUserFactory, 'newFromUserIdentity' ] );
100 $this->setService( 'UserFactory', $userFactory );
102 // Replace gender cache to avoid gender DB lookups
103 $genderCache = $this->createMock( GenderCache
::class );
104 $genderCache->method( 'getGenderOf' )->willReturn( 'unknown' );
105 $this->setService( 'GenderCache', $genderCache );
109 self
::removeSomeHtml( $formatter->getActionText() ),
110 'Action text is equal to expected text'
113 $this->assertSame( // ensure types and array key order
115 self
::removeApiMetaData( $formatter->formatParametersForApi() ),
116 'Api log params is equal to expected array'
119 if ( isset( $extra['preload'] ) ) {
120 $this->assertArrayEquals(
121 $this->getLinkTargetsAsStrings( $extra['preload'] ),
122 $this->getLinkTargetsAsStrings(
123 $formatter->getPreloadTitles()
129 private function getLinkTargetsAsStrings( array $linkTargets ) {
130 return array_map( static function ( LinkTarget
$t ) {
131 return $t->getInterwiki() . ':' . $t->getNamespace() . ':'
132 . $t->getDBkey() . '#' . $t->getFragment();
136 protected function isLegacy( $extra ) {
137 return isset( $extra['legacy'] ) && $extra['legacy'];
140 protected function expandDatabaseRow( $data, $legacy ) {
142 // no log_id because no insert in database
143 'log_type' => $data['type'],
144 'log_action' => $data['action'],
145 'log_timestamp' => $data['timestamp'] ??
wfTimestampNow(),
146 'log_user' => $data['user'] ??
42,
147 'log_user_text' => $data['user_text'] ??
'User',
148 'log_actor' => $data['actor'] ??
24,
149 'log_namespace' => $data['namespace'] ?? NS_MAIN
,
150 'log_title' => $data['title'] ??
'Main_Page',
151 'log_page' => $data['page'] ??
0,
152 'log_comment_text' => $data['comment'] ??
'',
153 'log_comment_data' => null,
154 'log_params' => $legacy
155 ? LogPage
::makeParamBlob( $data['params'] )
156 : LogEntryBase
::makeParamBlob( $data['params'] ),
157 'log_deleted' => $data['deleted'] ??
0,
161 protected static function removeSomeHtml( $html ) {
162 $html = str_replace( '"', '"', $html );
163 $html = preg_replace( '/\xE2\x80[\x8E\x8F]/', '', $html ); // Strip lrm/rlm
164 return trim( strip_tags( $html ) );
167 protected static function removeApiMetaData( $val ) {
168 if ( is_array( $val ) ) {
169 unset( $val['_element'] );
170 unset( $val['_type'] );
171 foreach ( $val as $key => $value ) {
172 $val[$key] = self
::removeApiMetaData( $value );