/** * @dataProvider validInferQTIVersionProvider */ public function testInferQTIVersionValid($file, $expectedVersion) { $dom = new DOMDocument('1.0', 'UTF-8'); $dom->load($file); $this->assertEquals($expectedVersion, Utils::inferQTIVersion($dom)); }
protected function loadImplementation($data, $validate = false, $fromString = false) { try { $this->setDomDocument(new DOMDocument('1.0', 'UTF-8')); $this->getDomDocument()->preserveWhiteSpace = false; // disable xml warnings and errors and fetch error information as needed. $oldErrorConfig = libxml_use_internal_errors(true); $loadMethod = $fromString === true ? 'loadXML' : 'load'; $doc = $this->getDomDocument(); if (call_user_func_array(array($doc, $loadMethod), array($data, LIBXML_COMPACT | LIBXML_NONET | LIBXML_XINCLUDE))) { // Infer the QTI version. if (($version = XmlUtils::inferQTIVersion($this->getDomDocument())) !== false) { $this->setVersion($version); } else { $msg = "Cannot infer QTI version. Is it well formed?"; throw new XmlStorageException($msg); } if ($validate === true) { $this->schemaValidate(); } try { // Get the root element and unmarshall. $element = $this->getDomDocument()->documentElement; $factory = $this->createMarshallerFactory(); $marshaller = $factory->createMarshaller($element); $this->setDocumentComponent($marshaller->unmarshall($element)); } catch (UnmarshallingException $e) { $line = $e->getDOMElement()->getLineNo(); $msg = "An error occured while processing QTI-XML at line {$line}."; throw new XmlStorageException($msg, $e); } catch (RuntimeException $e) { $msg = "Unmarshallable element '" . $element->localName . "' in QTI-XML."; throw new XmlStorageException($msg, $e); } } else { $libXmlErrors = libxml_get_errors(); $formattedErrors = self::formatLibXmlErrors($libXmlErrors); libxml_clear_errors(); libxml_use_internal_errors($oldErrorConfig); $msg = "An internal occured while parsing QTI-XML:\n{$formattedErrors}"; throw new XmlStorageException($msg, null, new LibXmlErrorCollection($libXmlErrors)); } } catch (DOMException $e) { $line = $e->getLine(); $msg = "An error occured while parsing QTI-XML at line {$line}."; throw new XmlStorageException($msg, $e); } }