xref: /PHP-8.4/ext/dom/tests/gh11500.phpt (revision 961e57eb)
1--TEST--
2GH-11500 (Namespace reuse in createElementNS() generates wrong output)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7function api_test_depth2($root_ns) {
8    $dom = new DOMDocument();
9    $root = $dom->createElementNS($root_ns, 'root');
10    $dom->appendChild($root);
11
12    $a1 = $dom->createElementNS('http://example.com', 'a1');
13    $b1 = $a1->appendChild($dom->createElementNS('http://example.com', 'b1'));
14    $root->appendChild($a1);
15
16    $a2 = $dom->createElementNS('http://example.com', 'a2');
17    $b2 = $a2->appendChild($dom->createElementNS('http://example.com', 'b2'));
18    $root->appendChild($a2);
19
20    echo $dom->saveXML();
21
22    var_dump($root->namespaceURI);
23    var_dump($a1->namespaceURI);
24    var_dump($b1->namespaceURI);
25    var_dump($a2->namespaceURI);
26    var_dump($b2->namespaceURI);
27}
28
29function api_test_depth3($root_ns, $swapped) {
30    $dom = new DOMDocument();
31    $root = $dom->createElementNS($root_ns, 'root');
32    $dom->appendChild($root);
33
34    $a1 = $dom->createElementNS('http://example.com', 'a1');
35    $b1 = $a1->appendChild($dom->createElementNS('http://example.com', 'b1'));
36    $c1 = $b1->appendChild($dom->createElementNS('http://example.com', 'c1'));
37    $root->appendChild($a1);
38
39    $a2 = $dom->createElementNS('http://example.com', 'a2');
40    if ($swapped) {
41        $b2 = $dom->createElementNS('http://example.com', 'b2');
42        $c2 = $b2->appendChild($dom->createElementNS('http://example.com', 'c2'));
43        $a2->appendChild($b2);
44    } else {
45        $b2 = $a2->appendChild($dom->createElementNS('http://example.com', 'b2'));
46        $c2 = $b2->appendChild($dom->createElementNS('http://example.com', 'c2'));
47    }
48    $root->appendChild($a2);
49
50    echo $dom->saveXML();
51
52    var_dump($root->namespaceURI);
53    var_dump($a1->namespaceURI);
54    var_dump($b1->namespaceURI);
55    var_dump($c1->namespaceURI);
56    var_dump($a2->namespaceURI);
57    var_dump($b2->namespaceURI);
58    var_dump($c2->namespaceURI);
59}
60
61echo "-- Constructed from API (depth 2, mismatched root variation) --\n";
62api_test_depth2('http://example2.com');
63
64echo "-- Constructed from API (depth 2, matching root variation) --\n";
65api_test_depth2('http://example.com');
66
67echo "-- Constructed from API (depth 3, mismatched root variation, non-swapped) --\n";
68api_test_depth3('http://example2.com', false);
69
70echo "-- Constructed from API (depth 3, matching root variation, non-swapped) --\n";
71api_test_depth3('http://example.com', false);
72
73echo "-- Constructed from API (depth 3, mismatched root variation, swapped) --\n";
74api_test_depth3('http://example2.com', true);
75
76echo "-- Constructed from API (depth 3, matching root variation, swapped) --\n";
77api_test_depth3('http://example.com', true);
78
79echo "-- Constructed depth 2 from string --\n";
80$xml = '<?xml version="1.0"?><root xmlns="http://example2.com"><a1 xmlns="http://example.com"><b1/></a1><a2 xmlns="http://example.com"><b2/></a2></root>';
81$dom = new DOMDocument;
82$dom->loadXML($xml);
83echo $dom->saveXML(), "\n";
84
85var_dump($dom->documentElement->namespaceURI); // root
86var_dump($dom->documentElement->firstChild->namespaceURI); // a1
87var_dump($dom->documentElement->firstChild->firstChild->namespaceURI); // b1
88var_dump($dom->documentElement->firstChild->nextSibling->namespaceURI); // a2
89var_dump($dom->documentElement->firstChild->nextSibling->firstChild->namespaceURI); // b2
90?>
91--EXPECT--
92-- Constructed from API (depth 2, mismatched root variation) --
93<?xml version="1.0"?>
94<root xmlns="http://example2.com"><a1 xmlns="http://example.com"><b1/></a1><a2 xmlns="http://example.com"><b2/></a2></root>
95string(19) "http://example2.com"
96string(18) "http://example.com"
97string(18) "http://example.com"
98string(18) "http://example.com"
99string(18) "http://example.com"
100-- Constructed from API (depth 2, matching root variation) --
101<?xml version="1.0"?>
102<root xmlns="http://example.com"><a1><b1/></a1><a2><b2/></a2></root>
103string(18) "http://example.com"
104string(18) "http://example.com"
105string(18) "http://example.com"
106string(18) "http://example.com"
107string(18) "http://example.com"
108-- Constructed from API (depth 3, mismatched root variation, non-swapped) --
109<?xml version="1.0"?>
110<root xmlns="http://example2.com"><a1 xmlns="http://example.com"><b1><c1/></b1></a1><a2 xmlns="http://example.com"><b2><c2/></b2></a2></root>
111string(19) "http://example2.com"
112string(18) "http://example.com"
113string(18) "http://example.com"
114string(18) "http://example.com"
115string(18) "http://example.com"
116string(18) "http://example.com"
117string(18) "http://example.com"
118-- Constructed from API (depth 3, matching root variation, non-swapped) --
119<?xml version="1.0"?>
120<root xmlns="http://example.com"><a1><b1><c1/></b1></a1><a2><b2><c2/></b2></a2></root>
121string(18) "http://example.com"
122string(18) "http://example.com"
123string(18) "http://example.com"
124string(18) "http://example.com"
125string(18) "http://example.com"
126string(18) "http://example.com"
127string(18) "http://example.com"
128-- Constructed from API (depth 3, mismatched root variation, swapped) --
129<?xml version="1.0"?>
130<root xmlns="http://example2.com"><a1 xmlns="http://example.com"><b1><c1/></b1></a1><a2 xmlns="http://example.com"><b2><c2/></b2></a2></root>
131string(19) "http://example2.com"
132string(18) "http://example.com"
133string(18) "http://example.com"
134string(18) "http://example.com"
135string(18) "http://example.com"
136string(18) "http://example.com"
137string(18) "http://example.com"
138-- Constructed from API (depth 3, matching root variation, swapped) --
139<?xml version="1.0"?>
140<root xmlns="http://example.com"><a1><b1><c1/></b1></a1><a2><b2><c2/></b2></a2></root>
141string(18) "http://example.com"
142string(18) "http://example.com"
143string(18) "http://example.com"
144string(18) "http://example.com"
145string(18) "http://example.com"
146string(18) "http://example.com"
147string(18) "http://example.com"
148-- Constructed depth 2 from string --
149<?xml version="1.0"?>
150<root xmlns="http://example2.com"><a1 xmlns="http://example.com"><b1/></a1><a2 xmlns="http://example.com"><b2/></a2></root>
151
152string(19) "http://example2.com"
153string(18) "http://example.com"
154string(18) "http://example.com"
155string(18) "http://example.com"
156string(18) "http://example.com"
157