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