Merge "Revert "Log the reason why revision->getContent() returns null""
[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 use MediaWikiCoversValidator;
35
36 public function testConstructor_InvalidArgument() {
37 $this->setExpectedException( InvalidArgumentException::class );
38
39 new SiteExporter( 'Foo' );
40 }
41
42 public function testExportSites() {
43 $foo = Site::newForType( Site::TYPE_UNKNOWN );
44 $foo->setGlobalId( 'Foo' );
45
46 $acme = Site::newForType( Site::TYPE_UNKNOWN );
47 $acme->setGlobalId( 'acme.com' );
48 $acme->setGroup( 'Test' );
49 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
50 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
51
52 $tmp = tmpfile();
53 $exporter = new SiteExporter( $tmp );
54
55 $exporter->exportSites( [ $foo, $acme ] );
56
57 fseek( $tmp, 0 );
58 $xml = fread( $tmp, 16 * 1024 );
59
60 $this->assertContains( '<sites ', $xml );
61 $this->assertContains( '<site>', $xml );
62 $this->assertContains( '<globalid>Foo</globalid>', $xml );
63 $this->assertContains( '</site>', $xml );
64 $this->assertContains( '<globalid>acme.com</globalid>', $xml );
65 $this->assertContains( '<group>Test</group>', $xml );
66 $this->assertContains( '<localid type="interwiki">acme</localid>', $xml );
67 $this->assertContains( '<path type="link">http://acme.com/</path>', $xml );
68 $this->assertContains( '</sites>', $xml );
69
70 // NOTE: HHVM (at least on wmf Jenkins) doesn't like file URLs.
71 $xsdFile = __DIR__ . '/../../../../docs/sitelist-1.0.xsd';
72 $xsdData = file_get_contents( $xsdFile );
73
74 $document = new DOMDocument();
75 $document->loadXML( $xml, LIBXML_NONET );
76 $document->schemaValidateSource( $xsdData );
77 }
78
79 private function newSiteStore( SiteList $sites ) {
80 $store = $this->getMockBuilder( SiteStore::class )->getMock();
81
82 $store->expects( $this->once() )
83 ->method( 'saveSites' )
84 ->will( $this->returnCallback( function ( $moreSites ) use ( $sites ) {
85 foreach ( $moreSites as $site ) {
86 $sites->setSite( $site );
87 }
88 } ) );
89
90 $store->expects( $this->any() )
91 ->method( 'getSites' )
92 ->will( $this->returnValue( new SiteList() ) );
93
94 return $store;
95 }
96
97 public function provideRoundTrip() {
98 $foo = Site::newForType( Site::TYPE_UNKNOWN );
99 $foo->setGlobalId( 'Foo' );
100
101 $acme = Site::newForType( Site::TYPE_UNKNOWN );
102 $acme->setGlobalId( 'acme.com' );
103 $acme->setGroup( 'Test' );
104 $acme->addLocalId( Site::ID_INTERWIKI, 'acme' );
105 $acme->setPath( Site::PATH_LINK, 'http://acme.com/' );
106
107 $dewiki = Site::newForType( Site::TYPE_MEDIAWIKI );
108 $dewiki->setGlobalId( 'dewiki' );
109 $dewiki->setGroup( 'wikipedia' );
110 $dewiki->setForward( true );
111 $dewiki->addLocalId( Site::ID_INTERWIKI, 'wikipedia' );
112 $dewiki->addLocalId( Site::ID_EQUIVALENT, 'de' );
113 $dewiki->setPath( Site::PATH_LINK, 'http://de.wikipedia.org/w/' );
114 $dewiki->setPath( MediaWikiSite::PATH_PAGE, 'http://de.wikipedia.org/wiki/' );
115 $dewiki->setSource( 'meta.wikimedia.org' );
116
117 return [
118 'empty' => [
119 new SiteList()
120 ],
121
122 'some' => [
123 new SiteList( [ $foo, $acme, $dewiki ] ),
124 ],
125 ];
126 }
127
128 /**
129 * @dataProvider provideRoundTrip()
130 */
131 public function testRoundTrip( SiteList $sites ) {
132 $tmp = tmpfile();
133 $exporter = new SiteExporter( $tmp );
134
135 $exporter->exportSites( $sites );
136
137 fseek( $tmp, 0 );
138 $xml = fread( $tmp, 16 * 1024 );
139
140 $actualSites = new SiteList();
141 $store = $this->newSiteStore( $actualSites );
142
143 $importer = new SiteImporter( $store );
144 $importer->importFromXML( $xml );
145
146 $this->assertEquals( $sites, $actualSites );
147 }
148
149 }