Merge "Allow third party code to hook-up MIME type detection"
[lhc/web/wiklou.git] / tests / phpunit / includes / LinksUpdateTest.php
1 <?php
2
3 /**
4 * @group Database
5 * ^--- make sure temporary tables are used.
6 */
7 class LinksUpdateTest extends MediaWikiTestCase {
8
9 function __construct( $name = null, array $data = array(), $dataName = '' ) {
10 parent::__construct( $name, $data, $dataName );
11
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'
23 )
24 );
25 }
26
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',
40 )
41 );
42 }
43
44 protected function makeTitleAndParserOutput( $name, $id ) {
45 $t = Title::newFromText( $name );
46 $t->mArticleID = $id; # XXX: this is fugly
47
48 $po = new ParserOutput();
49 $po->setTitleText( $t->getPrefixedText() );
50
51 return array( $t, $po );
52 }
53
54 /**
55 * @covers ParserOutput::addLink
56 */
57 public function testUpdate_pagelinks() {
58 /** @var ParserOutput $po */
59 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
60
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
65
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' ) )
74 );
75 $this->assertArrayEquals( array(
76 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
77 ), $update->getAddedLinks() );
78
79 $po = new ParserOutput();
80 $po->setTitleText( $t->getPrefixedText() );
81
82 $po->addLink( Title::newFromText( "Bar" ) );
83 $po->addLink( Title::newFromText( "Talk:Bar" ) );
84
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' ),
95 )
96 );
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() );
104 }
105
106 /**
107 * @covers ParserOutput::addExternalLink
108 */
109 public function testUpdate_externallinks() {
110 /** @var ParserOutput $po */
111 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
112
113 $po->addExternalLink( "http://testing.com/wiki/Foo" );
114
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 ) );
118 }
119
120 /**
121 * @covers ParserOutput::addCategory
122 */
123 public function testUpdate_categorylinks() {
124 /** @var ParserOutput $po */
125 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
126
127 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
128
129 $po->addCategory( "Foo", "FOO" );
130
131 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
132 array( 'Foo', "FOO\nTESTING" ),
133 ) );
134 }
135
136 /**
137 * @covers ParserOutput::addInterwikiLink
138 */
139 public function testUpdate_iwlinks() {
140 /** @var ParserOutput $po */
141 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
142
143 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
144 $po->addInterwikiLink( $target );
145
146 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
147 array( 'linksupdatetest', 'Foo' ),
148 ) );
149 }
150
151 /**
152 * @covers ParserOutput::addTemplate
153 */
154 public function testUpdate_templatelinks() {
155 /** @var ParserOutput $po */
156 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
157
158 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
159
160 $this->assertLinksUpdate(
161 $t,
162 $po,
163 'templatelinks',
164 'tl_namespace,
165 tl_title',
166 'tl_from = 111',
167 array( array( NS_TEMPLATE, 'Foo' ) )
168 );
169 }
170
171 /**
172 * @covers ParserOutput::addImage
173 */
174 public function testUpdate_imagelinks() {
175 /** @var ParserOutput $po */
176 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
177
178 $po->addImage( "Foo.png" );
179
180 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
181 array( 'Foo.png' ),
182 ) );
183 }
184
185 /**
186 * @covers ParserOutput::addLanguageLink
187 */
188 public function testUpdate_langlinks() {
189 $this->setMwGlobals( array(
190 'wgCapitalLinks' => true,
191 ) );
192
193 /** @var ParserOutput $po */
194 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
195
196 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
197
198 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
199 array( 'En', 'Foo' ),
200 ) );
201 }
202
203 /**
204 * @covers ParserOutput::setProperty
205 */
206 public function testUpdate_page_props() {
207 global $wgPagePropsHaveSortkey;
208
209 /** @var ParserOutput $po */
210 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
211
212 $fields = array( 'pp_propname', 'pp_value' );
213 $expected = array();
214
215 $po->setProperty( "bool", true );
216 $expected[] = array( "bool", true );
217
218 $po->setProperty( "float", 4.0 + 1.0/4.0 );
219 $expected[] = array( "float", 4.0 + 1.0/4.0 );
220
221 $po->setProperty( "int", -7 );
222 $expected[] = array( "int", -7 );
223
224 $po->setProperty( "string", "33 bar" );
225 $expected[] = array( "string", "33 bar" );
226
227 // compute expected sortkey values
228 if ( $wgPagePropsHaveSortkey ) {
229 $fields[] = 'pp_sortkey';
230
231 foreach ( $expected as &$row ) {
232 $value = $row[1];
233
234 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
235 $row[] = floatval( $value );
236 } else {
237 $row[] = null;
238 }
239 }
240 }
241
242 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
243 }
244
245 public function testUpdate_page_props_without_sortkey() {
246 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
247
248 $this->testUpdate_page_props();
249 }
250
251 // @todo test recursive, too!
252
253 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
254 $table, $fields, $condition, array $expectedRows
255 ) {
256 $update = new LinksUpdate( $title, $parserOutput );
257
258 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
259 $update->beginTransaction();
260 $update->doUpdate();
261 $update->commitTransaction();
262
263 $this->assertSelect( $table, $fields, $condition, $expectedRows );
264 return $update;
265 }
266 }