xref: /PHP-8.3/ext/dom/tests/bug67440.phpt (revision b1d8e240)
1--TEST--
2Bug #67440 (append_node of a DOMDocumentFragment does not reconcile namespaces)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8function createDocument() {
9    $document = new DOMDocument();
10    $document->loadXML('<?xml version="1.0"?><rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2"></rootElement>');
11    $fragment = $document->createDocumentFragment();
12    $fragment->appendChild($document->createTextNode("\n"));
13    $fragment->appendChild($document->createElementNS('http://example/ns', 'myns:childNode', '1'));
14    $fragment->appendChild($document->createTextNode("\n"));
15    $fragment->appendChild($document->createElementNS('http://example/ns', 'myns:childNode', '2'));
16    $fragment->appendChild($document->createTextNode("\n"));
17    return array($document, $fragment);
18}
19
20function case1($method) {
21    list($document, $fragment) = createDocument();
22    $document->documentElement->{$method}($fragment);
23    echo $document->saveXML();
24}
25
26function case2($method) {
27    list($document, $fragment) = createDocument();
28    $childNodes = iterator_to_array($fragment->childNodes);
29    foreach ($childNodes as $childNode) {
30        $document->documentElement->{$method}($childNode);
31    }
32    echo $document->saveXML();
33}
34
35function case3($method) {
36    list($document, $fragment) = createDocument();
37    $fragment->removeChild($fragment->firstChild);
38    $document->documentElement->{$method}($fragment);
39    echo $document->saveXML();
40}
41
42function case4($method) {
43    list($document, $fragment) = createDocument();
44    $fragment->childNodes[1]->appendChild($document->createElementNS('http://example/ns2', 'myns2:childNode', '3'));
45    $document->documentElement->{$method}($fragment);
46    echo $document->saveXML();
47}
48
49echo "== appendChild ==\n";
50echo "-- fragment to document element --\n"; case1('appendChild'); echo "\n";
51echo "-- children manually document element --\n"; case2('appendChild'); echo "\n";
52echo "-- fragment to document where first element is not a text node --\n"; case3('appendChild'); echo "\n";
53echo "-- fragment with namespace declarations in children --\n"; case4('appendChild'); echo "\n";
54
55echo "== insertBefore ==\n";
56echo "-- fragment to document element --\n"; case1('insertBefore'); echo "\n";
57echo "-- children manually document element --\n"; case2('insertBefore'); echo "\n";
58echo "-- fragment to document where first element is not a text node --\n"; case3('insertBefore'); echo "\n";
59echo "-- fragment with namespace declarations in children --\n"; case4('insertBefore'); echo "\n";
60
61echo "== insertAfter ==\n";
62echo "-- fragment to document element --\n"; case1('insertBefore'); echo "\n";
63echo "-- children manually document element --\n"; case2('insertBefore'); echo "\n";
64echo "-- fragment to document where first element is not a text node --\n"; case3('insertBefore'); echo "\n";
65echo "-- fragment with namespace declarations in children --\n"; case4('insertBefore'); echo "\n";
66
67?>
68--EXPECT--
69== appendChild ==
70-- fragment to document element --
71<?xml version="1.0"?>
72<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
73<myns:childNode>1</myns:childNode>
74<myns:childNode>2</myns:childNode>
75</rootElement>
76
77-- children manually document element --
78<?xml version="1.0"?>
79<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
80<myns:childNode>1</myns:childNode>
81<myns:childNode>2</myns:childNode>
82</rootElement>
83
84-- fragment to document where first element is not a text node --
85<?xml version="1.0"?>
86<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2"><myns:childNode>1</myns:childNode>
87<myns:childNode>2</myns:childNode>
88</rootElement>
89
90-- fragment with namespace declarations in children --
91<?xml version="1.0"?>
92<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
93<myns:childNode>1<myns2:childNode>3</myns2:childNode></myns:childNode>
94<myns:childNode>2</myns:childNode>
95</rootElement>
96
97== insertBefore ==
98-- fragment to document element --
99<?xml version="1.0"?>
100<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
101<myns:childNode>1</myns:childNode>
102<myns:childNode>2</myns:childNode>
103</rootElement>
104
105-- children manually document element --
106<?xml version="1.0"?>
107<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
108<myns:childNode>1</myns:childNode>
109<myns:childNode>2</myns:childNode>
110</rootElement>
111
112-- fragment to document where first element is not a text node --
113<?xml version="1.0"?>
114<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2"><myns:childNode>1</myns:childNode>
115<myns:childNode>2</myns:childNode>
116</rootElement>
117
118-- fragment with namespace declarations in children --
119<?xml version="1.0"?>
120<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
121<myns:childNode>1<myns2:childNode>3</myns2:childNode></myns:childNode>
122<myns:childNode>2</myns:childNode>
123</rootElement>
124
125== insertAfter ==
126-- fragment to document element --
127<?xml version="1.0"?>
128<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
129<myns:childNode>1</myns:childNode>
130<myns:childNode>2</myns:childNode>
131</rootElement>
132
133-- children manually document element --
134<?xml version="1.0"?>
135<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
136<myns:childNode>1</myns:childNode>
137<myns:childNode>2</myns:childNode>
138</rootElement>
139
140-- fragment to document where first element is not a text node --
141<?xml version="1.0"?>
142<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2"><myns:childNode>1</myns:childNode>
143<myns:childNode>2</myns:childNode>
144</rootElement>
145
146-- fragment with namespace declarations in children --
147<?xml version="1.0"?>
148<rootElement xmlns:myns="http://example/ns" xmlns:myns2="http://example/ns2">
149<myns:childNode>1<myns2:childNode>3</myns2:childNode></myns:childNode>
150<myns:childNode>2</myns:childNode>
151</rootElement>
152