* @package taoItems */ class XhtmlTestCase extends TestCase { public function testGetScriptElements() { $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'xhtml' . DIRECTORY_SEPARATOR . 'raw.html'; try { $dom = new DOMDocument('1.0', TAO_DEFAULT_ENCODING); if (@$dom->load($file)) { $this->assertEquals(2, count(taoItems_helpers_Xhtml::getScriptElements($dom, '/^jquery-/'))); $this->assertEquals(0, count(taoItems_helpers_Xhtml::getScriptElements($dom, '/^jQuery-/'))); } else { $this->assertTrue(false, "An error occured while loading '${file}'."); } } catch (DOMException $e) { $this->assertTrue(false, "An error occured while parsing '${file}'."); } } public function testHasScriptElements() { $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'xhtml' . DIRECTORY_SEPARATOR . 'raw.html'; try { $dom = new DOMDocument('1.0', TAO_DEFAULT_ENCODING); if (@$dom->load($file)) { $this->assertNotEquals(taoItems_helpers_Xhtml::getScriptElements($dom, '/^jquery-/'), false); $this->assertNotNull(taoItems_helpers_Xhtml::getScriptElements($dom, '/^jquery-/'), false); } else { $this->assertTrue(false, "An error occured while loading '${file}'."); } } catch (DOMException $e) { $this->assertTrue(false, "An error occured while parsing '${file}'."); } } public function testRemoveScriptElements() { $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'xhtml' . DIRECTORY_SEPARATOR . 'raw.html'; try { $dom = new DOMDocument('1.0', TAO_DEFAULT_ENCODING); if (@$dom->load($file)) { $this->assertEquals(2, taoItems_helpers_Xhtml::removeScriptElements($dom, '/^jquery-/')); $this->assertFalse(taoItems_helpers_Xhtml::hasScriptElements($dom, '/jquery/')); } else { $this->assertTrue(false, "An error occured while loading '${file}'."); } } catch (DOMException $e) { $this->assertTrue(false, "An error occured while parsing '${file}'."); } } public function testAddScriptElement() { $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'xhtml' . DIRECTORY_SEPARATOR . 'raw.html'; try { $dom = new DOMDocument('1.0', TAO_DEFAULT_ENCODING); if (@$dom->load($file)) { // -- Append taoItems_helpers_Xhtml::addScriptElement($dom, 'scripts/taoMatching.js'); $addedElements = taoItems_helpers_Xhtml::getScriptElements($dom, '/^taomatching.js/iu'); $this->assertEquals(1, count($addedElements)); $added = $addedElements[0]; // Was it really appended? $xpath = new DOMXPath($dom); $heads = $xpath->query('/html/head'); foreach ($heads as $head) { $this->assertTrue($head->lastChild === $added); break; } // -- Prepend taoItems_helpers_Xhtml::addScriptElement($dom, 'http://www.taotesting.com/scripts/wfapi.min.js', $append = false); $addedElements = taoItems_helpers_Xhtml::getScriptElements($dom, '/wfapi\.min/'); $this->assertEquals(1, count($addedElements)); $added = $addedElements[0]; // Was it really prepended? $xpath = new DOMXPath($dom); $heads = $xpath->query('/html/head'); foreach ($heads as $head) { $children = $head->getElementsByTagName('script'); $this->assertTrue($children->item(0) === $added); } } else { $this->assertTrue(false, "An error occured while loading '${file}'."); } } catch (DOMException $e) { $this->assertTrue(false, "An error occured while parsing '${file}'."); } } }