Skip ImportTest if allow_url_fopen disabled
[lhc/web/wiklou.git] / tests / phpunit / includes / ImportTest.php
1 <?php
2
3 /**
4 * Test class for Import methods.
5 *
6 * @group Database
7 *
8 * @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
9 */
10 class ImportTest extends MediaWikiLangTestCase {
11
12 private function getInputStreamSource( $xml ) {
13 if ( ini_get( 'allow_url_fopen' ) != 1 ) {
14 $this->markTestSkipped( 'bug 73283: this test needs allow_url_fopen to be enabled' );
15 }
16 $file = 'data:application/xml,' . $xml;
17 $status = ImportStreamSource::newFromFile( $file );
18 if ( !$status->isGood() ) {
19 throw new MWException( "Cannot create InputStreamSource." );
20 }
21 return $status->value;
22 }
23
24 /**
25 * @covers WikiImporter::handlePage
26 * @dataProvider getRedirectXML
27 * @param string $xml
28 * @param string|null $redirectTitle
29 */
30 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
31 $source = $this->getInputStreamSource( $xml );
32
33 $redirect = null;
34 $callback = function ( Title $title, ForeignTitle $foreignTitle, $revCount,
35 $sRevCount, $pageInfo ) use ( &$redirect ) {
36 if ( array_key_exists( 'redirect', $pageInfo ) ) {
37 $redirect = $pageInfo['redirect'];
38 }
39 };
40
41 $importer = new WikiImporter( $source, ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
42 $importer->setPageOutCallback( $callback );
43 $importer->doImport();
44
45 $this->assertEquals( $redirectTitle, $redirect );
46 }
47
48 public function getRedirectXML() {
49 return array(
50 array(
51 <<< EOF
52 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
53 <page>
54 <title>Test</title>
55 <ns>0</ns>
56 <id>21</id>
57 <redirect title="Test22"/>
58 <revision>
59 <id>20</id>
60 <timestamp>2014-05-27T10:00:00Z</timestamp>
61 <contributor>
62 <username>Admin</username>
63 <id>10</id>
64 </contributor>
65 <comment>Admin moved page [[Test]] to [[Test22]]</comment>
66 <model>wikitext</model>
67 <format>text/x-wiki</format>
68 <text xml:space="preserve" bytes="20">#REDIRECT [[Test22]]</text>
69 <sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1>
70 </revision>
71 </page>
72 </mediawiki>
73 EOF
74 ,
75 'Test22'
76 ),
77 array(
78 <<< EOF
79 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.9/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.9/ http://www.mediawiki.org/xml/export-0.9.xsd" version="0.9" xml:lang="en">
80 <page>
81 <title>Test</title>
82 <ns>0</ns>
83 <id>42</id>
84 <revision>
85 <id>421</id>
86 <timestamp>2014-05-27T11:00:00Z</timestamp>
87 <contributor>
88 <username>Admin</username>
89 <id>10</id>
90 </contributor>
91 <text xml:space="preserve" bytes="4">Abcd</text>
92 <sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
93 <model>wikitext</model>
94 <format>text/x-wiki</format>
95 </revision>
96 </page>
97 </mediawiki>
98 EOF
99 ,
100 null
101 ),
102 );
103 }
104
105 /**
106 * @covers WikiImporter::handleSiteInfo
107 * @dataProvider getSiteInfoXML
108 * @param string $xml
109 * @param array|null $namespaces
110 */
111 public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
112 $source = $this->getInputStreamSource( $xml );
113
114 $importNamespaces = null;
115 $callback = function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
116 $importNamespaces = $siteinfo['_namespaces'];
117 };
118
119 $importer = new WikiImporter( $source, ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
120 $importer->setSiteInfoCallback( $callback );
121 $importer->doImport();
122
123 $this->assertEquals( $importNamespaces, $namespaces );
124 }
125
126 public function getSiteInfoXML() {
127 return array(
128 array(
129 <<< EOF
130 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
131 <siteinfo>
132 <namespaces>
133 <namespace key="-2" case="first-letter">Media</namespace>
134 <namespace key="-1" case="first-letter">Special</namespace>
135 <namespace key="0" case="first-letter" />
136 <namespace key="1" case="first-letter">Talk</namespace>
137 <namespace key="2" case="first-letter">User</namespace>
138 <namespace key="3" case="first-letter">User talk</namespace>
139 <namespace key="100" case="first-letter">Portal</namespace>
140 <namespace key="101" case="first-letter">Portal talk</namespace>
141 </namespaces>
142 </siteinfo>
143 </mediawiki>
144 EOF
145 ,
146 array(
147 '-2' => 'Media',
148 '-1' => 'Special',
149 '0' => '',
150 '1' => 'Talk',
151 '2' => 'User',
152 '3' => 'User talk',
153 '100' => 'Portal',
154 '101' => 'Portal talk',
155 )
156 ),
157 );
158 }
159
160 }