xref: /PHP-8.3/ext/dom/tests/gh14702.phpt (revision 42908f9f)
1--TEST--
2GH-14702 (DOMDocument::xinclude() crash)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7$doc = new DOMDocument();
8$doc->loadXML(<<<XML
9<?xml version="1.0"?>
10<root>
11  <child/>
12  <include href="foo" xmlns="http://www.w3.org/2001/XInclude">
13    <fallback/>
14  </include>
15  <keep/>
16</root>
17XML);
18$xi = $doc->createElementNS('http://www.w3.org/2001/XInclude', 'xi:include');
19$xi->setAttribute('href', 'nonexistent');
20
21$fallback = $doc->createElementNS('http://www.w3.org/2001/XInclude', 'xi:fallback');
22$xi->appendChild($fallback);
23$child1 = $fallback->appendChild($doc->createElement('fallback-child1'));
24$child2 = $fallback->appendChild($doc->createElement('fallback-child2'));
25
26$xpath = new DOMXPath($doc);
27$toReplace = $xpath->query('//child')->item(0);
28$toReplace->parentNode->replaceChild($xi, $toReplace);
29
30$keep = $doc->documentElement->lastElementChild;
31
32var_dump(@$doc->xinclude());
33echo $doc->saveXML();
34
35var_dump($child1, $child2, $fallback, $keep->nodeName);
36
37$keep->textContent = 'still works';
38echo $doc->saveXML();
39?>
40--EXPECT--
41int(2)
42<?xml version="1.0"?>
43<root>
44  <fallback-child1/><fallback-child2/>
45
46  <keep/>
47</root>
48object(DOMElement)#4 (1) {
49  ["schemaTypeInfo"]=>
50  NULL
51}
52object(DOMElement)#5 (1) {
53  ["schemaTypeInfo"]=>
54  NULL
55}
56object(DOMElement)#3 (1) {
57  ["schemaTypeInfo"]=>
58  NULL
59}
60string(4) "keep"
61<?xml version="1.0"?>
62<root>
63  <fallback-child1/><fallback-child2/>
64
65  <keep>still works</keep>
66</root>
67