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
,
27 protected function setUp() {
29 $dbw = wfGetDB( DB_MASTER
);
34 'iw_prefix' => 'linksupdatetest',
35 'iw_url' => 'http://testing.com/wiki/$1',
36 'iw_api' => 'http://testing.com/w/api.php',
39 'iw_wikiid' => 'linksupdatetest',
44 protected function makeTitleAndParserOutput( $name, $id ) {
45 $t = Title
::newFromText( $name );
46 $t->mArticleID
= $id; # XXX: this is fugly
48 $po = new ParserOutput();
49 $po->setTitleText( $t->getPrefixedText() );
51 return array( $t, $po );
55 * @covers ParserOutput::addLink
57 public function testUpdate_pagelinks() {
58 /** @var ParserOutput $po */
59 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
61 $po->addLink( Title
::newFromText( "Foo" ) );
62 $po->addLink( Title
::newFromText( "Special:Foo" ) ); // special namespace should be ignored
63 $po->addLink( Title
::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
64 $po->addLink( Title
::newFromText( "#Foo" ) ); // hash link should be ignored
66 $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
67 array( NS_MAIN
, 'Foo' ),
69 $this->assertArrayEquals( array(
70 Title
::makeTitle( NS_MAIN
, 'Foo' ), // newFromText doesn't yield the same internal state....
71 ), $update->getAddedLinks() );
73 $po = new ParserOutput();
74 $po->setTitleText( $t->getPrefixedText() );
76 $po->addLink( Title
::newFromText( "Bar" ) );
77 $po->addLink( Title
::newFromText( "Talk:Bar" ) );
79 $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
80 array( NS_MAIN
, 'Bar' ),
81 array( NS_TALK
, 'Bar' ),
83 $this->assertArrayEquals( array(
84 Title
::makeTitle( NS_MAIN
, 'Bar' ),
85 Title
::makeTitle( NS_TALK
, 'Bar' ),
86 ), $update->getAddedLinks() );
87 $this->assertArrayEquals( array(
88 Title
::makeTitle( NS_MAIN
, 'Foo' ),
89 ), $update->getRemovedLinks() );
93 * @covers ParserOutput::addExternalLink
95 public function testUpdate_externallinks() {
96 /** @var ParserOutput $po */
97 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
99 $po->addExternalLink( "http://testing.com/wiki/Foo" );
101 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
102 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
107 * @covers ParserOutput::addCategory
109 public function testUpdate_categorylinks() {
110 /** @var ParserOutput $po */
111 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
113 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
115 $po->addCategory( "Foo", "FOO" );
117 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
118 array( 'Foo', "FOO\nTESTING" ),
123 * @covers ParserOutput::addInterwikiLink
125 public function testUpdate_iwlinks() {
126 /** @var ParserOutput $po */
127 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
129 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
130 $po->addInterwikiLink( $target );
132 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
133 array( 'linksupdatetest', 'Foo' ),
138 * @covers ParserOutput::addTemplate
140 public function testUpdate_templatelinks() {
141 /** @var ParserOutput $po */
142 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
144 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
146 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
147 array( NS_TEMPLATE
, 'Foo' ),
152 * @covers ParserOutput::addImage
154 public function testUpdate_imagelinks() {
155 /** @var ParserOutput $po */
156 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
158 $po->addImage( "Foo.png" );
160 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
166 * @covers ParserOutput::addLanguageLink
168 public function testUpdate_langlinks() {
169 /** @var ParserOutput $po */
170 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
172 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
174 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
175 array( 'En', 'Foo' ),
180 * @covers ParserOutput::setProperty
182 public function testUpdate_page_props() {
183 global $wgPagePropsHaveSortkey;
185 /** @var ParserOutput $po */
186 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
188 $fields = array( 'pp_propname', 'pp_value' );
191 $po->setProperty( "bool", true );
192 $expected[] = array( "bool", true );
194 $po->setProperty( "float", 4.0 +
1.0/4.0 );
195 $expected[] = array( "float", 4.0 +
1.0/4.0 );
197 $po->setProperty( "int", -7 );
198 $expected[] = array( "int", -7 );
200 $po->setProperty( "string", "33 bar" );
201 $expected[] = array( "string", "33 bar" );
203 // compute expected sortkey values
204 if ( $wgPagePropsHaveSortkey ) {
205 $fields[] = 'pp_sortkey';
207 foreach ( $expected as &$row ) {
210 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
211 $row[] = floatval( $value );
218 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
221 public function testUpdate_page_props_without_sortkey() {
222 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
224 $this->testUpdate_page_props();
227 // @todo test recursive, too!
229 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput, $table, $fields, $condition, array $expectedRows ) {
230 $update = new LinksUpdate( $title, $parserOutput );
232 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
233 $update->beginTransaction();
235 $update->commitTransaction();
237 $this->assertSelect( $table, $fields, $condition, $expectedRows );