Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion includes/Importers/WP/WXR_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function parse( $file ) {
}

// Attempt to use proper XML parsers first
if ( extension_loaded( 'simplexml' ) ) {
if ( $this->can_use_simplexml() ) {
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
$parser = new WXR_Parser_SimpleXML();
$this->logger->log( 'Using WXR_Parser_SimpleXML...', 'progress' );
$result = $parser->parse( $file );
Expand Down Expand Up @@ -92,4 +92,13 @@ public function parse( $file ) {

return $result;
}

/**
* Check whether the SimpleXML parser can run; it loads the file through DOMDocument.
*
* @return bool
*/
protected function can_use_simplexml() {
return extension_loaded( 'simplexml' ) && class_exists( 'DOMDocument' );
}
}
46 changes: 46 additions & 0 deletions tests/wxr-parser-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* WXR parser selection tests.
*
* @package templates-patterns-collection
*/

use TIOB\Importers\WP\WXR_Parser;
use TIOB\Importers\WP\WXR_Parser_SimpleXML;
use TIOB\Importers\WP\WXR_Parser_XML;

/**
* WXR parser on a host without the DOM extension.
*/
class WXR_Parser_Without_Dom extends WXR_Parser {
protected function can_use_simplexml(): bool {
return false;
}
}

/**
* Class WXR_Parser_Test
*/
class WXR_Parser_Test extends WP_UnitTestCase {
/**
* @var string
*/
private $file;

public function setUp(): void {
parent::setUp();
$this->file = __DIR__ . '/fixtures/export.xml';
}

public function test_parse_uses_simplexml_parser_when_dom_is_available(): void {
$expected = ( new WXR_Parser_SimpleXML() )->parse( $this->file );

$this->assertSame( $expected, ( new WXR_Parser( 'gutenberg' ) )->parse( $this->file ) );
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

public function test_parse_falls_back_to_xml_parser_when_dom_is_unavailable(): void {
$expected = ( new WXR_Parser_XML() )->parse( $this->file );

$this->assertSame( $expected, ( new WXR_Parser_Without_Dom( 'gutenberg' ) )->parse( $this->file ) );
}
}
Loading