summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/LinksUpdateTest.php
blob: 5ade250e2f078cd6aa9b3b5184a1b7228e811311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 *
 * @group Database
 * ^--- make sure temporary tables are used.
 */
class LinksUpdateTest extends MediaWikiTestCase {

	function __construct( $name = null, array $data = array(), $dataName = '' ) {
		parent::__construct( $name, $data, $dataName );

		$this->tablesUsed = array_merge( $this->tablesUsed,
			array(
				'interwiki',
				'page_props',
				'pagelinks',
				'categorylinks',
				'langlinks',
				'externallinks',
				'imagelinks',
				'templatelinks',
				'iwlinks'
			)
		);
	}

	protected function setUp() {
		parent::setUp();
		$dbw = wfGetDB( DB_MASTER );
		$dbw->replace(
			'interwiki',
			array( 'iw_prefix' ),
			array(
				'iw_prefix' => 'linksupdatetest',
				'iw_url' => 'http://testing.com/wiki/$1',
				'iw_api' => 'http://testing.com/w/api.php',
				'iw_local' => 0,
				'iw_trans' => 0,
				'iw_wikiid' => 'linksupdatetest',
			)
		);
	}

	protected function makeTitleAndParserOutput( $name, $id ) {
		$t = Title::newFromText( $name );
		$t->mArticleID = $id; # XXX: this is fugly

		$po = new ParserOutput();
		$po->setTitleText( $t->getPrefixedText() );

		return array( $t, $po );
	}

	public function testUpdate_pagelinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addLink( Title::newFromText( "Foo" ) );
		$po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
		$po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
		$po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored

		$update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
			array( NS_MAIN, 'Foo' ),
		) );
		$this->assertArrayEquals( array(
			Title::makeTitle( NS_MAIN, 'Foo' ),  // newFromText doesn't yield the same internal state....
		), $update->getAddedLinks() );

		$po = new ParserOutput();
		$po->setTitleText( $t->getPrefixedText() );

		$po->addLink( Title::newFromText( "Bar" ) );
		$po->addLink( Title::newFromText( "Talk:Bar" ) );

		$update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
			array( NS_MAIN, 'Bar' ),
			array( NS_TALK, 'Bar' ),
		) );
		$this->assertArrayEquals( array(
			Title::makeTitle( NS_MAIN, 'Bar' ),
			Title::makeTitle( NS_TALK, 'Bar' ),
		), $update->getAddedLinks() );
		$this->assertArrayEquals( array(
			Title::makeTitle( NS_MAIN, 'Foo' ),
		), $update->getRemovedLinks() );
	}

	public function testUpdate_externallinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addExternalLink( "http://testing.com/wiki/Foo" );

		$this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
			array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
		) );
	}

	public function testUpdate_categorylinks() {
		$this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );

		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addCategory( "Foo", "FOO" );

		$this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
			array( 'Foo', "FOO\nTESTING" ),
		) );
	}

	public function testUpdate_iwlinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
		$po->addInterwikiLink( $target );

		$this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
			array( 'linksupdatetest', 'Foo' ),
		) );
	}

	public function testUpdate_templatelinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );

		$this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
			array( NS_TEMPLATE, 'Foo' ),
		) );
	}

	public function testUpdate_imagelinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addImage( "Foo.png" );

		$this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
			array( 'Foo.png' ),
		) );
	}

	public function testUpdate_langlinks() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );

		$this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
			array( 'En', 'Foo' ),
		) );
	}

	public function testUpdate_page_props() {
		list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );

		$po->setProperty( "foo", "bar" );

		$this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
			array( 'foo', 'bar' ),
		) );
	}

	// @todo test recursive, too!

	protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
		$update = new LinksUpdate( $title, $parserOutput );

		//NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
		$update->beginTransaction();
		$update->doUpdate();
		$update->commitTransaction();

		$this->assertSelect( $table, $fields, $condition, $expectedRows );
		return $update;
	}
}