/** * Test {@link taoQtiTest_models_classes_QtiTestConverter::fromJson} * @dataProvider dataProvider * * @param string $testPath * @param string $json */ public function testFromJson($testPath, $json) { $doc = new XmlDocument('2.1'); $converter = new taoQtiTest_models_classes_QtiTestConverter($doc); $converter->fromJson($json); $result = preg_replace(array('/ {2,}/', '/<!--.*?-->|\\t|(?:\\r?\\n[ \\t]*)+/s'), array(' ', ''), $doc->saveToString()); $expected = preg_replace(array('/ {2,}/', '/<!--.*?-->|\\t|(?:\\r?\\n[ \\t]*)+/s'), array(' ', ''), file_get_contents($testPath)); $this->assertEquals($result, $expected); }
/** * @dataProvider qtiImplementationGuideAssessmentTestFiles * * @param string $uri The URI describing the file to load. */ public function testLoadSaveToStringSchemaValidate($uri) { $doc = new XmlDocument('2.1'); $doc->load($uri); $file = tempnam('/tmp', 'qsm'); $str = $doc->saveToString(); file_put_contents($file, $str); $doc = new XmlDocument('2.1'); try { $doc->load($file, true); // validate on load. $this->assertTrue(true); unlink($file); } catch (XmlStorageException $e) { $this->assertTrue(false, $e->getMessage()); unlink($file); } }
public function convert(item $item, array $questions) { // Make sure we clean up the log LogService::flush(); // Try to build the identifier using item `reference` // Otherwise, generate an alternative identifier and store the original reference as `label` $itemReference = $item->get_reference(); $itemIdentifier = Format::isIdentifier($itemReference, false) ? $itemReference : 'ITEM_' . StringUtil::generateRandomString(12); if ($itemReference !== $itemIdentifier) { LogService::log("The item `reference` ({$itemReference}) is not a valid identifier, thus can not be used for `assessmentItem` identifier. " . "Replaced it with randomly generated `{$itemIdentifier}` and stored the original `reference` as `label` attribute"); } $builder = new AssessmentItemBuilder(); $assessmentItem = $builder->build($itemIdentifier, $itemReference, $questions, $item->get_content()); $xml = new XmlDocument(); $xml->setDocumentComponent($assessmentItem); // Flush out all the error messages stored in this static class, also ensure they are unique $messages = array_values(array_unique(LogService::flush())); return [$xml->saveToString(true), $messages]; }
/** * Explode the rubric blocks of the test definition into separate QTI-XML files and * remove the compact XML document from the file system (useless for * the rest of the compilation process). * * @param XmlCompactDocument $compiledDoc */ protected function explodeRubricBlocks(XmlCompactDocument $compiledDoc) { $privateDir = $this->getPrivateDirectory(); $explodedRubricBlocks = $compiledDoc->explodeRubricBlocks(); foreach ($explodedRubricBlocks as $href => $rubricBlock) { $doc = new XmlDocument(); $doc->setDocumentComponent($rubricBlock); $data = $doc->saveToString(); $privateDir->write($href, $data); } }
/** * @dataProvider sharedStimulusConvertProvider */ public function testEmbedAssets($directory, $exception, $converted) { $xmlDocument = new XmlDocument(); $xmlDocument->load($directory . '/stimulus.xml'); try { $xmlConverted = SharedStimulusPackageImporter::embedAssets($directory . '/stimulus.xml'); $xmlDocument->load($xmlConverted); $strXml = $xmlDocument->saveToString(); $xmlDocument->load($converted); $convertStr = $xmlDocument->saveToString(); $this->assertEquals($convertStr, $strXml, __('Conversion return a wrong string')); } catch (\tao_models_classes_FileNotFoundException $e) { $this->assertNotNull($exception, __('It should not throw an exception')); if (!is_null($e)) { $this->assertInstanceOf(get_class($exception), $e, __('The exception class is wrong')); if ($exception->getMessage() !== '') { $this->assertEquals($exception->getMessage(), $e->getMessage(), __('The exception message is wrong')); } } } }
public function testUnknownClassWhileSavingBecauseOfVersion3() { $doc = new XmlDocument('2.2.0'); $doc->loadFromString(' <div> <bdo dir="rtl">I am reversed!</bdo> </div>'); // This should fail because in QTI 2.2.0 because <bdo> does not exist. $doc->setVersion('2.1.0'); $expectedMsg = "'bdo' components are not supported in QTI version '2.1.0'"; $this->setExpectedException('qtism\\data\\storage\\xml\\XmlStorageException', $expectedMsg, XmlStorageException::VERSION); $str = $doc->saveToString(true); }
/** * Save the content of test from a QTI Document * @param core_kernel_classes_Resource $test * @param qtism\data\storage\xml\XmlDocument $doc * @return boolean true if saved * @throws taoQtiTest_models_classes_QtiTestServiceException */ private function saveDoc(core_kernel_classes_Resource $test, XmlDocument $doc) { $file = $this->getQtiTestFile($test); return $file->update($doc->saveToString()); }