Skip to content

Commit

Permalink
Merge pull request #345 from oat-sa/feature/serializable-xml-document…
Browse files Browse the repository at this point in the history
…-legacy

[legacy] Feature Serializable XML Document
  • Loading branch information
wazelin authored Nov 15, 2022
2 parents aa9cba8 + d66ca6a commit e141ca1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 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;
}
}
26 changes: 16 additions & 10 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,12 +79,17 @@ public function __construct($version = '2.1', QtiComponent $documentComponent =
parent::__construct($version, $documentComponent);
}

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 @@ -95,7 +101,7 @@ protected function setDomDocument(DOMDocument $domDocument)
*/
public function getDomDocument()
{
return $this->domDocument;
return $this->domDocument->getDom();
}

/**
Expand Down Expand Up @@ -137,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 @@ -260,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 Expand Up @@ -329,14 +335,14 @@ public function schemaValidate($filename = '')
}

$doc = $this->getDomDocument();

$oldErrorConfig = libxml_use_internal_errors(true);
$valid = $doc->schemaValidate($filename);

$libXmlErrors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($oldErrorConfig);

if (!$valid) {
$formattedErrors = self::formatLibXmlErrors($libXmlErrors);

Expand All @@ -345,7 +351,7 @@ public function schemaValidate($filename = '')
realpath($filename),
$formattedErrors
);

throw XmlStorageException::createValidationException($msg, $libXmlErrors);
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/qtismtest/data/storage/xml/XmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,35 @@ public function testLoadFromMalformedString()
$doc->loadFromString('<assessmentItem>');
}

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

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

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 e141ca1

Please sign in to comment.