Merge "deleteEqualMessages: Exclude messages that are empty by default"
[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(
15 'interwiki',
16 'page_props',
17 'pagelinks',
18 'categorylinks',
19 'langlinks',
20 'externallinks',
21 'imagelinks',
22 'templatelinks',
23 'iwlinks'
24 )
25 );
26 }
27
28 protected function setUp() {
29 parent::setUp();
30 $dbw = wfGetDB( DB_MASTER );
31 $dbw->replace(
32 'interwiki',
33 array( 'iw_prefix' ),
34 array(
35 'iw_prefix' => 'linksupdatetest',
36 'iw_url' => 'http://testing.com/wiki/$1',
37 'iw_api' => 'http://testing.com/w/api.php',
38 'iw_local' => 0,
39 'iw_trans' => 0,
40 'iw_wikiid' => 'linksupdatetest',
41 )
42 );
43 }
44
45 protected function makeTitleAndParserOutput( $name, $id ) {
46 $t = Title::newFromText( $name );
47 $t->mArticleID = $id; # XXX: this is fugly
48
49 $po = new ParserOutput();
50 $po->setTitleText( $t->getPrefixedText() );
51
52 return array( $t, $po );
53 }
54
55 public function testUpdate_pagelinks() {
56 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
57
58 $po->addLink( Title::newFromText( "Foo" ) );
59 $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
60 $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
61 $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
62
63 $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
64 array( NS_MAIN, 'Foo' ),
65 ) );
66 $this->assertArrayEquals( array(
67 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
68 ), $update->getAddedLinks() );
69
70 $po = new ParserOutput();
71 $po->setTitleText( $t->getPrefixedText() );
72
73 $po->addLink( Title::newFromText( "Bar" ) );
74 $po->addLink( Title::newFromText( "Talk:Bar" ) );
75
76 $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
77 array( NS_MAIN, 'Bar' ),
78 array( NS_TALK, 'Bar' ),
79 ) );
80 $this->assertArrayEquals( array(
81 Title::makeTitle( NS_MAIN, 'Bar' ),
82 Title::makeTitle( NS_TALK, 'Bar' ),
83 ), $update->getAddedLinks() );
84 $this->assertArrayEquals( array(
85 Title::makeTitle( NS_MAIN, 'Foo' ),
86 ), $update->getRemovedLinks() );
87 }
88
89 public function testUpdate_externallinks() {
90 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
91
92 $po->addExternalLink( "http://testing.com/wiki/Foo" );
93
94 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
95 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
96 ) );
97 }
98
99 public function testUpdate_categorylinks() {
100 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
101
102 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
103
104 $po->addCategory( "Foo", "FOO" );
105
106 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
107 array( 'Foo', "FOO\nTESTING" ),
108 ) );
109 }
110
111 public function testUpdate_iwlinks() {
112 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
113
114 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
115 $po->addInterwikiLink( $target );
116
117 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
118 array( 'linksupdatetest', 'Foo' ),
119 ) );
120 }
121
122 public function testUpdate_templatelinks() {
123 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
124
125 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
126
127 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
128 array( NS_TEMPLATE, 'Foo' ),
129 ) );
130 }
131
132 public function testUpdate_imagelinks() {
133 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
134
135 $po->addImage( "Foo.png" );
136
137 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
138 array( 'Foo.png' ),
139 ) );
140 }
141
142 public function testUpdate_langlinks() {
143 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
144
145 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
146
147 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
148 array( 'En', 'Foo' ),
149 ) );
150 }
151
152 public function testUpdate_page_props() {
153 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
154
155 $po->setProperty( "foo", "bar" );
156
157 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
158 array( 'foo', 'bar' ),
159 ) );
160 }
161
162 // @todo test recursive, too!
163
164 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
165 $update = new LinksUpdate( $title, $parserOutput );
166
167 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
168 $update->beginTransaction();
169 $update->doUpdate();
170 $update->commitTransaction();
171
172 $this->assertSelect( $table, $fields, $condition, $expectedRows );
173 return $update;
174 }
175 }