Merge "Enable users to watch category membership changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / LinksUpdateTest.php
1 <?php
2
3 /**
4 * @group Database
5 * ^--- make sure temporary tables are used.
6 */
7 class LinksUpdateTest extends MediaWikiTestCase {
8
9 function __construct( $name = null, array $data = array(), $dataName = '' ) {
10 parent::__construct( $name, $data, $dataName );
11
12 $this->tablesUsed = array_merge( $this->tablesUsed,
13 array(
14 'interwiki',
15 'page_props',
16 'pagelinks',
17 'categorylinks',
18 'langlinks',
19 'externallinks',
20 'imagelinks',
21 'templatelinks',
22 'iwlinks',
23 'recentchanges',
24 )
25 );
26 }
27
28 protected function setUp() {
29 parent::setUp();
30 $dbw = wfGetDB( DB_MASTER );
31 $dbw->replace(
32 'interwiki',
33 array( 'iw_prefix' ),
34 array(
35 'iw_prefix' => 'linksupdatetest',
36 'iw_url' => 'http://testing.com/wiki/$1',
37 'iw_api' => 'http://testing.com/w/api.php',
38 'iw_local' => 0,
39 'iw_trans' => 0,
40 'iw_wikiid' => 'linksupdatetest',
41 )
42 );
43 }
44
45 public function addDBData() {
46 $this->insertPage( 'Testing' );
47 $this->insertPage( 'Some_other_page' );
48 $this->insertPage( 'Template:TestingTemplate' );
49 }
50
51 protected function makeTitleAndParserOutput( $name, $id ) {
52 $t = Title::newFromText( $name );
53 $t->mArticleID = $id; # XXX: this is fugly
54
55 $po = new ParserOutput();
56 $po->setTitleText( $t->getPrefixedText() );
57
58 return array( $t, $po );
59 }
60
61 /**
62 * @covers ParserOutput::addLink
63 */
64 public function testUpdate_pagelinks() {
65 /** @var ParserOutput $po */
66 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
67
68 $po->addLink( Title::newFromText( "Foo" ) );
69 $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
70 $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
71 $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
72
73 $update = $this->assertLinksUpdate(
74 $t,
75 $po,
76 'pagelinks',
77 'pl_namespace,
78 pl_title',
79 'pl_from = 111',
80 array( array( NS_MAIN, 'Foo' ) )
81 );
82 $this->assertArrayEquals( array(
83 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
84 ), $update->getAddedLinks() );
85
86 $po = new ParserOutput();
87 $po->setTitleText( $t->getPrefixedText() );
88
89 $po->addLink( Title::newFromText( "Bar" ) );
90 $po->addLink( Title::newFromText( "Talk:Bar" ) );
91
92 $update = $this->assertLinksUpdate(
93 $t,
94 $po,
95 'pagelinks',
96 'pl_namespace,
97 pl_title',
98 'pl_from = 111',
99 array(
100 array( NS_MAIN, 'Bar' ),
101 array( NS_TALK, 'Bar' ),
102 )
103 );
104 $this->assertArrayEquals( array(
105 Title::makeTitle( NS_MAIN, 'Bar' ),
106 Title::makeTitle( NS_TALK, 'Bar' ),
107 ), $update->getAddedLinks() );
108 $this->assertArrayEquals( array(
109 Title::makeTitle( NS_MAIN, 'Foo' ),
110 ), $update->getRemovedLinks() );
111 }
112
113 /**
114 * @covers ParserOutput::addExternalLink
115 */
116 public function testUpdate_externallinks() {
117 /** @var ParserOutput $po */
118 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
119
120 $po->addExternalLink( "http://testing.com/wiki/Foo" );
121
122 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
123 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
124 ) );
125 }
126
127 /**
128 * @covers ParserOutput::addCategory
129 */
130 public function testUpdate_categorylinks() {
131 /** @var ParserOutput $po */
132 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
133
134 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
135
136 $po->addCategory( "Foo", "FOO" );
137
138 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
139 array( 'Foo', "FOO\nTESTING" ),
140 ) );
141 }
142
143 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
144 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
145
146 $title = Title::newFromText( 'Testing' );
147 $wikiPage = new WikiPage( $title );
148 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
149
150 $this->assertRecentChangeByCategorization(
151 $title,
152 $wikiPage->getParserOutput( new ParserOptions() ),
153 Title::newFromText( 'Category:Foo' ),
154 array( array( 'Foo', '[[:Testing]] added to category' ) )
155 );
156
157 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'added category' );
158 $this->assertRecentChangeByCategorization(
159 $title,
160 $wikiPage->getParserOutput( new ParserOptions() ),
161 Title::newFromText( 'Category:Foo' ),
162 array(
163 array( 'Foo', '[[:Testing]] added to category' ),
164 array( 'Foo', '[[:Testing]] removed from category' ),
165 )
166 );
167
168 $this->assertRecentChangeByCategorization(
169 $title,
170 $wikiPage->getParserOutput( new ParserOptions() ),
171 Title::newFromText( 'Category:Bar' ),
172 array(
173 array( 'Bar', '[[:Testing]] added to category' ),
174 )
175 );
176 }
177
178 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
179 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
180
181 $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
182 $templatePage = new WikiPage( $templateTitle );
183
184 $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
185 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
186 $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
187 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
188 $templatePage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
189
190 $this->assertRecentChangeByCategorization(
191 $templateTitle,
192 $templatePage->getParserOutput( new ParserOptions() ),
193 Title::newFromText( 'Foo' ),
194 array( array( 'Foo', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
195 );
196 }
197
198 /**
199 * @covers ParserOutput::addInterwikiLink
200 */
201 public function testUpdate_iwlinks() {
202 /** @var ParserOutput $po */
203 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
204
205 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
206 $po->addInterwikiLink( $target );
207
208 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
209 array( 'linksupdatetest', 'Foo' ),
210 ) );
211 }
212
213 /**
214 * @covers ParserOutput::addTemplate
215 */
216 public function testUpdate_templatelinks() {
217 /** @var ParserOutput $po */
218 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
219
220 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
221
222 $this->assertLinksUpdate(
223 $t,
224 $po,
225 'templatelinks',
226 'tl_namespace,
227 tl_title',
228 'tl_from = 111',
229 array( array( NS_TEMPLATE, 'Foo' ) )
230 );
231 }
232
233 /**
234 * @covers ParserOutput::addImage
235 */
236 public function testUpdate_imagelinks() {
237 /** @var ParserOutput $po */
238 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
239
240 $po->addImage( "Foo.png" );
241
242 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
243 array( 'Foo.png' ),
244 ) );
245 }
246
247 /**
248 * @covers ParserOutput::addLanguageLink
249 */
250 public function testUpdate_langlinks() {
251 $this->setMwGlobals( array(
252 'wgCapitalLinks' => true,
253 ) );
254
255 /** @var ParserOutput $po */
256 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
257
258 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
259
260 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
261 array( 'En', 'Foo' ),
262 ) );
263 }
264
265 /**
266 * @covers ParserOutput::setProperty
267 */
268 public function testUpdate_page_props() {
269 global $wgPagePropsHaveSortkey;
270
271 /** @var ParserOutput $po */
272 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
273
274 $fields = array( 'pp_propname', 'pp_value' );
275 $expected = array();
276
277 $po->setProperty( "bool", true );
278 $expected[] = array( "bool", true );
279
280 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
281 $expected[] = array( "float", 4.0 + 1.0 / 4.0 );
282
283 $po->setProperty( "int", -7 );
284 $expected[] = array( "int", -7 );
285
286 $po->setProperty( "string", "33 bar" );
287 $expected[] = array( "string", "33 bar" );
288
289 // compute expected sortkey values
290 if ( $wgPagePropsHaveSortkey ) {
291 $fields[] = 'pp_sortkey';
292
293 foreach ( $expected as &$row ) {
294 $value = $row[1];
295
296 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
297 $row[] = floatval( $value );
298 } else {
299 $row[] = null;
300 }
301 }
302 }
303
304 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
305 }
306
307 public function testUpdate_page_props_without_sortkey() {
308 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
309
310 $this->testUpdate_page_props();
311 }
312
313 // @todo test recursive, too!
314
315 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
316 $table, $fields, $condition, array $expectedRows
317 ) {
318 $update = new LinksUpdate( $title, $parserOutput );
319
320 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
321 $update->beginTransaction();
322 $update->doUpdate();
323 $update->commitTransaction();
324
325 $this->assertSelect( $table, $fields, $condition, $expectedRows );
326 return $update;
327 }
328
329 protected function assertRecentChangeByCategorization(
330 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
331 ) {
332 $update = new LinksUpdate( $pageTitle, $parserOutput );
333 $revision = Revision::newFromTitle( $pageTitle );
334 $update->setRevision( $revision );
335 $update->beginTransaction();
336 $update->doUpdate();
337 $update->commitTransaction();
338
339 $this->assertSelect(
340 'recentchanges',
341 'rc_title, rc_comment',
342 array(
343 'rc_type' => RC_CATEGORIZE,
344 'rc_namespace' => NS_CATEGORY,
345 'rc_title' => $categoryTitle->getDBkey()
346 ),
347 $expectedRows
348 );
349 }
350 }