Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / site / SiteExporterTest.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 *
21 * @ingroup Site
22 * @ingroup Test
23 *
24 * @group Site
25 *
26 * @covers SiteExporter
27 *
28 * @author Daniel Kinzler
29 */
30 class SiteExporterTest extends MediaWikiUnitTestCase {
31
32 public function testConstructor_InvalidArgument() {
33 $this->setExpectedException( InvalidArgumentException::class );
34
35 new SiteExporter( 'Foo' );
36 }
37
38 public function testExportSites() {
39 $foo = Site::newForType( Site::TYPE_UNKNOWN );
40 $foo->setGlobalId( 'Foo' );
41
42 $acme = Site::newForType( Site::TYPE_UNKNOWN );
43 $acme->setGlobalId( 'acme.com' );
44 $acme->setGroup( 'Test' );
45 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
46 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
47
48 $tmp = tmpfile();
49 $exporter = new SiteExporter( $tmp );
50
51 $exporter->exportSites( [ $foo, $acme ] );
52
53 fseek( $tmp, 0 );
54 $xml = fread( $tmp, 16 * 1024 );
55
56 $this->assertContains( '<sites ', $xml );
57 $this->assertContains( '<site>', $xml );
58 $this->assertContains( '<globalid>Foo</globalid>', $xml );
59 $this->assertContains( '</site>', $xml );
60 $this->assertContains( '<globalid>acme.com</globalid>', $xml );
61 $this->assertContains( '<group>Test</group>', $xml );
62 $this->assertContains( '<localid type="interwiki">acme</localid>', $xml );
63 $this->assertContains( '<path type="link">http://acme.com/</path>', $xml );
64 $this->assertContains( '</sites>', $xml );
65
66 // NOTE: HHVM (at least on wmf Jenkins) doesn't like file URLs.
67 $xsdFile = __DIR__ . '/../../../../../docs/sitelist-1.0.xsd';
68 $xsdData = file_get_contents( $xsdFile );
69
70 $document = new DOMDocument();
71 $document->loadXML( $xml, LIBXML_NONET );
72 $document->schemaValidateSource( $xsdData );
73 }
74
75 private function newSiteStore( SiteList $sites ) {
76 $store = $this->getMockBuilder( SiteStore::class )->getMock();
77
78 $store->expects( $this->once() )
79 ->method( 'saveSites' )
80 ->will( $this->returnCallback( function ( $moreSites ) use ( $sites ) {
81 foreach ( $moreSites as $site ) {
82 $sites->setSite( $site );
83 }
84 } ) );
85
86 $store->expects( $this->any() )
87 ->method( 'getSites' )
88 ->will( $this->returnValue( new SiteList() ) );
89
90 return $store;
91 }
92
93 public function provideRoundTrip() {
94 $foo = Site::newForType( Site::TYPE_UNKNOWN );
95 $foo->setGlobalId( 'Foo' );
96
97 $acme = Site::newForType( Site::TYPE_UNKNOWN );
98 $acme->setGlobalId( 'acme.com' );
99 $acme->setGroup( 'Test' );
100 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
101 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
102
103 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
104 $dewiki->setGlobalId( 'dewiki' );
105 $dewiki->setGroup( 'wikipedia' );
106 $dewiki->setForward( true );
107 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
108 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
109 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
110 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
111 $dewiki->setSource( 'meta.wikimedia.org' );
112
113 return [
114 'empty' => [
115 new SiteList()
116 ],
117
118 'some' => [
119 new SiteList( [ $foo, $acme, $dewiki ] ),
120 ],
121 ];
122 }
123
124 /**
125 * @dataProvider provideRoundTrip()
126 */
127 public function testRoundTrip( SiteList $sites ) {
128 $tmp = tmpfile();
129 $exporter = new SiteExporter( $tmp );
130
131 $exporter->exportSites( $sites );
132
133 fseek( $tmp, 0 );
134 $xml = fread( $tmp, 16 * 1024 );
135
136 $actualSites = new SiteList();
137 $store = $this->newSiteStore( $actualSites );
138
139 $importer = new SiteImporter( $store );
140 $importer->importFromXML( $xml );
141
142 $this->assertEquals( $sites, $actualSites );
143 }
144
145 }