Merge "Provide direction hinting in the personal toolbar"
[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( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
67 array( NS_MAIN, 'Foo' ),
68 ) );
69 $this->assertArrayEquals( array(
70 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
71 ), $update->getAddedLinks() );
72
73 $po = new ParserOutput();
74 $po->setTitleText( $t->getPrefixedText() );
75
76 $po->addLink( Title::newFromText( "Bar" ) );
77 $po->addLink( Title::newFromText( "Talk:Bar" ) );
78
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' ),
82 ) );
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() );
90 }
91
92 /**
93 * @covers ParserOutput::addExternalLink
94 */
95 public function testUpdate_externallinks() {
96 /** @var ParserOutput $po */
97 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
98
99 $po->addExternalLink( "http://testing.com/wiki/Foo" );
100
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' ),
103 ) );
104 }
105
106 /**
107 * @covers ParserOutput::addCategory
108 */
109 public function testUpdate_categorylinks() {
110 /** @var ParserOutput $po */
111 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
112
113 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
114
115 $po->addCategory( "Foo", "FOO" );
116
117 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
118 array( 'Foo', "FOO\nTESTING" ),
119 ) );
120 }
121
122 /**
123 * @covers ParserOutput::addInterwikiLink
124 */
125 public function testUpdate_iwlinks() {
126 /** @var ParserOutput $po */
127 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
128
129 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
130 $po->addInterwikiLink( $target );
131
132 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
133 array( 'linksupdatetest', 'Foo' ),
134 ) );
135 }
136
137 /**
138 * @covers ParserOutput::addTemplate
139 */
140 public function testUpdate_templatelinks() {
141 /** @var ParserOutput $po */
142 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
143
144 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
145
146 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
147 array( NS_TEMPLATE, 'Foo' ),
148 ) );
149 }
150
151 /**
152 * @covers ParserOutput::addImage
153 */
154 public function testUpdate_imagelinks() {
155 /** @var ParserOutput $po */
156 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
157
158 $po->addImage( "Foo.png" );
159
160 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
161 array( 'Foo.png' ),
162 ) );
163 }
164
165 /**
166 * @covers ParserOutput::addLanguageLink
167 */
168 public function testUpdate_langlinks() {
169 /** @var ParserOutput $po */
170 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
171
172 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
173
174 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
175 array( 'En', 'Foo' ),
176 ) );
177 }
178
179 /**
180 * @covers ParserOutput::setProperty
181 */
182 public function testUpdate_page_props() {
183 /** @var ParserOutput $po */
184 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
185
186 $po->setProperty( "foo", "bar" );
187
188 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
189 array( 'foo', 'bar' ),
190 ) );
191 }
192
193 // @todo test recursive, too!
194
195 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
196 $update = new LinksUpdate( $title, $parserOutput );
197
198 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
199 $update->beginTransaction();
200 $update->doUpdate();
201 $update->commitTransaction();
202
203 $this->assertSelect( $table, $fields, $condition, $expectedRows );
204 return $update;
205 }
206 }