Debian Codegraph @

tests/phpunit/includes/export/ExportTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Content\Content;
4 use MediaWiki\Content\ContentHandler;
5 use MediaWiki\Content\Renderer\ContentParseParams;
6 use MediaWiki\Content\TextContent;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Page\PageIdentity;
9 use MediaWiki\Parser\ParserOutput;
10
11 /**
12 * Test class for Export methods.
13 *
14 * @group Database
15 *
16 * @author Isaac Hutt <[email protected]>
17 */
18 class ExportTest extends MediaWikiLangTestCase {
19
20 protected function setUp(): void {
21 parent::setUp();
22 $this->overrideConfigValue( MainConfigNames::CapitalLinks, true );
23 }
24
25 /**
26 * @covers \WikiExporter::pageByTitle
27 */
28 public function testPageByTitle() {
29 $services = $this->getServiceContainer();
30
31 $page = $this->getExistingTestPage();
32
33 $xmlObject = $this->getXmlDumpForPage( $page );
34
35 /**
36 * Check namespaces match xml
37 */
38 foreach ( $xmlObject->siteinfo->namespaces->children() as $namespace ) {
39 // Get the text content of the SimpleXMLElement
40 $xmlNamespaces[] = (string)$namespace;
41 }
42 $xmlNamespaces = str_replace( ' ', '_', $xmlNamespaces );
43
44 $actualNamespaces = (array)$services->getContentLanguage()->getNamespaces();
45 $actualNamespaces = array_values( $actualNamespaces );
46 $this->assertEquals( $actualNamespaces, $xmlNamespaces );
47
48 // Check xml page title correct
49 $xmlTitle = (array)$xmlObject->page->title;
50 $this->assertEquals( $page->getTitle()->getPrefixedText(), $xmlTitle[0] );
51
52 // Check xml page text is not empty
53 $text = (array)$xmlObject->page->revision->text;
54 $this->assertNotEquals( '', $text[0] );
55 }
56
57 /**
58 * Regression test for T328503 to verify that custom content types
59 * with a getNativeData() override that returns a non-string value export correctly.
60 *
61 * @covers \XmlDumpWriter::writeText
62 */
63 public function testShouldExportContentWithNonStringNativeData(): void {
64 // Make a mock ContentHandler for a Content that has a getNativeData() method
65 // with a non-string return value.
66 $contentModelId = 'non-string-test-content-model';
67 $contentHandler = new class( $contentModelId ) extends ContentHandler {
68
69 public function __construct( $contentModelId ) {
70 parent::__construct(
71 $contentModelId,
72 [ CONTENT_FORMAT_TEXT ]
73 );
74 }
75
76 public function serializeContent( Content $content, $format = null ) {
77 return json_encode( $content->getNativeData() );
78 }
79
80 public function unserializeContent( $blob, $format = null ) {
81 return $this->getTestContent( $blob );
82 }
83
84 public function makeEmptyContent() {
85 return $this->getTestContent( '{}' );
86 }
87
88 protected function fillParserOutput(
89 Content $content,
90 ContentParseParams $cpoParams,
91 ParserOutput &$output
92 ) {
93 $output->setRawText( json_encode( $content->getNativeData() ) );
94 }
95
96 private function getTestContent( string $blob ): Content {
97 return new class( $blob, $this->getModelID() ) extends TextContent {
98 /** @var array */
99 private $data;
100
101 public function __construct( $text, $contentModelId ) {
102 parent::__construct(
103 $text,
104 $contentModelId
105 );
106
107 $this->data = json_decode( $text, true );
108 }
109
110 public function getNativeData() {
111 return $this->data;
112 }
113 };
114 }
115 };
116
117 $this->setTemporaryHook(
118 'ContentHandlerForModelID',
119 static function (
120 string $modelId,
121 ?ContentHandler &$handlerRef
122 ) use ( $contentModelId, $contentHandler ): void {
123 if ( $modelId === $contentModelId ) {
124 $handlerRef = $contentHandler;
125 }
126 }
127 );
128
129 $wikiPage = $this->getNonexistingTestPage( 'NonStringNativeDataExportTest' );
130
131 $testText = json_encode( [ 'test' => 'data' ] );
132 $content = $contentHandler->unserializeContent( $testText );
133
134 $this->editPage( $wikiPage, $content );
135
136 $xmlObject = $this->getXmlDumpForPage( $wikiPage );
137
138 $this->assertSame( $contentModelId, (string)$xmlObject->page->revision->model );
139 $this->assertSame( $testText, (string)$xmlObject->page->revision->text );
140 }
141
142 /**
143 * Convenience function to export the content of the given page in MediaWiki's XML dump format.
144 * @param PageIdentity $page page to export
145 * @return SimpleXMLElement root element of the generated XML
146 */
147 private function getXmlDumpForPage( PageIdentity $page ): SimpleXMLElement {
148 $exporter = $this->getServiceContainer()
149 ->getWikiExporterFactory()
150 ->getWikiExporter( $this->getDb(), WikiExporter::FULL );
151
152 $sink = new DumpStringOutput();
153 $exporter->setOutputSink( $sink );
154 $exporter->openStream();
155 $exporter->pageByTitle( $page );
156 $exporter->closeStream();
157
158 // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
159 $oldDisable = @libxml_disable_entity_loader( true );
160
161 // This throws error if invalid xml output
162 $xmlObject = simplexml_load_string( $sink );
163
164 // phpcs:ignore Generic.PHP.NoSilencedErrors
165 @libxml_disable_entity_loader( $oldDisable );
166
167 return $xmlObject;
168 }
169
170 }