Skip to content

Commit e02dd27

Browse files
committed
Add regression tests
1 parent 2d5bd4b commit e02dd27

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
DOMDocument::createAttributeNS() with prefix name conflict
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument();
9+
$doc->appendChild($doc->createElement('container'));
10+
11+
echo "--- setAttributeNode variation (you shouldn't do this, but we test this anyway) ---\n";
12+
13+
// Adheres to DOM Core Level 3
14+
15+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns1', 'hello'))?->namespaceURI);
16+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns2', 'hello'))?->namespaceURI);
17+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns3', 'hello'))?->namespaceURI);
18+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns4', 'hello'))?->namespaceURI);
19+
echo $doc->saveXML(), "\n";
20+
21+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns1', 'foo:hello'))?->namespaceURI);
22+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns2', 'foo:hello'))?->namespaceURI);
23+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns3', 'foo:hello'))?->namespaceURI);
24+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns4', 'foo:hello'))?->namespaceURI);
25+
echo $doc->saveXML(), "\n";
26+
27+
$doc = new DOMDocument();
28+
$doc->appendChild($doc->createElement('container'));
29+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns1', 'foo:hello'))?->namespaceURI);
30+
var_dump($doc->documentElement->setAttributeNode($doc->createAttributeNS('http://php.net/ns1', 'hello'))?->namespaceURI);
31+
echo $doc->saveXML(), "\n";
32+
33+
echo "--- setAttributeNodeNS variation ---\n";
34+
35+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns1', 'hello'))?->namespaceURI);
36+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns2', 'hello'))?->namespaceURI);
37+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns3', 'hello'))?->namespaceURI);
38+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns4', 'hello'))?->namespaceURI);
39+
echo $doc->saveXML(), "\n";
40+
41+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns1', 'foo:hello'))?->namespaceURI);
42+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns2', 'foo:hello'))?->namespaceURI);
43+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns3', 'foo:hello'))?->namespaceURI);
44+
var_dump($doc->documentElement->setAttributeNodeNS($doc->createAttributeNS('http://php.net/ns4', 'foo:hello'))?->namespaceURI);
45+
echo $doc->saveXML(), "\n";
46+
47+
?>
48+
--EXPECTF--
49+
--- setAttributeNode variation (you shouldn't do this, but we test this anyway) ---
50+
NULL
51+
52+
Fatal error: Uncaught DOMException: Namespace Error in %s:%d
53+
Stack trace:
54+
#0 %s(%d): DOMDocument->createAttributeNS('http://php.net/...', 'hello')
55+
#1 {main}
56+
thrown in %s on line %d
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
DOMDocument::importNode() with attribute prefix name conflict
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
echo "--- Non-default namespace test case without a default namespace in the destination ---\n";
9+
10+
$dom1 = new DOMDocument();
11+
$dom2 = new DOMDocument();
12+
$dom1->loadXML('<?xml version="1.0"?><container xmlns:foo="http://php.net" foo:bar="yes"/>');
13+
$dom2->loadXML('<?xml version="1.0"?><container xmlns:foo="http://php.net/2"/>');
14+
$attribute = $dom1->documentElement->getAttributeNode('foo:bar');
15+
$imported = $dom2->importNode($attribute);
16+
$dom2->documentElement->setAttributeNodeNS($imported);
17+
18+
echo $dom1->saveXML();
19+
echo $dom2->saveXML();
20+
21+
echo "--- Non-default namespace test case with a default namespace in the destination ---\n";
22+
23+
$dom1 = new DOMDocument();
24+
$dom2 = new DOMDocument();
25+
$dom1->loadXML('<?xml version="1.0"?><container xmlns:foo="http://php.net" foo:bar="yes"/>');
26+
$dom2->loadXML('<?xml version="1.0"?><container xmlns="http://php.net" xmlns:foo="http://php.net/2"/>');
27+
$attribute = $dom1->documentElement->getAttributeNode('foo:bar');
28+
$imported = $dom2->importNode($attribute);
29+
$dom2->documentElement->setAttributeNodeNS($imported);
30+
31+
echo $dom1->saveXML();
32+
echo $dom2->saveXML();
33+
34+
echo "--- Default namespace test case ---\n";
35+
36+
// We don't expect the namespace to be imported because default namespaces on the same element don't apply to attributes
37+
// but the attribute should be imported
38+
$dom1 = new DOMDocument();
39+
$dom2 = new DOMDocument();
40+
$dom1->loadXML('<?xml version="1.0"?><container xmlns="http://php.net" bar="yes"/>');
41+
$dom2->loadXML('<?xml version="1.0"?><container xmlns="http://php.net/2"/>');
42+
$attribute = $dom1->documentElement->getAttributeNode('bar');
43+
$imported = $dom2->importNode($attribute);
44+
$dom2->documentElement->setAttributeNodeNS($imported);
45+
46+
echo $dom1->saveXML();
47+
echo $dom2->saveXML();
48+
49+
?>
50+
--EXPECT--
51+
--- Non-default namespace test case without a default namespace in the destination ---
52+
<?xml version="1.0"?>
53+
<container xmlns:foo="http://php.net" foo:bar="yes"/>
54+
<?xml version="1.0"?>
55+
<container xmlns:foo="http://php.net/2" bar="yes"/>
56+
--- Non-default namespace test case with a default namespace in the destination ---
57+
<?xml version="1.0"?>
58+
<container xmlns:foo="http://php.net" foo:bar="yes"/>
59+
<?xml version="1.0"?>
60+
<container xmlns="http://php.net" xmlns:foo="http://php.net/2" bar="yes"/>
61+
--- Default namespace test case ---
62+
<?xml version="1.0"?>
63+
<container xmlns="http://php.net" bar="yes"/>
64+
<?xml version="1.0"?>
65+
<container xmlns="http://php.net/2" bar="yes"/>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
DOMElement::setAttributeNS() with prefix name conflict
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
echo "--- Non-default namespace test case ---\n";
8+
9+
$dom = new DOMDocument();
10+
$dom->loadXML('<?xml version="1.0"?><container xmlns:foo="http://php.net" foo:bar="yes"/>');
11+
$dom->documentElement->setAttributeNS('http://php.net/2', 'foo:bar', 'no1');
12+
echo $dom->saveXML();
13+
$dom->documentElement->setAttributeNS('http://php.net/2', 'bar', 'no2');
14+
echo $dom->saveXML();
15+
16+
echo "--- Default namespace test case ---\n";
17+
18+
$dom = new DOMDocument();
19+
$dom->loadXML('<?xml version="1.0"?><container xmlns="http://php.net" bar="yes"/>');
20+
$dom->documentElement->setAttributeNS('http://php.net/2', 'bar', 'no1');
21+
echo $dom->saveXML();
22+
$dom->documentElement->setAttributeNS('http://php.net/2', 'bar', 'no2');
23+
echo $dom->saveXML();
24+
?>
25+
--EXPECTF--
26+
--- Non-default namespace test case ---
27+
28+
Fatal error: Uncaught DOMException: Namespace Error in %s:%d
29+
Stack trace:
30+
#0 %s(%d): DOMElement->setAttributeNS('http://php.net/...', 'foo:bar', 'no1')
31+
#1 {main}
32+
thrown in %s on line %d

0 commit comments

Comments
 (0)