Merge "(bug 37755) Set robot meta tags for 'view source' pages"
[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 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
64 array( NS_MAIN, 'Foo' ),
65 ) );
66
67 $po = new ParserOutput();
68 $po->setTitleText( $t->getPrefixedText() );
69
70 $po->addLink( Title::newFromText( "Bar" ) );
71
72 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
73 array( NS_MAIN, 'Bar' ),
74 ) );
75 }
76
77 public function testUpdate_externallinks() {
78 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
79
80 $po->addExternalLink( "http://testing.com/wiki/Foo" );
81
82 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
83 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
84 ) );
85 }
86
87 public function testUpdate_categorylinks() {
88 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
89
90 $po->addCategory( "Foo", "FOO" );
91
92 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
93 array( 'Foo', "FOO\nTESTING" ),
94 ) );
95 }
96
97 public function testUpdate_iwlinks() {
98 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
99
100 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
101 $po->addInterwikiLink( $target );
102
103 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
104 array( 'linksupdatetest', 'Foo' ),
105 ) );
106 }
107
108 public function testUpdate_templatelinks() {
109 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
110
111 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
112
113 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
114 array( NS_TEMPLATE, 'Foo' ),
115 ) );
116 }
117
118 public function testUpdate_imagelinks() {
119 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
120
121 $po->addImage( "Foo.png" );
122
123
124 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
125 array( 'Foo.png' ),
126 ) );
127 }
128
129 public function testUpdate_langlinks() {
130 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
131
132 $po->addLanguageLink( Title::newFromText( "en:Foo" ) );
133
134
135 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
136 array( 'En', 'Foo' ),
137 ) );
138 }
139
140 public function testUpdate_page_props() {
141 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
142
143 $po->setProperty( "foo", "bar" );
144
145 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
146 array( 'foo', 'bar' ),
147 ) );
148 }
149
150 #@todo: test recursive, too!
151
152 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
153 $update = new LinksUpdate( $title, $parserOutput );
154
155 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
156 $update->beginTransaction();
157 $update->doUpdate();
158 $update->commitTransaction();
159
160 $this->assertSelect( $table, $fields, $condition, $expectedRows );
161 }
162 }
163