Merge "fix merge of Iec98e472" into Wikidata
[lhc/web/wiklou.git] / tests / phpunit / includes / LinksUpdateTest.php
1 <?php
2
3 /**
4 *
5 * @group Database
6 * ^--- make sure temporary tables are used.
7 */
8 class LinksUpdateTest extends MediaWikiTestCase {
9
10 function __construct( $name = null, array $data = array(), $dataName = '' ) {
11 parent::__construct( $name, $data, $dataName );
12
13 $this->tablesUsed = array_merge ( $this->tablesUsed,
14 array( 'interwiki',
15
16 'page_props',
17 'pagelinks',
18 'categorylinks',
19 'langlinks',
20 'externallinks',
21 'imagelinks',
22 'templatelinks',
23 'iwlinks' ) );
24 }
25
26 function setUp() {
27 $dbw = wfGetDB( DB_MASTER );
28 $dbw->replace( 'interwiki',
29 array('iw_prefix'),
30 array( 'iw_prefix' => 'linksupdatetest',
31 'iw_url' => 'http://testing.com/wiki/$1',
32 'iw_api' => 'http://testing.com/w/api.php',
33 'iw_local' => 0,
34 'iw_trans' => 0,
35 'iw_wikiid' => 'linksupdatetest',
36 ) );
37 }
38
39 protected function makeTitleAndParserOutput( $name, $id ) {
40 $t = Title::newFromText( $name );
41 $t->mArticleID = $id; # XXX: this is fugly
42
43 $po = new ParserOutput();
44 $po->setTitleText( $t->getPrefixedText() );
45
46 return array( $t, $po );
47 }
48
49 public function testUpdate_pagelinks() {
50 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
51
52 $po->addLink( Title::newFromText( "Foo" ) );
53 $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
54 $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
55 $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
56
57 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
58 array( NS_MAIN, 'Foo' ),
59 ) );
60
61 $po = new ParserOutput();
62 $po->setTitleText( $t->getPrefixedText() );
63
64 $po->addLink( Title::newFromText( "Bar" ) );
65
66 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
67 array( NS_MAIN, 'Bar' ),
68 ) );
69 }
70
71 public function testUpdate_externallinks() {
72 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
73
74 $po->addExternalLink( "http://testing.com/wiki/Foo" );
75
76 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
77 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
78 ) );
79 }
80
81 public function testUpdate_categorylinks() {
82 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
83
84 $po->addCategory( "Foo", "FOO" );
85
86 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
87 array( 'Foo', "FOO\nTESTING" ),
88 ) );
89 }
90
91 public function testUpdate_iwlinks() {
92 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
93
94 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
95 $po->addInterwikiLink( $target );
96
97 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
98 array( 'linksupdatetest', 'Foo' ),
99 ) );
100 }
101
102 public function testUpdate_templatelinks() {
103 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
104
105 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
106
107 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
108 array( NS_TEMPLATE, 'Foo' ),
109 ) );
110 }
111
112 public function testUpdate_imagelinks() {
113 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
114
115 $po->addImage( "Foo.png" );
116
117
118 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
119 array( 'Foo.png' ),
120 ) );
121 }
122
123 public function testUpdate_langlinks() {
124 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
125
126 $po->addLanguageLink( Title::newFromText( "en:Foo" ) );
127
128
129 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
130 array( 'En', 'Foo' ),
131 ) );
132 }
133
134 public function testUpdate_page_props() {
135 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
136
137 $po->setProperty( "foo", "bar" );
138
139 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
140 array( 'foo', 'bar' ),
141 ) );
142 }
143
144 #@todo: test recursive, too!
145
146 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, Array $expectedRows ) {
147 $update = new LinksUpdate( $title, $parserOutput );
148
149 $update->beginTransaction();
150 $update->doUpdate();
151 $update->commitTransaction();
152
153 $this->assertSelect( $table, $fields, $condition, $expectedRows );
154 }
155 }
156