Skip to content

Commit

Permalink
fix: support older XmlDocument serialized data deserialization
Browse files Browse the repository at this point in the history
(cherry picked from commit 33f8ba8)
  • Loading branch information
wazelin committed Nov 15, 2022
1 parent 40a7e4f commit d66ca6a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
19 changes: 12 additions & 7 deletions qtism/common/dom/SerializableDomDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@

namespace qtism\common\dom;

use DOMAttr;
use DOMCDATASection;
use DOMComment;
use DOMDocument;
use DOMDocumentFragment;
use DOMDocumentType;
use DOMElement;
use DOMImplementation;
use DOMDocumentFragment;
use DOMComment;
use DOMCDATASection;
use DOMProcessingInstruction;
use DOMText;
use DOMAttr;
use DOMEntityReference;
use DOMImplementation;
use DOMNode;
use DOMNodeList;
use DOMProcessingInstruction;
use DOMText;
use Error;

/**
Expand Down Expand Up @@ -183,4 +183,9 @@ public function __unset(string $name): void
{
unset($this->dom->$name);
}

public function getDom(): DOMDocument
{
return $this->dom;
}
}
25 changes: 8 additions & 17 deletions qtism/data/storage/xml/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use InvalidArgumentException;
use LibXMLError;
use LogicException;
use qtism\common\dom\SerializableDomDocument;
use qtism\common\utils\Url;
use qtism\data\content\Flow;
use qtism\data\QtiComponent;
Expand Down Expand Up @@ -59,7 +60,7 @@ class XmlDocument extends QtiDocument
* The produced domDocument after a successful call to
* XmlDocument::load or XmlDocument::save.
*
* @var DOMDocument
* @var SerializableDomDocument
*/
private $domDocument = null;

Expand All @@ -78,27 +79,17 @@ public function __construct($version = '2.1', QtiComponent $documentComponent =
parent::__construct($version, $documentComponent);
}

public function __serialize(): array
{
return ['domDocument' => (string)$this];
}

public function __unserialize(array $data): void
{
$this->loadFromString($data['domDocument']);
}

public function __toString(): string
{
return $this->saveToString(false);
}

/**
* Set the DOMDocument object in use.
* Set the SerializableDomDocument object in use.
*
* @param DOMDocument $domDocument A DOMDocument object.
* @param SerializableDomDocument $domDocument A DOMDocument object.
*/
protected function setDomDocument(DOMDocument $domDocument)
protected function setDomDocument(SerializableDomDocument $domDocument)
{
$this->domDocument = $domDocument;
}
Expand All @@ -110,7 +101,7 @@ protected function setDomDocument(DOMDocument $domDocument)
*/
public function getDomDocument()
{
return $this->domDocument;
return $this->domDocument->getDom();
}

/**
Expand Down Expand Up @@ -152,7 +143,7 @@ public function loadFromString($string, $validate = false)
protected function loadImplementation($data, $validate = false, $fromString = false)
{
try {
$this->setDomDocument(new DOMDocument('1.0', 'UTF-8'));
$this->setDomDocument(new SerializableDomDocument('1.0', 'UTF-8'));
$this->getDomDocument()->preserveWhiteSpace = true;

// Disable xml warnings and errors and fetch error information as needed.
Expand Down Expand Up @@ -275,7 +266,7 @@ protected function saveImplementation($uri = '', $formatOutput = true)
$assessmentTest = $this->getDocumentComponent();

if (!empty($assessmentTest)) {
$this->setDomDocument(new DOMDocument('1.0', 'UTF-8'));
$this->setDomDocument(new SerializableDomDocument('1.0', 'UTF-8'));

if ($formatOutput == true) {
$this->getDomDocument()->formatOutput = true;
Expand Down
18 changes: 18 additions & 0 deletions test/qtismtest/data/storage/xml/XmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ public function testSerialization()
);
}

public function testOlderSerializedDataDeserialization()
{
if (PHP_VERSION_ID >= 80100 || PHP_VERSION_ID < 70400) {
$this->markTestSkipped('DOM objects serialization is impossible in PHP 8.1 or higher.');
}

$unserializedDoc = unserialize(file_get_contents(self::samplesDir(). 'serialized/xmldocument'));
$this->assertInstanceOf(XmlDocument::class, $unserializedDoc);

$doc = new XmlDocument('2.1');
$doc->loadFromString('<assessmentItemRef identifier="Q01" href="./Q01.xml"/>');

$this->assertEquals(
$doc->saveToString(),
$unserializedDoc->saveToString()
);
}

public function testLoadNoVersion()
{
$doc = new XmlDocument('2.1');
Expand Down
Binary file added test/samples/serialized/xmldocument
Binary file not shown.

0 comments on commit d66ca6a

Please sign in to comment.