forked from simplesamlphp/xml-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPathTest.php
More file actions
435 lines (359 loc) · 15.1 KB
/
XPathTest.php
File metadata and controls
435 lines (359 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\XPath;
use DOMDocument;
use DOMElement;
use DOMText;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XPath\XPath;
use Throwable;
use function libxml_clear_errors;
use function libxml_use_internal_errors;
/**
* Tests for the SimpleSAML\XPath\XPath helper.
*/
#[CoversClass(XPath::class)]
final class XPathTest extends TestCase
{
public function testGetXPathCachesPerDocumentAndRegistersCoreNamespaces(): void
{
// Doc A with an xml:space attribute to validate 'xml' prefix usage works
$docA = new DOMDocument();
$docA->loadXML(<<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<child>value</child>
</root>
XML);
// Doc B is different
$docB = new DOMDocument();
$docB->loadXML(<<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<another><node/></another>
XML);
$xpA1 = XPath::getXPath($docA);
$xpA2 = XPath::getXPath($docA);
$xpB = XPath::getXPath($docB);
// Cached instance reused per same document
$this->assertSame($xpA1, $xpA2);
// Different document => different DOMXPath instance
$this->assertNotSame($xpA1, $xpB);
// 'xml' prefix registered: query should be valid and return xml:space attribute
$rootA = $docA->documentElement;
$this->assertInstanceOf(DOMElement::class, $rootA);
$attrs = XPath::xpQuery($rootA, '@xml:space', $xpA1);
$this->assertCount(1, $attrs);
$this->assertSame('preserve', $attrs[0]->nodeValue);
}
public function testAncestorNamespaceRegistrationAllowsCustomPrefixes(): void
{
// Custom namespace declared on the root; query from a descendant node
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:foo="https://example.org/foo">
<a>
<b>
<foo:item>ok</foo:item>
</b>
</a>
</r>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
// Use a deep context node to ensure ancestor-walk picks up xmlns:foo from root
$context = $doc->getElementsByTagName('b')->item(0);
$this->assertInstanceOf(\DOMElement::class, $context);
$xp = XPath::getXPath($context);
$nodes = XPath::xpQuery($context, 'foo:item', $xp);
$this->assertCount(1, $nodes);
$this->assertSame('ok', $nodes[0]->textContent);
}
public function testXpQueryThrowsOnMalformedExpression(): void
{
$doc = new DOMDocument();
$doc->loadXML('<root><x/></root>');
$xp = XPath::getXPath($doc);
// If xpQuery throws a specific exception, put that class here instead of Throwable.
$this->expectException(Throwable::class);
// Keep message assertion resilient to libxml version differences.
$this->expectExceptionMessageMatches('/(XPath|expression).*invalid|malformed|error/i');
// Malformed XPath: missing closing bracket
$root = $doc->documentElement;
$this->assertInstanceOf(DOMElement::class, $root);
// Avoid emitting a PHP warning; let xpQuery surface it as an exception.
libxml_use_internal_errors(true);
try {
XPath::xpQuery($root, '//*[', $xp);
} finally {
$errors = libxml_get_errors();
self::assertCount(1, $errors);
self::assertEquals("Invalid expression\n", $errors[0]->message);
libxml_clear_errors();
libxml_use_internal_errors(false);
}
}
public function testXmlnsDetectionRegistersPrefixedNamespace(): void
{
// This XML ensures we hit the detection branch and pass the second guard:
// - xmlns:foo="urn:two" should be detected and registered
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ctx xmlns:foo="urn:two">
<foo:item>ok</foo:item>
</ctx>
</root>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
$context = $doc->getElementsByTagName('ctx')->item(0);
$this->assertInstanceOf(DOMElement::class, $context);
$xp = XPath::getXPath($context);
// Passing the guards should register 'foo' so this resolves
$nodes = XPath::xpQuery($context, 'foo:item', $xp);
$this->assertCount(1, $nodes);
$this->assertSame('ok', $nodes[0]->textContent);
}
public function testNormalizationFromTextNode(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:foo="https://example.org/foo">
<a><foo:item>ok</foo:item></a>
</r>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
$item = $doc->getElementsByTagNameNS('https://example.org/foo', 'item')->item(0);
$this->assertInstanceOf(DOMElement::class, $item);
$text = $item->firstChild; // DOMText node inside <foo:item>
$this->assertInstanceOf(DOMText::class, $text);
// getXPath should handle a non-element node, normalize to the nearest element ancestor,
// and register the 'foo' namespace so a prefixed query works.
$xp = XPath::getXPath($text);
$nodes = XPath::xpQuery($text, 'ancestor::foo:item', $xp);
$this->assertCount(1, $nodes);
$this->assertSame('ok', $nodes[0]->textContent);
}
public function testNormalizationFromAttributeNode(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:bar="urn:bar">
<bar:elt bar:attr="v"><x/></bar:elt>
</root>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
$elt = $doc->getElementsByTagNameNS('urn:bar', 'elt')->item(0);
$this->assertInstanceOf(\DOMElement::class, $elt);
$attr = $elt->getAttributeNodeNS('urn:bar', 'attr');
/** @var \DOMAttr $attr */
// getXPath should normalize from DOMAttr to the element and ensure 'bar' is registered.
$xp = XPath::getXPath($attr);
$nodes = XPath::xpQuery($attr, 'ancestor::bar:elt', $xp);
$this->assertCount(1, $nodes);
// Ensure we have an element before calling element-only methods.
$this->assertInstanceOf(\DOMElement::class, $nodes[0]);
/** @var \DOMElement $el */
$el = $nodes[0];
$this->assertSame('v', $el->getAttributeNS('urn:bar', 'attr'));
}
public function testSkipsDefaultNamespaceDeclarationDoesNotCreateUsableXmlnsPrefix(): void
{
// Default namespace present; no prefixed declaration.
// The guard should skip registering 'xmlns' as a usable prefix.
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:def">
<a><b>t</b></a>
</root>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
$context = $doc->documentElement?->getElementsByTagName('b')->item(0);
$this->assertInstanceOf(\DOMElement::class, $context);
$xp = XPath::getXPath($context);
// Using 'xmlns' as a prefix should fail because the code skips binding it.
libxml_use_internal_errors(true);
try {
$this->expectException(\Throwable::class);
// The XPath helper wraps libxml errors into a generic message:
$this->expectExceptionMessage('Malformed XPath query or invalid contextNode provided.');
XPath::xpQuery($context, 'xmlns:b', $xp);
} finally {
$errors = libxml_get_errors();
$this->assertEquals("Undefined namespace prefix\n", $errors[0]->message);
libxml_clear_errors();
libxml_use_internal_errors(false);
}
}
public function testSkipsEmptyUriNamespaceDeclaration(): void
{
// An empty-URI namespace declaration must be ignored by the guard ($uri === '').
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:empty="">
<child>t</child>
</root>
XML;
// Attempting to use the 'empty' prefix should fail because it wasn't registered.
libxml_use_internal_errors(true);
try {
$doc = new DOMDocument();
$doc->loadXML($xml);
$context = $doc->getElementsByTagName('child')->item(0);
$this->assertInstanceOf(\DOMElement::class, $context);
$xp = XPath::getXPath($context);
$this->expectException(\Throwable::class);
// The XPath helper wraps libxml errors into a generic message:
$this->expectExceptionMessage('Malformed XPath query or invalid contextNode provided.');
XPath::xpQuery($context, 'empty:whatever', $xp);
} finally {
$errors = libxml_get_errors();
$this->assertEquals("xmlns:empty: Empty XML namespace is not allowed\n", $errors[0]->message);
$this->assertEquals("Undefined namespace prefix\n", $errors[1]->message);
libxml_clear_errors();
libxml_use_internal_errors(false);
}
}
public function testXmlnsPrefixedDeclarationRegistersNamespaceViaAttributeBranch(): void
{
// Build DOM programmatically to ensure xmlns:foo exists as attribute.
$doc = new DOMDocument('1.0', 'UTF-8');
$root = $doc->createElement('root');
$doc->appendChild($root);
// Add xmlns:foo on the root (attribute-branch should detect and register)
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:foo', 'https://example.org/foo');
// Deep subtree that uses foo prefix but the context node itself is unprefixed
$ctx = $doc->createElement('ctx');
$root->appendChild($ctx);
$fooItem = $doc->createElementNS('https://example.org/foo', 'foo:item', 'ok');
$ctx->appendChild($fooItem);
// Use the unprefixed context to ensure ancestor-walk is required.
$xp = XPath::getXPath($ctx);
$nodes = XPath::xpQuery($ctx, 'foo:item', $xp);
// If attribute-branch registered 'foo', the query resolves.
$this->assertCount(1, $nodes);
$this->assertSame('ok', $nodes[0]->textContent);
}
/**
* Provides relative file paths for the two XML variants.
*
* @return array<string, array{0: string}>
*/
public static function xmlVariantsProviderForTopLevelSlatePerson(): array
{
$base = dirname(__FILE__, 3) . '/tests/resources/xml';
return [
"Ancestor-declared 'slate'; top-level person AFTER attributes" => [
$base . '/success_response_a.xml',
false,
false,
],
"Ancestor-declared 'slate'; top-level person BEFORE attributes" => [
$base . '/success_response_b.xml',
false,
false,
],
"Descendant-only 'slate'; no ancestor binding (fails without autoregister)" => [
$base . '/success_response_c.xml',
false,
true,
],
"Descendant-only 'slate'; no ancestor binding (succeeds with autoregister)" => [
$base . '/success_response_c.xml',
true,
false,
],
];
}
/**
* Ensure that absolute XPath '/foo:serviceResponse/foo:authenticationSuccess/slate:person'
* finds the same top-level slate:person regardless of whether it appears before or after
* cas:attributes in the document, even when the slate prefix is only declared on the element itself.
*/
#[DataProvider('xmlVariantsProviderForTopLevelSlatePerson')]
public function testAbsoluteXPathFindsTopLevelSlatePerson(
string $filePath,
bool $autoregister,
bool $shouldFail,
): void {
$doc = DOMDocumentFactory::fromFile($filePath);
$fooNs = 'https://example.org/foo';
/** @var \DOMElement|null $attributesNode */
$attributesNode = $doc->getElementsByTagNameNS($fooNs, 'attributes')->item(0);
$this->assertNotNull($attributesNode, 'Attributes element not found');
$xp = XPath::getXPath($attributesNode, $autoregister);
$query = '/foo:serviceResponse/foo:authenticationSuccess/slate:person';
if ($shouldFail) {
libxml_use_internal_errors(true);
try {
$this->expectException(\SimpleSAML\Assert\AssertionFailedException::class);
$this->expectExceptionMessage('Malformed XPath query or invalid contextNode provided.');
XPath::xpQuery($attributesNode, $query, $xp);
} finally {
$errors = libxml_get_errors();
$this->assertNotEmpty($errors);
$this->assertSame("Undefined namespace prefix\n", $errors[0]->message);
libxml_clear_errors();
libxml_use_internal_errors(false);
}
return;
}
$nodes = XPath::xpQuery($attributesNode, $query, $xp);
$this->assertCount(1, $nodes);
$this->assertSame('12345_top', trim($nodes[0]->textContent));
}
public function testFindElementFindsDirectChildUnprefixed(): void
{
$doc = new DOMDocument();
$doc->loadXML('<root><target>t</target><other/></root>');
$root = $doc->documentElement;
$this->assertInstanceOf(DOMElement::class, $root);
$found = XPath::findElement($root, 'target');
$this->assertInstanceOf(DOMElement::class, $found);
$this->assertSame('target', $found->localName);
$this->assertSame('t', $found->textContent);
}
public function testFindElementFindsDirectChildWithPrefixWhenNsOnRoot(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="https://example.org/foo">
<foo:item>ok</foo:item>
</root>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
$root = $doc->documentElement;
$this->assertInstanceOf(DOMElement::class, $root);
// Namespace is declared on root, so getXPath($doc) used by findElement knows 'foo'
$found = XPath::findElement($root, 'foo:item');
$this->assertInstanceOf(DOMElement::class, $found);
$this->assertSame('item', $found->localName);
$this->assertSame('https://example.org/foo', $found->namespaceURI);
$this->assertSame('ok', $found->textContent);
}
public function testFindElementReturnsFalseWhenNotFoundAndDoesNotDescend(): void
{
// 'target' is a grandchild; findElement should only match direct children via './name'
$doc = new DOMDocument();
$doc->loadXML('<root><container><target/></container></root>');
$root = $doc->documentElement;
$this->assertInstanceOf(DOMElement::class, $root);
$found = XPath::findElement($root, 'target');
$this->assertFalse($found, 'Should return false for non-direct child');
}
public function testFindElementThrowsIfNoOwnerDocument(): void
{
// A standalone DOMElement (not created by a DOMDocument) has no ownerDocument
$ref = new \DOMElement('container');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot search, no DOMDocument available');
XPath::findElement($ref, 'anything');
}
}