5 * ^--- make sure temporary tables are used.
7 class LinksUpdateTest
extends MediaWikiTestCase
{
9 function __construct( $name = null, array $data = array(), $dataName = '' ) {
10 parent
::__construct( $name, $data, $dataName );
12 $this->tablesUsed
= array_merge( $this->tablesUsed
,
28 protected function setUp() {
30 $dbw = wfGetDB( DB_MASTER
);
35 'iw_prefix' => 'linksupdatetest',
36 'iw_url' => 'http://testing.com/wiki/$1',
37 'iw_api' => 'http://testing.com/w/api.php',
40 'iw_wikiid' => 'linksupdatetest',
43 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
46 public function addDBData() {
47 $this->insertPage( 'Testing' );
48 $this->insertPage( 'Some_other_page' );
49 $this->insertPage( 'Template:TestingTemplate' );
52 protected function makeTitleAndParserOutput( $name, $id ) {
53 $t = Title
::newFromText( $name );
54 $t->mArticleID
= $id; # XXX: this is fugly
56 $po = new ParserOutput();
57 $po->setTitleText( $t->getPrefixedText() );
59 return array( $t, $po );
63 * @covers ParserOutput::addLink
65 public function testUpdate_pagelinks() {
66 /** @var ParserOutput $po */
67 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
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
74 $update = $this->assertLinksUpdate(
81 array( array( NS_MAIN
, 'Foo' ) )
83 $this->assertArrayEquals( array(
84 Title
::makeTitle( NS_MAIN
, 'Foo' ), // newFromText doesn't yield the same internal state....
85 ), $update->getAddedLinks() );
87 $po = new ParserOutput();
88 $po->setTitleText( $t->getPrefixedText() );
90 $po->addLink( Title
::newFromText( "Bar" ) );
91 $po->addLink( Title
::newFromText( "Talk:Bar" ) );
93 $update = $this->assertLinksUpdate(
101 array( NS_MAIN
, 'Bar' ),
102 array( NS_TALK
, 'Bar' ),
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() );
115 * @covers ParserOutput::addExternalLink
117 public function testUpdate_externallinks() {
118 /** @var ParserOutput $po */
119 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
121 $po->addExternalLink( "http://testing.com/wiki/Foo" );
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' ),
129 * @covers ParserOutput::addCategory
131 public function testUpdate_categorylinks() {
132 /** @var ParserOutput $po */
133 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
135 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
137 $po->addCategory( "Foo", "FOO" );
139 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
140 array( 'Foo', "FOO\nTESTING" ),
144 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
145 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
147 $title = Title
::newFromText( 'Testing' );
148 $wikiPage = new WikiPage( $title );
149 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
151 $this->assertRecentChangeByCategorization(
153 $wikiPage->getParserOutput( new ParserOptions() ),
154 Title
::newFromText( 'Category:Foo' ),
155 array( array( 'Foo', '[[:Testing]] added to category' ) )
158 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'added category' );
159 $this->assertRecentChangeByCategorization(
161 $wikiPage->getParserOutput( new ParserOptions() ),
162 Title
::newFromText( 'Category:Foo' ),
164 array( 'Foo', '[[:Testing]] added to category' ),
165 array( 'Foo', '[[:Testing]] removed from category' ),
169 $this->assertRecentChangeByCategorization(
171 $wikiPage->getParserOutput( new ParserOptions() ),
172 Title
::newFromText( 'Category:Bar' ),
174 array( 'Bar', '[[:Testing]] added to category' ),
179 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
180 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
182 $templateTitle = Title
::newFromText( 'Template:TestingTemplate' );
183 $templatePage = new WikiPage( $templateTitle );
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' );
191 $this->assertRecentChangeByCategorization(
193 $templatePage->getParserOutput( new ParserOptions() ),
194 Title
::newFromText( 'Foo' ),
195 array( array( 'Foo', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
200 * @covers ParserOutput::addInterwikiLink
202 public function testUpdate_iwlinks() {
203 /** @var ParserOutput $po */
204 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
206 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
207 $po->addInterwikiLink( $target );
209 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
210 array( 'linksupdatetest', 'Foo' ),
215 * @covers ParserOutput::addTemplate
217 public function testUpdate_templatelinks() {
218 /** @var ParserOutput $po */
219 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
221 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
223 $this->assertLinksUpdate(
230 array( array( NS_TEMPLATE
, 'Foo' ) )
235 * @covers ParserOutput::addImage
237 public function testUpdate_imagelinks() {
238 /** @var ParserOutput $po */
239 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
241 $po->addImage( "Foo.png" );
243 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
249 * @covers ParserOutput::addLanguageLink
251 public function testUpdate_langlinks() {
252 $this->setMwGlobals( array(
253 'wgCapitalLinks' => true,
256 /** @var ParserOutput $po */
257 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
259 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
261 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
262 array( 'En', 'Foo' ),
267 * @covers ParserOutput::setProperty
269 public function testUpdate_page_props() {
270 global $wgPagePropsHaveSortkey;
272 /** @var ParserOutput $po */
273 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
275 $fields = array( 'pp_propname', 'pp_value' );
278 $po->setProperty( "bool", true );
279 $expected[] = array( "bool", true );
281 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
282 $expected[] = array( "float", 4.0 +
1.0 / 4.0 );
284 $po->setProperty( "int", -7 );
285 $expected[] = array( "int", -7 );
287 $po->setProperty( "string", "33 bar" );
288 $expected[] = array( "string", "33 bar" );
290 // compute expected sortkey values
291 if ( $wgPagePropsHaveSortkey ) {
292 $fields[] = 'pp_sortkey';
294 foreach ( $expected as &$row ) {
297 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
298 $row[] = floatval( $value );
305 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
308 public function testUpdate_page_props_without_sortkey() {
309 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
311 $this->testUpdate_page_props();
314 // @todo test recursive, too!
316 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
317 $table, $fields, $condition, array $expectedRows
319 $update = new LinksUpdate( $title, $parserOutput );
321 // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
322 $update->beginTransaction();
324 $update->commitTransaction();
326 $this->assertSelect( $table, $fields, $condition, $expectedRows );
330 protected function assertRecentChangeByCategorization(
331 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
333 $update = new LinksUpdate( $pageTitle, $parserOutput );
334 $revision = Revision
::newFromTitle( $pageTitle );
335 $update->setRevision( $revision );
336 $update->beginTransaction();
338 $update->commitTransaction();
342 'rc_title, rc_comment',
344 'rc_type' => RC_CATEGORIZE
,
345 'rc_namespace' => NS_CATEGORY
,
346 'rc_title' => $categoryTitle->getDBkey()