1
0
mirror of https://github.com/php/php-src.git synced 2026-04-25 17:08:14 +02:00

DOM tests: checked on PHP 5.2.6, 5.3 and 6.0 (Windows, Linux and Linux 64 bit).

This commit is contained in:
Ant Phillips
2008-12-01 11:29:35 +00:00
parent e073d72618
commit 72e8a2ec60
26 changed files with 948 additions and 0 deletions
@@ -0,0 +1,35 @@
--TEST--
DOMCharacterData::appendData basic functionality test
--CREDITS--
Mike Sullivan <mike@regexia.com>
#TestFest 2008 (London)
--FILE--
<?php
$document = new DOMDocument;
$root = $document->createElement('root');
$document->appendChild($root);
$cdata = $document->createElement('cdata');
$root->appendChild($cdata);
$cdatanode = $document->createCDATASection('');
$cdata->appendChild($cdatanode);
$cdatanode->appendData('data');
echo "CDATA Length (one append): " . $cdatanode->length . "\n";
$cdatanode->appendData('><&"');
echo "CDATA Length (two appends): " . $cdatanode->length . "\n";
echo "CDATA Content: " . $cdatanode->data . "\n";
echo "\n" . $document->saveXML();
?>
--EXPECT--
CDATA Length (one append): 4
CDATA Length (two appends): 8
CDATA Content: data><&"
<?xml version="1.0"?>
<root><cdata><![CDATA[data><&"]]></cdata></root>
@@ -0,0 +1,21 @@
--TEST--
Test adding data to a DOMComment
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
$comment->appendData('-more-data');
$dom->appendChild($comment);
$dom->saveXML();
echo $dom->saveXML();
?>
--EXPECTF--
<?xml version="1.0"?>
<!--test-comment-more-data-->
@@ -0,0 +1,35 @@
--TEST--
DOMComment::appendData basic functionality test
--CREDITS--
Mike Sullivan <mike@regexia.com>
#TestFest 2008 (London)
--FILE--
<?php
$document = new DOMDocument;
$root = $document->createElement('root');
$document->appendChild($root);
$comment = $document->createElement('comment');
$root->appendChild($comment);
$commentnode = $document->createComment('');
$comment->appendChild($commentnode);
$commentnode->appendData('data');
echo "Comment Length (one append): " . $commentnode->length . "\n";
$commentnode->appendData('><&"');
echo "Comment Length (two appends): " . $commentnode->length . "\n";
echo "Comment Content: " . $commentnode->data . "\n";
echo "\n" . $document->saveXML();
?>
--EXPECT--
Comment Length (one append): 4
Comment Length (two appends): 8
Comment Content: data><&"
<?xml version="1.0"?>
<root><comment><!--data><&"--></comment></root>
@@ -0,0 +1,21 @@
--TEST--
Test inserting data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
//correct offset
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
$comment->insertData(4,'-inserted');
$dom->appendChild($comment);
echo $dom->saveXML();
?>
--EXPECTF--
<?xml version="1.0"?>
<!--test-inserted-comment-->
@@ -0,0 +1,24 @@
--TEST--
Test inserting data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
//Negative offset
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
try {
$comment->insertData(-1,'-inserted');
} catch (DOMException $e ) {
if ($e->getMessage() == 'Index Size Error'){
echo "Throws DOMException for -ve offset\n";
}
}
?>
--EXPECTF--
Throws DOMException for -ve offset
@@ -0,0 +1,24 @@
--TEST--
Test inserting data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
//offset to large
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
try {
$comment->insertData(999,'-inserted');
} catch (DOMException $e ) {
if ($e->getMessage() == 'Index Size Error'){
echo "Throws DOMException for offset too large\n";
}
}
?>
--EXPECTF--
Throws DOMException for offset too large
@@ -0,0 +1,29 @@
--TEST--
Test replacing data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php // require_once('skipif.inc'); ?>
--FILE--
<?php
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
$comment->replaceData(4,1,'replaced');
$dom->appendChild($comment);
echo $dom->saveXML();
// Replaces rest of string if count is greater than length of existing string
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
$comment->replaceData(0,50,'replaced');
$dom->appendChild($comment);
echo $dom->saveXML();
?>
--EXPECTF--
<?xml version="1.0"?>
<!--testreplacedcomment-->
<?xml version="1.0"?>
<!--replaced-->
@@ -0,0 +1,24 @@
--TEST--
Test replacing data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
//Negative offset
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
try {
$comment->replaceData(-1,4,'-inserted');
} catch (DOMException $e ) {
if ($e->getMessage() == 'Index Size Error'){
echo "Throws DOMException for -ve offest\n";
}
}
?>
--EXPECTF--
Throws DOMException for -ve offest
@@ -0,0 +1,24 @@
--TEST--
Test replacing data into a DOMComment basic test
--CREDITS--
Andrew Larssen <al@larssen.org>
London TestFest 2008
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
//offset to large
$dom = new DomDocument();
$comment = $dom->createComment('test-comment');
try {
$comment->replaceData(999,4,'-inserted');
} catch (DOMException $e ) {
if ($e->getMessage() == 'Index Size Error'){
echo "Throws DOMException for offest too large\n";
}
}
?>
--EXPECTF--
Throws DOMException for offest too large
@@ -0,0 +1,21 @@
--TEST--
Testing DOMDocumentFragment::appendXML and DOMDocumentFragment::hasChildNodes
--FILE--
<?php
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
if ($fragment->hasChildNodes()) {
echo "has child nodes\n";
} else {
echo "has no child nodes\n";
}
$fragment->appendXML('<foo>bar</foo>');
if ($fragment->hasChildNodes()) {
echo "has child nodes\n";
} else {
echo "has no child nodes\n";
}
--EXPECT--
has no child nodes
has child nodes
@@ -0,0 +1,24 @@
--TEST--
DomDocument::createAttribute() - basic test for DomDocument::createAttribute()
--CREDITS--
Muhammad Khalid Adnan
# TestFest 2008
--FILE--
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
// A pass case.
$test_attribute = $doc->createAttribute("hahaha");
$node->appendChild($test_attribute);
echo $doc->saveXML();
?>
--EXPECT--
<?xml version="1.0"?>
<para hahaha=""/>
@@ -0,0 +1,25 @@
--TEST--
Test DOMDocument::createAttribute() for expected expection thrown when wrong parameter passed
--FILE--
<?php
$dom = new DOMDocument();
try {
$attr = $dom->createAttribute(0);
}
catch(DOMException $e) {
$code = $e->getCode();
if(DOM_INVALID_CHARACTER_ERR === $code) {
echo "PASS";
}
else {
echo 'Wrong exception code';
}
}
catch(Exception $e) {
echo 'Wrong exception thrown';
}
?>
--EXPECTF--
PASS
@@ -0,0 +1,27 @@
--TEST--
DomDocument::createAttribute() - error test for DomDocument::createAttribute()
--CREDITS--
Muhammad Khalid Adnan
# TestFest 2008
--FILE--
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
try {
$failed_test_attribute = $doc->createAttribute("ha haha");
$node->appendChild($failed_test_attribute);
echo $doc->saveXML();
}
catch (DOMException $e) {
echo 'Test failed!', PHP_EOL;
}
?>
--EXPECT--
Test failed!
@@ -0,0 +1,12 @@
--TEST--
Test DOMDocument::createAttribute() for expected return value
--FILE--
<?php
$dom = new DOMDocument();
$attr = $dom->createAttribute('string');
echo get_class($attr);
?>
--EXPECTF--
DOMAttr
@@ -0,0 +1,28 @@
--TEST--
DomDocument::createProcessingInstruction() - basic test for DomDocument::createProcessingInstruction()
--CREDITS--
Muhammad Khalid Adnan
# TestFest 2008
--FILE--
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
$test_proc_inst0 =
$doc->createProcessingInstruction( "blablabla" );
$node->appendChild($test_proc_inst0);
$test_proc_inst1 =
$doc->createProcessingInstruction( "blablabla", "datadata" );
$node->appendChild($test_proc_inst1);
echo $doc->saveXML();
?>
--EXPECT--
<?xml version="1.0"?>
<para><?blablabla?><?blablabla datadata?></para>
@@ -0,0 +1,29 @@
--TEST--
DomDocument::createProcessingInstruction() - error test for DomDocument::createProcessingInstruction()
--CREDITS--
Muhammad Khalid Adnan
# TestFest 2008
--FILE--
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
try {
$test_proc_inst =
$doc->createProcessingInstruction( "bla bla bla" );
$node->appendChild($test_proc_inst);
echo $doc->saveXML();
}
catch (DOMException $e)
{
echo 'Test failed!', PHP_EOL;
}
?>
--EXPECT--
Test failed!
@@ -0,0 +1,18 @@
--TEST--
DOMDocument::loadHTML
--CREDITS--
Frank Cassedanne franck@ouarz.net
#London TestFest 2008
--SKIPIF--
<?php
require_once('skipif.inc');
?>
--FILE--
<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body><p>Test<br></p></body></html>");
echo $doc->saveHTML();
?>
--EXPECTF--
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Test<br></p></body></html>
+33
View File
@@ -0,0 +1,33 @@
--TEST--
DOMDocument::save Test basic function of save method
--SKIPIF--
<?php
require_once('skipif.inc');
?>
--FILE--
<?php
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
$temp_filename = dirname(__FILE__)."/DomDocument_save_basic.tmp";
echo 'Wrote: ' . $doc->save($temp_filename) . ' bytes'; // Wrote: 72 bytes
?>
--CLEAN--
<?php
$temp_filename = dirname(__FILE__)."/DomDocument_save_basic.tmp";
unlink($temp_filename);
?>
--EXPECTF--
Wrote: 72 bytes
@@ -0,0 +1,49 @@
--TEST--
DOMNode: hasAttributes()
--CREDITS--
James Lewis <james@s-1.com>
#TestFest 2008
--FILE--
<?php
require_once("dom_test.inc");
$dom = new DOMDocument;
$dom->loadXML($xmlstr);
if(!$dom) {
echo "Error while parsing the document\n";
exit;
}
$element = $dom->documentElement;
echo "Verify that we have a DOMElement object:\n";
var_dump($element);
echo "\nElement should have attributes:\n";
var_dump($element->hasAttributes());
$nodelist=$dom->getElementsByTagName('tbody') ;
$element = $nodelist->item(0);
echo "\nVerify that we have a DOMElement object:\n";
var_dump($element);
echo "\nElement should have no attributes:\n";
var_dump($element->hasAttributes())
?>
--EXPECTF--
Verify that we have a DOMElement object:
object(DOMElement)#%d (0) {
}
Element should have attributes:
bool(true)
Verify that we have a DOMElement object:
object(DOMElement)#%d (0) {
}
Element should have no attributes:
bool(false)
+111
View File
@@ -0,0 +1,111 @@
--TEST--
DOM cloneNode : Basic Functionality
--SKIPIF--
<?php
require_once('skipif.inc');
?>
--CREDITS--
Simon Hughes <odbc3@hotmail.com>
--FILE--
<?php
$xml = <<< EOXML
<?xml version="1.0" encoding="ISO-8859-1"?>
<courses>
<course title="one">
<notes>
<note>c1n1</note>
<note>c1n2</note>
</notes>
</course>
<course title="two">
<notes>
<note>c2n1</note>
<note>c2n2</note>
</notes>
</course>
</courses>
EOXML;
function dumpcourse($current) {
$title = ($current->nodeType != XML_TEXT_NODE && $current->hasAttribute('title')) ? $current->getAttribute('title'):"no title";
echo "Course: $title:";var_dump($current);
echo "~";var_dump($current->textContent);
}
$dom = new DOMDocument();
$dom->loadXML($xml);
$root = $dom->documentElement;
// strip all text nodes from this tree
$children = $root->childNodes;
$len = $children->length;
for ($index = $children->length - 1; $index >=0; $index--) {
$current = $children->item($index);
if ($current->nodeType == XML_TEXT_NODE) {
$noderemoved = $root->removeChild($current);
}
}
echo "Start cloneNode test\n";
$first_course = $children->item(0);
$cloned_first_course_default = $first_course->cloneNode();
$first_course->setAttribute('title', 'new title1');
$cloned_first_course_true = $first_course->cloneNode(true);
$first_course->setAttribute('title', 'new title2');
$cloned_first_course_false = $first_course->cloneNode(false);
$first_course->setAttribute('title', 'new title3');
$cloned_first_course_default->setAttribute('title', 'new title default');
$cloned_first_course_true->setAttribute('title', 'new title true');
$cloned_first_course_false->setAttribute('title', 'new title false');
$root->appendChild($cloned_first_course_default);
$root->appendChild($cloned_first_course_true);
$root->appendChild($cloned_first_course_false);
$children = $root->childNodes;
for ($index = 0; $index < $children->length; $index++) {
echo "node $index\n";
dumpcourse($children->item($index));
}
--EXPECTF--
Start cloneNode test
node 0
Course: new title3:object(DOMElement)#6 (0) {
}
~string(24) "
c1n1
c1n2
"
node 1
Course: two:object(DOMElement)#3 (0) {
}
~string(24) "
c2n1
c2n2
"
node 2
Course: new title default:object(DOMElement)#4 (0) {
}
~string(0) ""
node 3
Course: new title true:object(DOMElement)#7 (0) {
}
~string(24) "
c1n1
c1n2
"
node 4
Course: new title false:object(DOMElement)#8 (0) {
}
~string(0) ""
@@ -0,0 +1,43 @@
--TEST--
Test whether a node has child nodes: hasChildNodes()
--SKIPIF--
<?php
include('skipif.inc');
?>
--FILE--
<?php
/* Create an XML document
* with strcuture
* <book>
* <title>This is the title</title>
* </book>
* Check for child nodes of the <book>, <title> and This is the title
*
*/
$doc = new DOMDocument();
$root = $doc->createElement('book');
$doc->appendChild($root);
$title = $doc->createElement('title');
$root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$title->appendChild($text);
echo "Root has child nodes: ";
var_dump($root->hasChildNodes());
echo "Title has child nodes: ";
var_dump($title->hasChildNodes());
echo "Text has child nodes: ";
var_dump($text->hasChildNodes());
?>
--EXPECTF--
Root has child nodes: bool(true)
Title has child nodes: bool(true)
Text has child nodes: bool(false)
@@ -0,0 +1,35 @@
--TEST--
DOMNode: isSameNode()
--CREDITS--
James Lewis <james@s-1.com>
#TestFest 2008
--FILE--
<?php
require_once("dom_test.inc");
$dom = new DOMDocument;
$dom->loadXML($xmlstr);
if(!$dom) {
echo "Error while parsing the document\n";
exit;
}
$node = $dom->documentElement;
if($node->isSameNode($node))
echo "EXPECTING SAME NODE, PASSED\n" ;
else
echo "EXPECTING SAME NODE, FAILED\n" ;
$nodelist=$dom->getElementsByTagName('tbody') ;
if($nodelist->item(0)->isSameNode($node))
echo "EXPECTING NOT SAME NODE, FAILED\n" ;
else
echo "EXPECTING NOT SAME NODE, PASSED\n" ;
?>
===DONE===
--EXPECT--
EXPECTING SAME NODE, PASSED
EXPECTING NOT SAME NODE, PASSED
===DONE===
@@ -0,0 +1,64 @@
--TEST--
normalize()
--SKIPIF--
<?php
include('skipif.inc');
?>
--FILE--
<?php
/* Create an XML document
* with structure
* <book>
* <author></author>
* <title>This is the title</title>
* </book>
* Calculate the number of title text nodes (1).
* Add another text node to title. Calculate the number of title text nodes (2).
* Normalize author. Calculate the number of title text nodes (2).
* Normalize title. Calculate the number of title text nodes (1).
*/
$doc = new DOMDocument();
$root = $doc->createElement('book');
$doc->appendChild($root);
$title = $doc->createElement('title');
$root->appendChild($title);
$author = $doc->createElement('author');
$root->appendChild($author);
$text = $doc->createTextNode('This is the first title');
$title->appendChild($text);
echo "Number of child nodes of title = ";
var_dump($title->childNodes->length);
// add a second text node to title
$text = $doc->createTextNode('This is the second title');
$title->appendChild($text);
echo "Number of child nodes of title after adding second title = ";
var_dump($title->childNodes->length);
// should do nothing
$author->normalize();
echo "Number of child nodes of title after normalizing author = ";
var_dump($title->childNodes->length);
// should concatenate first and second title text nodes
$title->normalize();
echo "Number of child nodes of title after normalizing title = ";
var_dump($title->childNodes->length);
?>
--EXPECTF--
Number of child nodes of title = int(1)
Number of child nodes of title after adding second title = int(2)
Number of child nodes of title after normalizing author = int(2)
Number of child nodes of title after normalizing title = int(1)
@@ -0,0 +1,113 @@
--TEST--
DOM removeChild : Basic Functionality
--SKIPIF--
<?php
require_once('skipif.inc');
?>
--CREDITS--
Simon Hughes <odbc3@hotmail.com>
--FILE--
<?php
$xml = <<< EOXML
<?xml version="1.0" encoding="ISO-8859-1"?>
<courses>
<course title="one">
<notes>
<note>c1n1</note>
<note>c1n2</note>
</notes>
</course>
<course title="two">
<notes>
<note>c2n1</note>
<note>c2n2</note>
</notes>
</course>
</courses>
EOXML;
function dumpcourse($current) {
$title = ($current->nodeType != XML_TEXT_NODE && $current->hasAttribute('title')) ? $current->getAttribute('title'):"no title";
echo "Course: $title:";var_dump($current);
echo "~";var_dump($current->textContent);
}
$dom = new DOMDocument();
$dom->loadXML($xml);
$root = $dom->documentElement;
$children = $root->childNodes;
$len = $children->length;
echo "orignal has $len nodes\n";
for ($index = $children->length - 1; $index >=0; $index--) {
echo "node $index\n";
$current = $children->item($index);
dumpcourse($current);
if ($current->nodeType == XML_TEXT_NODE) {
$noderemoved = $root->removeChild($current);
}
}
$children = $root->childNodes;
$len = $children->length;
echo "after text removed it now has $len nodes\n";
for ($index = 0; $index < $children->length; $index++) {
echo "node $index\n";
$current = $children->item($index);
dumpcourse($current);
}
--EXPECTF--
orignal has 5 nodes
node 4
Course: no title:object(DOMText)#4 (0) {
}
~string(1) "
"
node 3
Course: two:object(DOMElement)#5 (0) {
}
~string(24) "
c2n1
c2n2
"
node 2
Course: no title:object(DOMText)#6 (0) {
}
~string(2) "
"
node 1
Course: one:object(DOMElement)#4 (0) {
}
~string(24) "
c1n1
c1n2
"
node 0
Course: no title:object(DOMText)#5 (0) {
}
~string(2) "
"
after text removed it now has 2 nodes
node 0
Course: one:object(DOMElement)#3 (0) {
}
~string(24) "
c1n1
c1n2
"
node 1
Course: two:object(DOMElement)#4 (0) {
}
~string(24) "
c2n1
c2n2
"
@@ -0,0 +1,44 @@
--TEST--
Replacing a child node
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--CREDITS--
Matt Raines <matt@raines.me.uk>
#London TestFest 2008
--FILE--
<?php
$document = new DOMDocument();
$document->loadXML('<?xml version="1.0" encoding="utf-8"?>
<root><foo><bar/><baz/></foo><spam><eggs/><eggs/></spam></root>');
// Replaces the child node oldChild with newChild in the list of children, and
// returns the oldChild node.
$parent = $document->getElementsByTagName('foo')->item(0);
$new_child = $document->createElement('qux');
$old_child = $parent->replaceChild($new_child, $parent->firstChild);
echo "New child replaces old child:\n" . $document->saveXML();
echo "Old child is returned:\n" . $old_child->tagName . "\n";
// If the newChild is already in the tree, it is first removed.
$parent = $document->getElementsByTagName('spam')->item(0);
$parent->replaceChild($new_child, $parent->firstChild);
echo "Existing child is removed from tree:\n" . $document->saveXML();
// Children are inserted in the correct order.
$new_child = $document->getElementsByTagName('spam')->item(0);
$parent = $document->getElementsByTagName('foo')->item(0);
$parent->replaceChild($new_child, $parent->firstChild);
echo "Children are inserted in order:\n" . $document->saveXML();
?>
--EXPECT--
New child replaces old child:
<?xml version="1.0" encoding="utf-8"?>
<root><foo><qux/><baz/></foo><spam><eggs/><eggs/></spam></root>
Old child is returned:
bar
Existing child is removed from tree:
<?xml version="1.0" encoding="utf-8"?>
<root><foo><baz/></foo><spam><qux/><eggs/></spam></root>
Children are inserted in order:
<?xml version="1.0" encoding="utf-8"?>
<root><foo><spam><qux/><eggs/></spam></foo></root>
@@ -0,0 +1,35 @@
--TEST--
DOMText::appendData basic functionality test
--CREDITS--
Mike Sullivan <mike@regexia.com>
#TestFest 2008 (London)
--FILE--
<?php
$document = new DOMDocument;
$root = $document->createElement('root');
$document->appendChild($root);
$text = $document->createElement('text');
$root->appendChild($text);
$textnode = $document->createTextNode('');
$text->appendChild($textnode);
$textnode->appendData('data');
echo "Text Length (one append): " . $textnode->length . "\n";
$textnode->appendData('><&"');
echo "Text Length (two appends): " . $textnode->length . "\n";
echo "Text Content: " . $textnode->data . "\n";
echo "\n" . $document->saveXML();
?>
--EXPECT--
Text Length (one append): 4
Text Length (two appends): 8
Text Content: data><&"
<?xml version="1.0"?>
<root><text>data&gt;&lt;&amp;"</text></root>