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