MediaWiki  master
RecentChangeTest.php
Go to the documentation of this file.
1 <?php
2 
4 
9  protected $title;
10  protected $target;
11  protected $user;
12  protected $user_comment;
13  protected $context;
14 
15  public function setUp() {
16  parent::setUp();
17 
18  $this->title = Title::newFromText( 'SomeTitle' );
19  $this->target = Title::newFromText( 'TestTarget' );
20  $this->user = User::newFromName( 'UserName' );
21 
22  $this->user_comment = '<User comment about action>';
23  $this->context = RequestContext::newExtraneousContext( $this->title );
24  }
25 
30  public function testNewFromRow() {
31  $user = $this->getTestUser()->getUser();
32  $actorId = $user->getActorId();
33 
34  $row = new stdClass();
35  $row->rc_foo = 'AAA';
36  $row->rc_timestamp = '20150921134808';
37  $row->rc_deleted = 'bar';
38  $row->rc_comment_text = 'comment';
39  $row->rc_comment_data = null;
40  $row->rc_user = $user->getId();
41 
42  $rc = RecentChange::newFromRow( $row );
43 
44  $expected = [
45  'rc_foo' => 'AAA',
46  'rc_timestamp' => '20150921134808',
47  'rc_deleted' => 'bar',
48  'rc_comment' => 'comment',
49  'rc_comment_text' => 'comment',
50  'rc_comment_data' => null,
51  'rc_user' => $user->getId(),
52  'rc_user_text' => $user->getName(),
53  'rc_actor' => $actorId,
54  ];
55  $this->assertEquals( $expected, $rc->getAttributes() );
56 
57  $row = new stdClass();
58  $row->rc_foo = 'AAA';
59  $row->rc_timestamp = '20150921134808';
60  $row->rc_deleted = 'bar';
61  $row->rc_comment = 'comment';
62  $row->rc_user = $user->getId();
63 
64  Wikimedia\suppressWarnings();
65  $rc = RecentChange::newFromRow( $row );
66  Wikimedia\restoreWarnings();
67 
68  $expected = [
69  'rc_foo' => 'AAA',
70  'rc_timestamp' => '20150921134808',
71  'rc_deleted' => 'bar',
72  'rc_comment' => 'comment',
73  'rc_comment_text' => 'comment',
74  'rc_comment_data' => null,
75  'rc_user' => $user->getId(),
76  'rc_user_text' => $user->getName(),
77  'rc_actor' => $actorId,
78  ];
79  $this->assertEquals( $expected, $rc->getAttributes() );
80  }
81 
85  public function testParseParams() {
86  $params = [
87  'root' => [
88  'A' => 1,
89  'B' => 'two'
90  ]
91  ];
92 
93  $this->assertParseParams(
94  $params,
95  'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
96  );
97 
98  $this->assertParseParams(
99  null,
100  null
101  );
102 
103  $this->assertParseParams(
104  null,
105  serialize( false )
106  );
107 
108  $this->assertParseParams(
109  null,
110  'not-an-array'
111  );
112  }
113 
118  protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
119  $rc = new RecentChange;
120  $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
121 
122  $actualParseParams = $rc->parseParams();
123 
124  $this->assertEquals( $expectedParseParams, $actualParseParams );
125  }
126 
130  public function provideIsInRCLifespan() {
131  return [
132  [ 6000, -3000, 0, true ],
133  [ 3000, -6000, 0, false ],
134  [ 6000, -3000, 6000, true ],
135  [ 3000, -6000, 6000, true ],
136  ];
137  }
138 
143  public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
144  $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
145  // Calculate this here instead of the data provider because the provider
146  // is expanded early on and the full test suite may take longer than 100 minutes
147  // when coverage is enabled.
148  $timestamp = time() + $offset;
149  $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
150  }
151 
152  public function provideRCTypes() {
153  return [
154  [ RC_EDIT, 'edit' ],
155  [ RC_NEW, 'new' ],
156  [ RC_LOG, 'log' ],
157  [ RC_EXTERNAL, 'external' ],
158  [ RC_CATEGORIZE, 'categorize' ],
159  ];
160  }
161 
166  public function testParseFromRCType( $rcType, $type ) {
167  $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
168  }
169 
174  public function testParseToRCType( $rcType, $type ) {
175  $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
176  }
177 
181  private function getMockPageProps() {
182  return $this->getMockBuilder( PageProps::class )
183  ->disableOriginalConstructor()
184  ->getMock();
185  }
186 
187  public function provideCategoryContent() {
188  return [
189  [ true ],
190  [ false ],
191  ];
192  }
193 
198  public function testHiddenCategoryChange( $isHidden ) {
199  $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
200 
201  $pageProps = $this->getMockPageProps();
202  $pageProps->expects( $this->once() )
203  ->method( 'getProperties' )
204  ->with( $categoryTitle, 'hiddencat' )
205  ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
206 
207  $scopedOverride = PageProps::overrideInstance( $pageProps );
208 
210  '0',
211  $categoryTitle,
212  $this->user,
213  $this->user_comment,
214  $this->title,
215  $categoryTitle->getLatestRevID(),
216  $categoryTitle->getLatestRevID(),
217  '0',
218  false
219  );
220 
221  $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
222 
223  ScopedCallback::consume( $scopedOverride );
224  }
225 }
testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected)
RecentChange::isInRCLifespan provideIsInRCLifespan.
const RC_CATEGORIZE
Definition: Defines.php:142
assertParseParams( $expectedParseParams, $rawRcParams)
testParseToRCType( $rcType, $type)
provideRCTypes RecentChange::parseToRCType
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static array static newFromRow( $row)
title
static parseFromRCType( $rcType)
Parsing RC_* constants to human-readable test.
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:1982
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.
Definition: distributors.txt:9
$params
const NS_CATEGORY
Definition: Defines.php:74
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
static newForCategorization( $timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $oldRevId, $newRevId, $lastTimestamp, $bot, $ip='', $deleted=0, $added=null)
Constructs a RecentChange object for the given categorization This does not call save() on the object...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
testParseParams()
RecentChange::parseParams.
static isInRCLifespan( $timestamp, $tolerance=0)
Check whether the given timestamp is new enough to have a RC row with a given tolerance as the recent...
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:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
testNewFromRow()
RecentChange::newFromRow RecentChange::loadFromRow.
static parseToRCType( $type)
Parsing text to RC_* constants.
const RC_EXTERNAL
Definition: Defines.php:141
const RC_NEW
Definition: Defines.php:139
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:594
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
testHiddenCategoryChange( $isHidden)
provideCategoryContent RecentChange::newForCategorization
testParseFromRCType( $rcType, $type)
provideRCTypes RecentChange::parseFromRCType
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
const RC_EDIT
Definition: Defines.php:138
const RC_LOG
Definition: Defines.php:140
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319