public function testAppendStylesheetWithInvalidItem() { $sampleSrc = dirname(__FILE__) . '/samples/invalid.xml'; $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->load($sampleSrc); // Assert there are some stylesheets (at the wrong place here). $this->assertEquals(2, $doc->documentElement->getElementsByTagName('stylesheet')->length); // Should be a child of assessmentItem (that's bad)... $this->assertEquals('assessmentItem', $doc->documentElement->getElementsByTagName('stylesheet')->item(0)->parentNode->tagName); Utils::appendStylesheet($doc, 'proot.css'); // Only 1 stylesheet should be found, as a previous sibling of itemBody. $this->assertEquals(1, $doc->documentElement->getElementsByTagName('stylesheet')->length); $stylesheet = $doc->documentElement->getElementsByTagName('stylesheet')->item(0); $this->assertEquals('proot.css', $stylesheet->getAttribute('href')); $this->assertEquals('all', $stylesheet->getAttribute('media')); $this->assertEquals('text/css', $stylesheet->getAttribute('type')); $this->assertEquals('', $stylesheet->getAttribute('title')); }
private static function migrateFrom09To091() { // Get all items... $itemService = \taoItems_models_classes_ItemsService::singleton(); $itemClass = $itemService->getRootClass(); foreach ($itemClass->getInstances(true) as $item) { if ($itemService->hasItemModel($item, array(TAO_ITEM_MODEL_QTI))) { $path = $itemService->getDefaultItemFolder($item); $qtiXml = $itemService->getItemContent($item); if (empty($qtiXml) === false) { $qtiDom = new \DOMDocument('1.0', 'UTF-8'); $qtiDom->loadXML($qtiXml); // Get all stylesheet hrefs. $hrefs = Utils::getStylesheetHrefs($qtiDom); // Make sure the hrefs are refering existing files. for ($i = 0; $i < count($hrefs); $i++) { $href = $hrefs[$i]; if (is_readable($path . $href) === false) { \common_Logger::i("The stylesheet->href '{$path}.{$href}' does not reference an existing file. Trying to repair..."); // Let's try with another name... $pathinfo = pathinfo($href); $altFileName = \tao_helpers_File::getSafeFileName($pathinfo['basename']); $dirSep = $pathinfo['dirname'] !== '.' ? $pathInfo['dirname'] . DIRECTORY_SEPARATOR : ''; $altPath = $path . $dirSep . $altFileName; if (is_readable($altPath)) { // Bingo! We rebind. $hrefs[$i] = $dirSep . $altFileName; \common_Logger::i("Repaired with new href '{$dirSep}.{$altFileName}}'."); } else { // It's definitely broken... unset($hrefs[$i]); \common_Logger::i("Could not be repaired! QTI stylesheet component removed from item."); } } } // Reput them in the item with cleanup enabled // to solve the XMLSchema validation issue. if (count($hrefs) > 0) { $href = array_shift($hrefs); Utils::appendStylesheet($qtiDom, $href, true); } // Append the rest of the stylesheets. foreach ($hrefs as $href) { Utils::appendStylesheet($qtiDom, $href); } $itemService->setItemContent($item, $qtiDom->saveXML()); } } } }
/** * @param $xml * @param $cssName * @return mixed */ protected function applyToXml($xml, $cssName) { $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->loadXML($xml); Utils::appendStylesheet($doc, $cssName); return $doc->saveXML(); }