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',
45 public function addDBData() {
46 $this->insertPage( 'Testing' );
47 $this->insertPage( 'Some_other_page' );
48 $this->insertPage( 'Template:TestingTemplate' );
51 protected function makeTitleAndParserOutput( $name, $id ) {
52 $t = Title
::newFromText( $name );
53 $t->mArticleID
= $id; # XXX: this is fugly
55 $po = new ParserOutput();
56 $po->setTitleText( $t->getPrefixedText() );
58 return array( $t, $po );
62 * @covers ParserOutput::addLink
64 public function testUpdate_pagelinks() {
65 /** @var ParserOutput $po */
66 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
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
73 $update = $this->assertLinksUpdate(
80 array( array( NS_MAIN
, 'Foo' ) )
82 $this->assertArrayEquals( array(
83 Title
::makeTitle( NS_MAIN
, 'Foo' ), // newFromText doesn't yield the same internal state....
84 ), $update->getAddedLinks() );
86 $po = new ParserOutput();
87 $po->setTitleText( $t->getPrefixedText() );
89 $po->addLink( Title
::newFromText( "Bar" ) );
90 $po->addLink( Title
::newFromText( "Talk:Bar" ) );
92 $update = $this->assertLinksUpdate(
100 array( NS_MAIN
, 'Bar' ),
101 array( NS_TALK
, 'Bar' ),
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() );
114 * @covers ParserOutput::addExternalLink
116 public function testUpdate_externallinks() {
117 /** @var ParserOutput $po */
118 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
120 $po->addExternalLink( "http://testing.com/wiki/Foo" );
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' ),
128 * @covers ParserOutput::addCategory
130 public function testUpdate_categorylinks() {
131 /** @var ParserOutput $po */
132 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
134 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
136 $po->addCategory( "Foo", "FOO" );
138 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
139 array( 'Foo', "FOO\nTESTING" ),
143 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
144 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
146 $title = Title
::newFromText( 'Testing' );
147 $wikiPage = new WikiPage( $title );
148 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
150 $this->assertRecentChangeByCategorization(
152 $wikiPage->getParserOutput( new ParserOptions() ),
153 Title
::newFromText( 'Category:Foo' ),
154 array( array( 'Foo', '[[:Testing]] added to category' ) )
157 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'added category' );
158 $this->assertRecentChangeByCategorization(
160 $wikiPage->getParserOutput( new ParserOptions() ),
161 Title
::newFromText( 'Category:Foo' ),
163 array( 'Foo', '[[:Testing]] added to category' ),
164 array( 'Foo', '[[:Testing]] removed from category' ),
168 $this->assertRecentChangeByCategorization(
170 $wikiPage->getParserOutput( new ParserOptions() ),
171 Title
::newFromText( 'Category:Bar' ),
173 array( 'Bar', '[[:Testing]] added to category' ),
178 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
179 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
181 $templateTitle = Title
::newFromText( 'Template:TestingTemplate' );
182 $templatePage = new WikiPage( $templateTitle );
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' );
190 $this->assertRecentChangeByCategorization(
192 $templatePage->getParserOutput( new ParserOptions() ),
193 Title
::newFromText( 'Foo' ),
194 array( array( 'Foo', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
199 * @covers ParserOutput::addInterwikiLink
201 public function testUpdate_iwlinks() {
202 /** @var ParserOutput $po */
203 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
205 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
206 $po->addInterwikiLink( $target );
208 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
209 array( 'linksupdatetest', 'Foo' ),
214 * @covers ParserOutput::addTemplate
216 public function testUpdate_templatelinks() {
217 /** @var ParserOutput $po */
218 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
220 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
222 $this->assertLinksUpdate(
229 array( array( NS_TEMPLATE
, 'Foo' ) )
234 * @covers ParserOutput::addImage
236 public function testUpdate_imagelinks() {
237 /** @var ParserOutput $po */
238 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
240 $po->addImage( "Foo.png" );
242 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
248 * @covers ParserOutput::addLanguageLink
250 public function testUpdate_langlinks() {
251 $this->setMwGlobals( array(
252 'wgCapitalLinks' => true,
255 /** @var ParserOutput $po */
256 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
258 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
260 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
261 array( 'En', 'Foo' ),
266 * @covers ParserOutput::setProperty
268 public function testUpdate_page_props() {
269 global $wgPagePropsHaveSortkey;
271 /** @var ParserOutput $po */
272 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
274 $fields = array( 'pp_propname', 'pp_value' );
277 $po->setProperty( "bool", true );
278 $expected[] = array( "bool", true );
280 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
281 $expected[] = array( "float", 4.0 +
1.0 / 4.0 );
283 $po->setProperty( "int", -7 );
284 $expected[] = array( "int", -7 );
286 $po->setProperty( "string", "33 bar" );
287 $expected[] = array( "string", "33 bar" );
289 // compute expected sortkey values
290 if ( $wgPagePropsHaveSortkey ) {
291 $fields[] = 'pp_sortkey';
293 foreach ( $expected as &$row ) {
296 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
297 $row[] = floatval( $value );
304 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
307 public function testUpdate_page_props_without_sortkey() {
308 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
310 $this->testUpdate_page_props();
313 // @todo test recursive, too!
315 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
316 $table, $fields, $condition, array $expectedRows
318 $update = new LinksUpdate( $title, $parserOutput );
320 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
321 $update->beginTransaction();
323 $update->commitTransaction();
325 $this->assertSelect( $table, $fields, $condition, $expectedRows );
329 protected function assertRecentChangeByCategorization(
330 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
332 $update = new LinksUpdate( $pageTitle, $parserOutput );
333 $revision = Revision
::newFromTitle( $pageTitle );
334 $update->setRevision( $revision );
335 $update->beginTransaction();
337 $update->commitTransaction();
341 'rc_title, rc_comment',
343 'rc_type' => RC_CATEGORIZE
,
344 'rc_namespace' => NS_CATEGORY
,
345 'rc_title' => $categoryTitle->getDBkey()