MediaWiki REL1_31
RCCacheEntryFactoryTest.php
Go to the documentation of this file.
1<?php
2
5
14
19
24
25 public function __construct( $name = null, array $data = [], $dataName = '' ) {
26 parent::__construct( $name, $data, $dataName );
27
28 $this->testRecentChangesHelper = new TestRecentChangesHelper();
29 }
30
31 protected function setUp() {
32 parent::setUp();
33
34 $this->setMwGlobals( [
35 'wgArticlePath' => '/wiki/$1'
36 ] );
37
38 $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
39 }
40
41 public function testNewFromRecentChange() {
42 $user = $this->getMutableTestUser()->getUser();
43 $recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
44 $user,
45 'Xyz',
46 5, // curid
47 191, // thisid
48 190, // lastid
49 '20131103212153',
50 0, // counter
51 0 // number of watching users
52 );
53 $cacheEntryFactory = new RCCacheEntryFactory(
54 $this->getContext(),
55 $this->getMessages(),
56 $this->linkRenderer
57 );
58 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
59
60 $this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
61
62 $this->assertEquals( false, $cacheEntry->watched, 'watched' );
63 $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
64 $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
65 $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
66
67 $this->assertUserLinks( $user->getName(), $cacheEntry );
68 $this->assertTitleLink( 'Xyz', $cacheEntry );
69
70 $diff = [ 'curid' => 5, 'diff' => 191, 'oldid' => 190 ];
71 $cur = [ 'curid' => 5, 'diff' => 0, 'oldid' => 191 ];
72 $this->assertQueryLink( 'cur', $cur, $cacheEntry->curlink );
73 $this->assertQueryLink( 'prev', $diff, $cacheEntry->lastlink );
74 $this->assertQueryLink( 'diff', $diff, $cacheEntry->difflink );
75 }
76
77 public function testNewForDeleteChange() {
78 $user = $this->getMutableTestUser()->getUser();
79 $recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
80 'delete',
81 'delete',
82 $user,
83 'Abc',
84 '20131103212153',
85 0, // counter
86 0 // number of watching users
87 );
88 $cacheEntryFactory = new RCCacheEntryFactory(
89 $this->getContext(),
90 $this->getMessages(),
91 $this->linkRenderer
92 );
93 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
94
95 $this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
96
97 $this->assertEquals( false, $cacheEntry->watched, 'watched' );
98 $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
99 $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
100 $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
101
102 $this->assertDeleteLogLink( $cacheEntry );
103 $this->assertUserLinks( $user->getName(), $cacheEntry );
104
105 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
106 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
107 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
108 }
109
111 $user = $this->getMutableTestUser()->getUser();
112 $recentChange = $this->testRecentChangesHelper->makeDeletedEditRecentChange(
113 $user,
114 'Zzz',
115 '20131103212153',
116 191, // thisid
117 190, // lastid
118 '20131103212153',
119 0, // counter
120 0 // number of watching users
121 );
122 $cacheEntryFactory = new RCCacheEntryFactory(
123 $this->getContext(),
124 $this->getMessages(),
125 $this->linkRenderer
126 );
127 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
128
129 $this->assertInstanceOf( RCCacheEntry::class, $cacheEntry );
130
131 $this->assertEquals( false, $cacheEntry->watched, 'watched' );
132 $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
133 $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
134 $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
135
136 $this->assertRevDel( $cacheEntry );
137 $this->assertTitleLink( 'Zzz', $cacheEntry );
138
139 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
140 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
141 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
142 }
143
144 private function assertValidHTML( $actual ) {
145 // Throws if invalid
146 $doc = PHPUnit_Util_XML::load( $actual, /* isHtml */ true );
147 }
148
149 private function assertUserLinks( $user, $cacheEntry ) {
150 $this->assertValidHTML( $cacheEntry->userlink );
151 $this->assertRegExp(
152 '#^<a .*class="new mw-userlink".*><bdi>' . $user . '</bdi></a>#',
153 $cacheEntry->userlink,
154 'verify user link'
155 );
156
157 $this->assertValidHTML( $cacheEntry->usertalklink );
158 $this->assertRegExp(
159 '#^ <span class="mw-usertoollinks">\‍(.*<a .+>talk</a>.*\‍)</span>#',
160 $cacheEntry->usertalklink,
161 'verify user talk link'
162 );
163
164 $this->assertValidHTML( $cacheEntry->usertalklink );
165 $this->assertRegExp(
166 '#^ <span class="mw-usertoollinks">\‍(.*<a .+>contribs</a>.*\‍)</span>$#',
167 $cacheEntry->usertalklink,
168 'verify user tool links'
169 );
170 }
171
172 private function assertDeleteLogLink( $cacheEntry ) {
173 $this->assertEquals(
174 '(<a href="/wiki/Special:Log/delete" title="Special:Log/delete">Deletion log</a>)',
175 $cacheEntry->link,
176 'verify deletion log link'
177 );
178
179 $this->assertValidHTML( $cacheEntry->link );
180 }
181
182 private function assertRevDel( $cacheEntry ) {
183 $this->assertEquals(
184 ' <span class="history-deleted">(username removed)</span>',
185 $cacheEntry->userlink,
186 'verify user link for change with deleted revision and user'
187 );
188 $this->assertValidHTML( $cacheEntry->userlink );
189 }
190
191 private function assertTitleLink( $title, $cacheEntry ) {
192 $this->assertEquals(
193 '<a href="/wiki/' . $title . '" title="' . $title . '">' . $title . '</a>',
194 $cacheEntry->link,
195 'verify title link'
196 );
197 $this->assertValidHTML( $cacheEntry->link );
198 }
199
200 private function assertQueryLink( $content, $params, $link ) {
201 $this->assertRegExp(
202 "#^<a .+>$content</a>$#",
203 $link,
204 'verify query link element'
205 );
206 $this->assertValidHTML( $link );
207
208 foreach ( $params as $key => $value ) {
209 $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
210 }
211 }
212
213 private function getMessages() {
214 return [
215 'cur' => 'cur',
216 'diff' => 'diff',
217 'hist' => 'hist',
218 'enhancedrc-history' => 'history',
219 'last' => 'prev',
220 'blocklink' => 'block',
221 'history' => 'Page history',
222 'semicolon-separator' => '; ',
223 'pipe-separator' => ' | '
224 ];
225 }
226
227 private function getContext() {
228 $user = $this->getMutableTestUser()->getUser();
229 $context = $this->testRecentChangesHelper->getTestContext( $user );
230
231 $title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
232 $context->setTitle( $title );
233
234 return $context;
235 }
236}
Base class that store and restore the Language objects.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Class that generates HTML links for pages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
TestRecentChangesHelper $testRecentChangesHelper
__construct( $name=null, array $data=[], $dataName='')
assertUserLinks( $user, $cacheEntry)
assertQueryLink( $content, $params, $link)
assertTitleLink( $title, $cacheEntry)
Helper for generating test recent changes entries.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3021
const NS_SPECIAL
Definition Defines.php:63
$params