Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / changes / RCCacheEntryFactoryTest.php
blobccabab68a90aa4f153999ccea5bd75b0227115cf
1 <?php
3 use MediaWiki\Linker\LinkRenderer;
4 use MediaWiki\MediaWikiServices;
6 /**
7 * @covers RCCacheEntryFactory
9 * @group Database
11 * @author Katie Filbert < aude.wiki@gmail.com >
13 class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
15 /**
16 * @var TestRecentChangesHelper
18 private $testRecentChangesHelper;
20 /**
21 * @var LinkRenderer;
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() {
32 parent::setUp();
34 $this->setMwGlobals( [
35 'wgArticlePath' => '/wiki/$1'
36 ] );
38 $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
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
53 $cacheEntryFactory = new RCCacheEntryFactory(
54 $this->getContext(),
55 $this->getMessages(),
56 $this->linkRenderer
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, 'cur link' );
73 $this->assertQueryLink( 'prev', $diff, $cacheEntry->lastlink, 'prev link' );
74 $this->assertQueryLink( 'diff', $diff, $cacheEntry->difflink, 'diff link' );
77 public function testNewForDeleteChange() {
78 $expected = [
79 'title' => 'Abc',
80 'user' => 'TestRecentChangesUser',
81 'timestamp' => '21:21',
82 'numberofWatchingusers' => 0,
83 'unpatrolled' => false
85 $user = $this->getMutableTestUser()->getUser();
86 $recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
87 'delete',
88 'delete',
89 $user,
90 'Abc',
91 '20131103212153',
92 0, // counter
93 0 // number of watching users
95 $cacheEntryFactory = new RCCacheEntryFactory(
96 $this->getContext(),
97 $this->getMessages(),
98 $this->linkRenderer
100 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
102 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
104 $this->assertEquals( false, $cacheEntry->watched, 'watched' );
105 $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
106 $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
107 $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
109 $this->assertDeleteLogLink( $cacheEntry );
110 $this->assertUserLinks( $user->getName(), $cacheEntry );
112 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
113 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
114 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
117 public function testNewForRevUserDeleteChange() {
118 $user = $this->getMutableTestUser()->getUser();
119 $recentChange = $this->testRecentChangesHelper->makeDeletedEditRecentChange(
120 $user,
121 'Zzz',
122 '20131103212153',
123 191, // thisid
124 190, // lastid
125 '20131103212153',
126 0, // counter
127 0 // number of watching users
129 $cacheEntryFactory = new RCCacheEntryFactory(
130 $this->getContext(),
131 $this->getMessages(),
132 $this->linkRenderer
134 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
136 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
138 $this->assertEquals( false, $cacheEntry->watched, 'watched' );
139 $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
140 $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
141 $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
143 $this->assertRevDel( $cacheEntry );
144 $this->assertTitleLink( 'Zzz', $cacheEntry );
146 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
147 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
148 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
151 private function assertUserLinks( $user, $cacheEntry ) {
152 $this->assertTag(
154 'tag' => 'a',
155 'attributes' => [
156 'class' => 'new mw-userlink'
158 'content' => $user
160 $cacheEntry->userlink,
161 'verify user link'
164 $this->assertTag(
166 'tag' => 'span',
167 'attributes' => [
168 'class' => 'mw-usertoollinks'
170 'child' => [
171 'tag' => 'a',
172 'content' => 'talk',
175 $cacheEntry->usertalklink,
176 'verify user talk link'
179 $this->assertTag(
181 'tag' => 'span',
182 'attributes' => [
183 'class' => 'mw-usertoollinks'
185 'child' => [
186 'tag' => 'a',
187 'content' => 'contribs',
190 $cacheEntry->usertalklink,
191 'verify user tool links'
195 private function assertDeleteLogLink( $cacheEntry ) {
196 $this->assertTag(
198 'tag' => 'a',
199 'attributes' => [
200 'href' => '/wiki/Special:Log/delete',
201 'title' => 'Special:Log/delete'
203 'content' => 'Deletion log'
205 $cacheEntry->link,
206 'verify deletion log link'
210 private function assertRevDel( $cacheEntry ) {
211 $this->assertTag(
213 'tag' => 'span',
214 'attributes' => [
215 'class' => 'history-deleted'
217 'content' => '(username removed)'
219 $cacheEntry->userlink,
220 'verify user link for change with deleted revision and user'
224 private function assertTitleLink( $title, $cacheEntry ) {
225 $this->assertTag(
227 'tag' => 'a',
228 'attributes' => [
229 'href' => '/wiki/' . $title,
230 'title' => $title
232 'content' => $title
234 $cacheEntry->link,
235 'verify title link'
239 private function assertQueryLink( $content, $params, $link ) {
240 $this->assertTag(
242 'tag' => 'a',
243 'content' => $content
245 $link,
246 'assert query link element'
249 foreach ( $params as $key => $value ) {
250 $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
254 private function getMessages() {
255 return [
256 'cur' => 'cur',
257 'diff' => 'diff',
258 'hist' => 'hist',
259 'enhancedrc-history' => 'history',
260 'last' => 'prev',
261 'blocklink' => 'block',
262 'history' => 'Page history',
263 'semicolon-separator' => '; ',
264 'pipe-separator' => ' | '
268 private function getContext() {
269 $user = $this->getMutableTestUser()->getUser();
270 $context = $this->testRecentChangesHelper->getTestContext( $user );
272 $title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
273 $context->setTitle( $title );
275 return $context;