MediaWiki REL1_33
RecentChangeTest.php
Go to the documentation of this file.
1<?php
2use Wikimedia\ScopedCallback;
3
8 protected $title;
9 protected $target;
10 protected $user;
11 protected $user_comment;
12 protected $context;
13
14 public function setUp() {
15 parent::setUp();
16
17 $this->title = Title::newFromText( 'SomeTitle' );
18 $this->target = Title::newFromText( 'TestTarget' );
19 $this->user = User::newFromName( 'UserName' );
20
21 $this->user_comment = '<User comment about action>';
22 $this->context = RequestContext::newExtraneousContext( $this->title );
23 }
24
29 public function testNewFromRow() {
30 $user = $this->getTestUser()->getUser();
31 $actorId = $user->getActorId();
32
33 $row = new stdClass();
34 $row->rc_foo = 'AAA';
35 $row->rc_timestamp = '20150921134808';
36 $row->rc_deleted = 'bar';
37 $row->rc_comment_text = 'comment';
38 $row->rc_comment_data = null;
39 $row->rc_user = $user->getId();
40
41 $rc = RecentChange::newFromRow( $row );
42
43 $expected = [
44 'rc_foo' => 'AAA',
45 'rc_timestamp' => '20150921134808',
46 'rc_deleted' => 'bar',
47 'rc_comment' => 'comment',
48 'rc_comment_text' => 'comment',
49 'rc_comment_data' => null,
50 'rc_user' => $user->getId(),
51 'rc_user_text' => $user->getName(),
52 'rc_actor' => $actorId,
53 ];
54 $this->assertEquals( $expected, $rc->getAttributes() );
55
56 $row = new stdClass();
57 $row->rc_foo = 'AAA';
58 $row->rc_timestamp = '20150921134808';
59 $row->rc_deleted = 'bar';
60 $row->rc_comment = 'comment';
61 $row->rc_user = $user->getId();
62
63 Wikimedia\suppressWarnings();
64 $rc = RecentChange::newFromRow( $row );
65 Wikimedia\restoreWarnings();
66
67 $expected = [
68 'rc_foo' => 'AAA',
69 'rc_timestamp' => '20150921134808',
70 'rc_deleted' => 'bar',
71 'rc_comment' => 'comment',
72 'rc_comment_text' => 'comment',
73 'rc_comment_data' => null,
74 'rc_user' => $user->getId(),
75 'rc_user_text' => $user->getName(),
76 'rc_actor' => $actorId,
77 ];
78 $this->assertEquals( $expected, $rc->getAttributes() );
79 }
80
84 public function testParseParams() {
85 $params = [
86 'root' => [
87 'A' => 1,
88 'B' => 'two'
89 ]
90 ];
91
92 $this->assertParseParams(
93 $params,
94 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
95 );
96
97 $this->assertParseParams(
98 null,
99 null
100 );
101
102 $this->assertParseParams(
103 null,
104 serialize( false )
105 );
106
107 $this->assertParseParams(
108 null,
109 'not-an-array'
110 );
111 }
112
117 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
118 $rc = new RecentChange;
119 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
120
121 $actualParseParams = $rc->parseParams();
122
123 $this->assertEquals( $expectedParseParams, $actualParseParams );
124 }
125
129 public function provideIsInRCLifespan() {
130 return [
131 [ 6000, -3000, 0, true ],
132 [ 3000, -6000, 0, false ],
133 [ 6000, -3000, 6000, true ],
134 [ 3000, -6000, 6000, true ],
135 ];
136 }
137
142 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
143 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
144 // Calculate this here instead of the data provider because the provider
145 // is expanded early on and the full test suite may take longer than 100 minutes
146 // when coverage is enabled.
147 $timestamp = time() + $offset;
148 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
149 }
150
151 public function provideRCTypes() {
152 return [
153 [ RC_EDIT, 'edit' ],
154 [ RC_NEW, 'new' ],
155 [ RC_LOG, 'log' ],
156 [ RC_EXTERNAL, 'external' ],
157 [ RC_CATEGORIZE, 'categorize' ],
158 ];
159 }
160
165 public function testParseFromRCType( $rcType, $type ) {
166 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
167 }
168
173 public function testParseToRCType( $rcType, $type ) {
174 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
175 }
176
180 private function getMockPageProps() {
181 return $this->getMockBuilder( PageProps::class )
182 ->disableOriginalConstructor()
183 ->getMock();
184 }
185
186 public function provideCategoryContent() {
187 return [
188 [ true ],
189 [ false ],
190 ];
191 }
192
197 public function testHiddenCategoryChange( $isHidden ) {
198 $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
199
200 $pageProps = $this->getMockPageProps();
201 $pageProps->expects( $this->once() )
202 ->method( 'getProperties' )
203 ->with( $categoryTitle, 'hiddencat' )
204 ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
205
206 $scopedOverride = PageProps::overrideInstance( $pageProps );
207
208 $rc = RecentChange::newForCategorization(
209 '0',
210 $categoryTitle,
211 $this->user,
212 $this->user_comment,
213 $this->title,
214 $categoryTitle->getLatestRevID(),
215 $categoryTitle->getLatestRevID(),
216 '0',
217 false
218 );
219
220 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
221
222 ScopedCallback::consume( $scopedOverride );
223 }
224}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
static overrideInstance(PageProps $store=null)
Overrides the default instance of this class This is intended for use while testing and will fail if ...
Definition PageProps.php:50
testParseFromRCType( $rcType, $type)
provideRCTypes RecentChange::parseFromRCType
testNewFromRow()
RecentChange::newFromRow RecentChange::loadFromRow.
testParseParams()
RecentChange::parseParams.
testHiddenCategoryChange( $isHidden)
provideCategoryContent RecentChange::newForCategorization
testParseToRCType( $rcType, $type)
provideRCTypes RecentChange::parseToRCType
testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected)
RecentChange::isInRCLifespan provideIsInRCLifespan.
assertParseParams( $expectedParseParams, $rawRcParams)
Utility class for creating new RC entries.
setAttribs( $attribs)
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
const RC_NEW
Definition Defines.php:152
const RC_LOG
Definition Defines.php:153
const RC_EXTERNAL
Definition Defines.php:154
const RC_EDIT
Definition Defines.php:151
const NS_CATEGORY
Definition Defines.php:87
const RC_CATEGORIZE
Definition Defines.php:155
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2004
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
title
$params