Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / import / ImportTest.php
blob53d91c65936a108470b476d5f67fcde09c1f65ff
1 <?php
2 use MediaWiki\MediaWikiServices;
4 /**
5 * Test class for Import methods.
7 * @group Database
9 * @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
11 class ImportTest extends MediaWikiLangTestCase {
13 private function getDataSource( $xml ) {
14 return new ImportStringSource( $xml );
17 /**
18 * @covers WikiImporter
19 * @dataProvider getUnknownTagsXML
20 * @param string $xml
21 * @param string $text
22 * @param string $title
24 public function testUnknownXMLTags( $xml, $text, $title ) {
25 $source = $this->getDataSource( $xml );
27 $importer = new WikiImporter(
28 $source,
29 MediaWikiServices::getInstance()->getMainConfig()
32 $importer->doImport();
33 $title = Title::newFromText( $title );
34 $this->assertTrue( $title->exists() );
36 $this->assertEquals( WikiPage::factory( $title )->getContent()->getNativeData(), $text );
39 public function getUnknownTagsXML() {
40 // @codingStandardsIgnoreStart Generic.Files.LineLength
41 return [
43 <<< EOF
44 <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">
45 <page unknown="123" dontknow="533">
46 <title>TestImportPage</title>
47 <unknowntag>Should be ignored</unknowntag>
48 <ns>0</ns>
49 <id unknown="123" dontknow="533">14</id>
50 <revision>
51 <id unknown="123" dontknow="533">15</id>
52 <unknowntag>Should be ignored</unknowntag>
53 <timestamp>2016-01-03T11:18:43Z</timestamp>
54 <contributor>
55 <unknowntag>Should be ignored</unknowntag>
56 <username unknown="123" dontknow="533">Admin</username>
57 <id>1</id>
58 </contributor>
59 <model>wikitext</model>
60 <format>text/x-wiki</format>
61 <text xml:space="preserve" bytes="0">noitazinagro tseb eht si ikiWaideM</text>
62 <sha1>phoiac9h4m842xq45sp7s6u21eteeq1</sha1>
63 <unknowntag>Should be ignored</unknowntag>
64 </revision>
65 </page>
66 <unknowntag>Should be ignored</unknowntag>
67 </mediawiki>
68 EOF
70 'noitazinagro tseb eht si ikiWaideM',
71 'TestImportPage'
74 // @codingStandardsIgnoreEnd
77 /**
78 * @covers WikiImporter::handlePage
79 * @dataProvider getRedirectXML
80 * @param string $xml
81 * @param string|null $redirectTitle
83 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
84 $source = $this->getDataSource( $xml );
86 $redirect = null;
87 $callback = function ( Title $title, ForeignTitle $foreignTitle, $revCount,
88 $sRevCount, $pageInfo ) use ( &$redirect ) {
89 if ( array_key_exists( 'redirect', $pageInfo ) ) {
90 $redirect = $pageInfo['redirect'];
94 $importer = new WikiImporter(
95 $source,
96 MediaWikiServices::getInstance()->getMainConfig()
98 $importer->setPageOutCallback( $callback );
99 $importer->doImport();
101 $this->assertEquals( $redirectTitle, $redirect );
104 public function getRedirectXML() {
105 // @codingStandardsIgnoreStart Generic.Files.LineLength
106 return [
108 <<< EOF
109 <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">
110 <page>
111 <title>Test</title>
112 <ns>0</ns>
113 <id>21</id>
114 <redirect title="Test22"/>
115 <revision>
116 <id>20</id>
117 <timestamp>2014-05-27T10:00:00Z</timestamp>
118 <contributor>
119 <username>Admin</username>
120 <id>10</id>
121 </contributor>
122 <comment>Admin moved page [[Test]] to [[Test22]]</comment>
123 <model>wikitext</model>
124 <format>text/x-wiki</format>
125 <text xml:space="preserve" bytes="20">#REDIRECT [[Test22]]</text>
126 <sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1>
127 </revision>
128 </page>
129 </mediawiki>
132 'Test22'
135 <<< EOF
136 <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">
137 <page>
138 <title>Test</title>
139 <ns>0</ns>
140 <id>42</id>
141 <revision>
142 <id>421</id>
143 <timestamp>2014-05-27T11:00:00Z</timestamp>
144 <contributor>
145 <username>Admin</username>
146 <id>10</id>
147 </contributor>
148 <text xml:space="preserve" bytes="4">Abcd</text>
149 <sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
150 <model>wikitext</model>
151 <format>text/x-wiki</format>
152 </revision>
153 </page>
154 </mediawiki>
157 null
160 // @codingStandardsIgnoreEnd
164 * @covers WikiImporter::handleSiteInfo
165 * @dataProvider getSiteInfoXML
166 * @param string $xml
167 * @param array|null $namespaces
169 public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
170 $source = $this->getDataSource( $xml );
172 $importNamespaces = null;
173 $callback = function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
174 $importNamespaces = $siteinfo['_namespaces'];
177 $importer = new WikiImporter(
178 $source,
179 MediaWikiServices::getInstance()->getMainConfig()
181 $importer->setSiteInfoCallback( $callback );
182 $importer->doImport();
184 $this->assertEquals( $importNamespaces, $namespaces );
187 public function getSiteInfoXML() {
188 // @codingStandardsIgnoreStart Generic.Files.LineLength
189 return [
191 <<< EOF
192 <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">
193 <siteinfo>
194 <namespaces>
195 <namespace key="-2" case="first-letter">Media</namespace>
196 <namespace key="-1" case="first-letter">Special</namespace>
197 <namespace key="0" case="first-letter" />
198 <namespace key="1" case="first-letter">Talk</namespace>
199 <namespace key="2" case="first-letter">User</namespace>
200 <namespace key="3" case="first-letter">User talk</namespace>
201 <namespace key="100" case="first-letter">Portal</namespace>
202 <namespace key="101" case="first-letter">Portal talk</namespace>
203 </namespaces>
204 </siteinfo>
205 </mediawiki>
209 '-2' => 'Media',
210 '-1' => 'Special',
211 '0' => '',
212 '1' => 'Talk',
213 '2' => 'User',
214 '3' => 'User talk',
215 '100' => 'Portal',
216 '101' => 'Portal talk',
220 // @codingStandardsIgnoreEnd