Merge "Blacklist Google Glass web browser from JS"
[mediawiki.git] / tests / phpunit / includes / LinksUpdateTest.php
blobb37ff2e3b76692ef49a5134da5944b788cedea5c
1 <?php
3 /**
5 * @group Database
6 * ^--- make sure temporary tables are used.
7 */
8 class LinksUpdateTest extends MediaWikiTestCase {
10 function __construct( $name = null, array $data = array(), $dataName = '' ) {
11 parent::__construct( $name, $data, $dataName );
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'
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',
45 protected function makeTitleAndParserOutput( $name, $id ) {
46 $t = Title::newFromText( $name );
47 $t->mArticleID = $id; # XXX: this is fugly
49 $po = new ParserOutput();
50 $po->setTitleText( $t->getPrefixedText() );
52 return array( $t, $po );
55 /**
56 * @covers LinksUpdate::addLink
58 public function testUpdate_pagelinks() {
59 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
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
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() );
73 $po = new ParserOutput();
74 $po->setTitleText( $t->getPrefixedText() );
76 $po->addLink( Title::newFromText( "Bar" ) );
77 $po->addLink( Title::newFromText( "Talk:Bar" ) );
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() );
92 /**
93 * @covers LinksUpdate::addExternalLink
95 public function testUpdate_externallinks() {
96 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
98 $po->addExternalLink( "http://testing.com/wiki/Foo" );
100 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
101 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
102 ) );
106 * @covers LinksUpdate::addCategory
108 public function testUpdate_categorylinks() {
109 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
111 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
113 $po->addCategory( "Foo", "FOO" );
115 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
116 array( 'Foo', "FOO\nTESTING" ),
117 ) );
121 * @covers LinksUpdate::addInterwikiLink
123 public function testUpdate_iwlinks() {
124 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
126 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
127 $po->addInterwikiLink( $target );
129 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
130 array( 'linksupdatetest', 'Foo' ),
131 ) );
135 * @covers LinksUpdate::addTemplate
137 public function testUpdate_templatelinks() {
138 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
140 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
142 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
143 array( NS_TEMPLATE, 'Foo' ),
144 ) );
148 * @covers LinksUpdate::addImage
150 public function testUpdate_imagelinks() {
151 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
153 $po->addImage( "Foo.png" );
155 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
156 array( 'Foo.png' ),
157 ) );
161 * @covers LinksUpdate::addLanguageLink
163 public function testUpdate_langlinks() {
164 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
166 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
168 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
169 array( 'En', 'Foo' ),
170 ) );
174 * @covers LinksUpdate::setProperty
176 public function testUpdate_page_props() {
177 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
179 $po->setProperty( "foo", "bar" );
181 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
182 array( 'foo', 'bar' ),
183 ) );
186 // @todo test recursive, too!
188 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
189 $update = new LinksUpdate( $title, $parserOutput );
191 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
192 $update->beginTransaction();
193 $update->doUpdate();
194 $update->commitTransaction();
196 $this->assertSelect( $table, $fields, $condition, $expectedRows );
197 return $update;