1--TEST--
2Bug #79701 (getElementById does not correctly work with duplicate definitions) - set attribute in xml variation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7function test($dom, $fn) {
8    $test1 = $dom->getElementById('x');
9    $test2 = $dom->getElementById('y');
10
11    echo "--- After resetting test1's id ---\n";
12
13    $fn($test1, 'xml:id', 'y');
14    var_dump($dom->getElementById('x')?->nodeName);
15    var_dump($dom->getElementById('y')?->nodeName);
16
17    echo "--- After resetting test2's id ---\n";
18
19    $fn($test2, 'xml:id', 'x');
20    var_dump($dom->getElementById('x')?->nodeName);
21    var_dump($dom->getElementById('y')?->nodeName);
22
23    echo "--- After resetting test1's id ---\n";
24
25    $fn($test1, 'xml:id', 'z');
26    var_dump($dom->getElementById('x')?->nodeName);
27    var_dump($dom->getElementById('y')?->nodeName);
28
29    echo "--- After resetting test2's id ---\n";
30
31    $fn($test2, 'xml:id', 'z');
32    var_dump($dom->getElementById('x')?->nodeName);
33    var_dump($dom->getElementById('y')?->nodeName);
34
35    echo "--- Get id z ---\n";
36
37    var_dump($dom->getElementById('z')?->nodeName);
38}
39
40function getNamespace($name) {
41    if (str_contains($name, 'xml:')) {
42        return 'http://www.w3.org/XML/1998/namespace';
43    }
44    return '';
45}
46
47$common_xml = <<<XML
48<root>
49  <test1 xml:id="x"/>
50  <test2 xml:id="y"/>
51</root>
52XML;
53
54echo "\n=== DOMDocument: setAttribute ===\n\n";
55
56$dom = new DOMDocument;
57$dom->loadXML($common_xml);
58test($dom, fn ($element, $name, $value) => $element->setAttribute($name, $value));
59
60echo "\n=== DOMDocument: setAttributeNS ===\n\n";
61
62$dom = new DOMDocument;
63$dom->loadXML($common_xml);
64test($dom, fn ($element, $name, $value) => $element->setAttributeNS(getNamespace($name), $name, $value));
65
66echo "\n=== Dom\\XMLDocument: setAttribute ===\n\n";
67
68$dom = Dom\XMLDocument::createFromString($common_xml);
69test($dom, fn ($element, $name, $value) => $element->setAttribute($name, $value));
70
71echo "\n=== Dom\\XMLDocument: setAttributeNS ===\n\n";
72
73$dom = Dom\XMLDocument::createFromString($common_xml);
74test($dom, fn ($element, $name, $value) => $element->setAttributeNS(getNamespace($name), $name, $value));
75?>
76--EXPECT--
77=== DOMDocument: setAttribute ===
78
79--- After resetting test1's id ---
80NULL
81string(5) "test1"
82--- After resetting test2's id ---
83string(5) "test2"
84string(5) "test1"
85--- After resetting test1's id ---
86string(5) "test2"
87NULL
88--- After resetting test2's id ---
89NULL
90NULL
91--- Get id z ---
92string(5) "test1"
93
94=== DOMDocument: setAttributeNS ===
95
96--- After resetting test1's id ---
97NULL
98string(5) "test1"
99--- After resetting test2's id ---
100string(5) "test2"
101string(5) "test1"
102--- After resetting test1's id ---
103string(5) "test2"
104NULL
105--- After resetting test2's id ---
106NULL
107NULL
108--- Get id z ---
109string(5) "test1"
110
111=== Dom\XMLDocument: setAttribute ===
112
113--- After resetting test1's id ---
114NULL
115string(5) "test1"
116--- After resetting test2's id ---
117string(5) "test2"
118string(5) "test1"
119--- After resetting test1's id ---
120string(5) "test2"
121NULL
122--- After resetting test2's id ---
123NULL
124NULL
125--- Get id z ---
126string(5) "test1"
127
128=== Dom\XMLDocument: setAttributeNS ===
129
130--- After resetting test1's id ---
131NULL
132string(5) "test1"
133--- After resetting test2's id ---
134string(5) "test2"
135string(5) "test1"
136--- After resetting test1's id ---
137string(5) "test2"
138NULL
139--- After resetting test2's id ---
140NULL
141NULL
142--- Get id z ---
143string(5) "test1"
144