3 use MediaWiki\Linker\LinkRenderer
;
4 use MediaWiki\MediaWikiServices
;
7 * @covers RCCacheEntryFactory
11 * @author Katie Filbert < aude.wiki@gmail.com >
13 class RCCacheEntryFactoryTest
extends MediaWikiLangTestCase
{
16 * @var TestRecentChangesHelper
18 private $testRecentChangesHelper;
23 private $linkRenderer;
25 public function __construct( $name = null, array $data = [], $dataName = '' ) {
26 parent
::__construct( $name, $data, $dataName );
28 $this->testRecentChangesHelper
= new TestRecentChangesHelper();
31 protected function setUp() {
34 $this->setMwGlobals( [
35 'wgArticlePath' => '/wiki/$1'
38 $this->linkRenderer
= MediaWikiServices
::getInstance()->getLinkRenderer();
41 public function testNewFromRecentChange() {
42 $user = $this->getMutableTestUser()->getUser();
43 $recentChange = $this->testRecentChangesHelper
->makeEditRecentChange(
51 0 // number of watching users
53 $cacheEntryFactory = new RCCacheEntryFactory(
58 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
60 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
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' );
67 $this->assertUserLinks( $user->getName(), $cacheEntry );
68 $this->assertTitleLink( 'Xyz', $cacheEntry );
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
);
77 public function testNewForDeleteChange() {
78 $user = $this->getMutableTestUser()->getUser();
79 $recentChange = $this->testRecentChangesHelper
->makeLogRecentChange(
86 0 // number of watching users
88 $cacheEntryFactory = new RCCacheEntryFactory(
93 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
95 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
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' );
102 $this->assertDeleteLogLink( $cacheEntry );
103 $this->assertUserLinks( $user->getName(), $cacheEntry );
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' );
110 public function testNewForRevUserDeleteChange() {
111 $user = $this->getMutableTestUser()->getUser();
112 $recentChange = $this->testRecentChangesHelper
->makeDeletedEditRecentChange(
120 0 // number of watching users
122 $cacheEntryFactory = new RCCacheEntryFactory(
124 $this->getMessages(),
127 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
129 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
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' );
136 $this->assertRevDel( $cacheEntry );
137 $this->assertTitleLink( 'Zzz', $cacheEntry );
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' );
144 private function assertValidHTML( $actual ) {
146 $doc = PHPUnit_Util_XML
::load( $actual, /* isHtml */ true );
149 private function assertUserLinks( $user, $cacheEntry ) {
150 $this->assertValidHTML( $cacheEntry->userlink
);
152 '#^<a .*class="new mw-userlink".*><bdi>' . $user . '</bdi></a>#',
153 $cacheEntry->userlink
,
157 $this->assertValidHTML( $cacheEntry->usertalklink
);
159 '#^ <span class="mw-usertoollinks">\(.*<a .+>talk</a>.*\)</span>#',
160 $cacheEntry->usertalklink
,
161 'verify user talk link'
164 $this->assertValidHTML( $cacheEntry->usertalklink
);
166 '#^ <span class="mw-usertoollinks">\(.*<a .+>contribs</a>.*\)</span>$#',
167 $cacheEntry->usertalklink
,
168 'verify user tool links'
172 private function assertDeleteLogLink( $cacheEntry ) {
174 '(<a href="/wiki/Special:Log/delete" title="Special:Log/delete">Deletion log</a>)',
176 'verify deletion log link'
179 $this->assertValidHTML( $cacheEntry->link
);
182 private function assertRevDel( $cacheEntry ) {
184 ' <span class="history-deleted">(username removed)</span>',
185 $cacheEntry->userlink
,
186 'verify user link for change with deleted revision and user'
188 $this->assertValidHTML( $cacheEntry->userlink
);
191 private function assertTitleLink( $title, $cacheEntry ) {
193 '<a href="/wiki/' . $title . '" title="' . $title . '">' . $title . '</a>',
197 $this->assertValidHTML( $cacheEntry->link
);
200 private function assertQueryLink( $content, $params, $link ) {
202 "#^<a .+>$content</a>$#",
204 'verify query link element'
206 $this->assertValidHTML( $link );
208 foreach ( $params as $key => $value ) {
209 $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
213 private function getMessages() {
218 'enhancedrc-history' => 'history',
220 'blocklink' => 'block',
221 'history' => 'Page history',
222 'semicolon-separator' => '; ',
223 'pipe-separator' => ' | '
227 private function getContext() {
228 $user = $this->getMutableTestUser()->getUser();
229 $context = $this->testRecentChangesHelper
->getTestContext( $user );
231 $title = Title
::newFromText( 'RecentChanges', NS_SPECIAL
);
232 $context->setTitle( $title );