Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / ImportTest.php
blob9c224309bb2553ede165cf4c82346dcc86b6526a
1 <?php
3 /**
4 * Test class for Import methods.
6 * @group Database
8 * @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
9 */
10 class ImportTest extends MediaWikiLangTestCase {
12 private function getDataSource( $xml ) {
13 return new ImportStringSource( $xml );
16 /**
17 * @covers WikiImporter::handlePage
18 * @dataProvider getRedirectXML
19 * @param string $xml
20 * @param string|null $redirectTitle
22 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
23 $source = $this->getDataSource( $xml );
25 $redirect = null;
26 $callback = function ( Title $title, ForeignTitle $foreignTitle, $revCount,
27 $sRevCount, $pageInfo ) use ( &$redirect ) {
28 if ( array_key_exists( 'redirect', $pageInfo ) ) {
29 $redirect = $pageInfo['redirect'];
33 $importer = new WikiImporter(
34 $source,
35 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
37 $importer->setPageOutCallback( $callback );
38 $importer->doImport();
40 $this->assertEquals( $redirectTitle, $redirect );
43 public function getRedirectXML() {
44 // @codingStandardsIgnoreStart Generic.Files.LineLength
45 return array(
46 array(
47 <<< EOF
48 <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">
49 <page>
50 <title>Test</title>
51 <ns>0</ns>
52 <id>21</id>
53 <redirect title="Test22"/>
54 <revision>
55 <id>20</id>
56 <timestamp>2014-05-27T10:00:00Z</timestamp>
57 <contributor>
58 <username>Admin</username>
59 <id>10</id>
60 </contributor>
61 <comment>Admin moved page [[Test]] to [[Test22]]</comment>
62 <model>wikitext</model>
63 <format>text/x-wiki</format>
64 <text xml:space="preserve" bytes="20">#REDIRECT [[Test22]]</text>
65 <sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1>
66 </revision>
67 </page>
68 </mediawiki>
69 EOF
71 'Test22'
73 array(
74 <<< EOF
75 <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">
76 <page>
77 <title>Test</title>
78 <ns>0</ns>
79 <id>42</id>
80 <revision>
81 <id>421</id>
82 <timestamp>2014-05-27T11:00:00Z</timestamp>
83 <contributor>
84 <username>Admin</username>
85 <id>10</id>
86 </contributor>
87 <text xml:space="preserve" bytes="4">Abcd</text>
88 <sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
89 <model>wikitext</model>
90 <format>text/x-wiki</format>
91 </revision>
92 </page>
93 </mediawiki>
94 EOF
96 null
99 // @codingStandardsIgnoreEnd
103 * @covers WikiImporter::handleSiteInfo
104 * @dataProvider getSiteInfoXML
105 * @param string $xml
106 * @param array|null $namespaces
108 public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
109 $source = $this->getDataSource( $xml );
111 $importNamespaces = null;
112 $callback = function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
113 $importNamespaces = $siteinfo['_namespaces'];
116 $importer = new WikiImporter(
117 $source,
118 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
120 $importer->setSiteInfoCallback( $callback );
121 $importer->doImport();
123 $this->assertEquals( $importNamespaces, $namespaces );
126 public function getSiteInfoXML() {
127 // @codingStandardsIgnoreStart Generic.Files.LineLength
128 return array(
129 array(
130 <<< EOF
131 <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">
132 <siteinfo>
133 <namespaces>
134 <namespace key="-2" case="first-letter">Media</namespace>
135 <namespace key="-1" case="first-letter">Special</namespace>
136 <namespace key="0" case="first-letter" />
137 <namespace key="1" case="first-letter">Talk</namespace>
138 <namespace key="2" case="first-letter">User</namespace>
139 <namespace key="3" case="first-letter">User talk</namespace>
140 <namespace key="100" case="first-letter">Portal</namespace>
141 <namespace key="101" case="first-letter">Portal talk</namespace>
142 </namespaces>
143 </siteinfo>
144 </mediawiki>
147 array(
148 '-2' => 'Media',
149 '-1' => 'Special',
150 '0' => '',
151 '1' => 'Talk',
152 '2' => 'User',
153 '3' => 'User talk',
154 '100' => 'Portal',
155 '101' => 'Portal talk',
159 // @codingStandardsIgnoreEnd