Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / deferred / LinksUpdateTest.php
blob02f6b2ab2d5a219604dc76456209b561a2484069
1 <?php
3 /**
4 * @group Database
5 * ^--- make sure temporary tables are used.
6 */
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,
13 array(
14 'interwiki',
15 'page_props',
16 'pagelinks',
17 'categorylinks',
18 'langlinks',
19 'externallinks',
20 'imagelinks',
21 'templatelinks',
22 'iwlinks'
27 protected function setUp() {
28 parent::setUp();
29 $dbw = wfGetDB( DB_MASTER );
30 $dbw->replace(
31 'interwiki',
32 array( 'iw_prefix' ),
33 array(
34 'iw_prefix' => 'linksupdatetest',
35 'iw_url' => 'http://testing.com/wiki/$1',
36 'iw_api' => 'http://testing.com/w/api.php',
37 'iw_local' => 0,
38 'iw_trans' => 0,
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 );
54 /**
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(
67 $t,
68 $po,
69 'pagelinks',
70 'pl_namespace,
71 pl_title',
72 'pl_from = 111',
73 array( array( NS_MAIN, 'Foo' ) )
75 $this->assertArrayEquals( array(
76 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
77 ), $update->getAddedLinks() );
79 $po = new ParserOutput();
80 $po->setTitleText( $t->getPrefixedText() );
82 $po->addLink( Title::newFromText( "Bar" ) );
83 $po->addLink( Title::newFromText( "Talk:Bar" ) );
85 $update = $this->assertLinksUpdate(
86 $t,
87 $po,
88 'pagelinks',
89 'pl_namespace,
90 pl_title',
91 'pl_from = 111',
92 array(
93 array( NS_MAIN, 'Bar' ),
94 array( NS_TALK, 'Bar' ),
97 $this->assertArrayEquals( array(
98 Title::makeTitle( NS_MAIN, 'Bar' ),
99 Title::makeTitle( NS_TALK, 'Bar' ),
100 ), $update->getAddedLinks() );
101 $this->assertArrayEquals( array(
102 Title::makeTitle( NS_MAIN, 'Foo' ),
103 ), $update->getRemovedLinks() );
107 * @covers ParserOutput::addExternalLink
109 public function testUpdate_externallinks() {
110 /** @var ParserOutput $po */
111 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
113 $po->addExternalLink( "http://testing.com/wiki/Foo" );
115 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
116 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
117 ) );
121 * @covers ParserOutput::addCategory
123 public function testUpdate_categorylinks() {
124 /** @var ParserOutput $po */
125 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
127 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
129 $po->addCategory( "Foo", "FOO" );
131 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
132 array( 'Foo', "FOO\nTESTING" ),
133 ) );
137 * @covers ParserOutput::addInterwikiLink
139 public function testUpdate_iwlinks() {
140 /** @var ParserOutput $po */
141 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
143 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
144 $po->addInterwikiLink( $target );
146 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
147 array( 'linksupdatetest', 'Foo' ),
148 ) );
152 * @covers ParserOutput::addTemplate
154 public function testUpdate_templatelinks() {
155 /** @var ParserOutput $po */
156 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
158 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
160 $this->assertLinksUpdate(
162 $po,
163 'templatelinks',
164 'tl_namespace,
165 tl_title',
166 'tl_from = 111',
167 array( array( NS_TEMPLATE, 'Foo' ) )
172 * @covers ParserOutput::addImage
174 public function testUpdate_imagelinks() {
175 /** @var ParserOutput $po */
176 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
178 $po->addImage( "Foo.png" );
180 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
181 array( 'Foo.png' ),
182 ) );
186 * @covers ParserOutput::addLanguageLink
188 public function testUpdate_langlinks() {
189 $this->setMwGlobals( array(
190 'wgCapitalLinks' => true,
191 ) );
193 /** @var ParserOutput $po */
194 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
196 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
198 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
199 array( 'En', 'Foo' ),
200 ) );
204 * @covers ParserOutput::setProperty
206 public function testUpdate_page_props() {
207 global $wgPagePropsHaveSortkey;
209 /** @var ParserOutput $po */
210 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
212 $fields = array( 'pp_propname', 'pp_value' );
213 $expected = array();
215 $po->setProperty( "bool", true );
216 $expected[] = array( "bool", true );
218 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
219 $expected[] = array( "float", 4.0 + 1.0 / 4.0 );
221 $po->setProperty( "int", -7 );
222 $expected[] = array( "int", -7 );
224 $po->setProperty( "string", "33 bar" );
225 $expected[] = array( "string", "33 bar" );
227 // compute expected sortkey values
228 if ( $wgPagePropsHaveSortkey ) {
229 $fields[] = 'pp_sortkey';
231 foreach ( $expected as &$row ) {
232 $value = $row[1];
234 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
235 $row[] = floatval( $value );
236 } else {
237 $row[] = null;
242 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
245 public function testUpdate_page_props_without_sortkey() {
246 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
248 $this->testUpdate_page_props();
251 // @todo test recursive, too!
253 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
254 $table, $fields, $condition, array $expectedRows
256 $update = new LinksUpdate( $title, $parserOutput );
258 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
259 $update->beginTransaction();
260 $update->doUpdate();
261 $update->commitTransaction();
263 $this->assertSelect( $table, $fields, $condition, $expectedRows );
264 return $update;