|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\XPath; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\XPath\XPath; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests for the SimpleSAML\XPath\XPath helper. |
| 13 | + */ |
| 14 | +#[CoversClass(XPath::class)] |
| 15 | +final class XPathTest extends TestCase |
| 16 | +{ |
| 17 | + public function testGetXPathCachesPerDocumentAndRegistersCoreNamespaces(): void |
| 18 | + { |
| 19 | + // Doc A with an xml:space attribute to validate 'xml' prefix usage works |
| 20 | + $docA = new \DOMDocument(); |
| 21 | + $docA->loadXML(<<<'XML' |
| 22 | +<?xml version="1.0" encoding="UTF-8"?> |
| 23 | +<root xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace"> |
| 24 | + <child>value</child> |
| 25 | +</root> |
| 26 | +XML); |
| 27 | + |
| 28 | + // Doc B is different |
| 29 | + $docB = new \DOMDocument(); |
| 30 | + $docB->loadXML(<<<'XML' |
| 31 | +<?xml version="1.0" encoding="UTF-8"?> |
| 32 | +<another><node/></another> |
| 33 | +XML); |
| 34 | + |
| 35 | + $xpA1 = XPath::getXPath($docA); |
| 36 | + $xpA2 = XPath::getXPath($docA); |
| 37 | + $xpB = XPath::getXPath($docB); |
| 38 | + |
| 39 | + // Cached instance reused per same document |
| 40 | + $this->assertSame($xpA1, $xpA2); |
| 41 | + // Different document => different DOMXPath instance |
| 42 | + $this->assertNotSame($xpA1, $xpB); |
| 43 | + |
| 44 | + // 'xml' prefix registered: query should be valid and return xml:space attribute |
| 45 | + $rootA = $docA->documentElement; |
| 46 | + $this->assertInstanceOf(\DOMElement::class, $rootA); |
| 47 | + $attrs = XPath::xpQuery($rootA, '@xml:space', $xpA1); |
| 48 | + $this->assertCount(1, $attrs); |
| 49 | + $this->assertSame('preserve', $attrs[0]->nodeValue); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public function testAncestorNamespaceRegistrationAllowsCustomPrefixes(): void |
| 54 | + { |
| 55 | + // Custom namespace declared on the root; query from a descendant node |
| 56 | + $xml = <<<'XML' |
| 57 | +<?xml version="1.0" encoding="UTF-8"?> |
| 58 | +<r xmlns:foo="https://example.org/foo"> |
| 59 | + <a> |
| 60 | + <b> |
| 61 | + <foo:item>ok</foo:item> |
| 62 | + </b> |
| 63 | + </a> |
| 64 | +</r> |
| 65 | +XML; |
| 66 | + $doc = new \DOMDocument(); |
| 67 | + $doc->loadXML($xml); |
| 68 | + |
| 69 | + // Use a deep context node to ensure ancestor-walk picks up xmlns:foo from root |
| 70 | + $context = $doc->getElementsByTagName('b')->item(0); |
| 71 | + $this->assertInstanceOf(\DOMElement::class, $context); |
| 72 | + |
| 73 | + $xp = XPath::getXPath($context); |
| 74 | + |
| 75 | + $nodes = XPath::xpQuery($context, 'foo:item', $xp); |
| 76 | + $this->assertCount(1, $nodes); |
| 77 | + $this->assertSame('ok', $nodes[0]->textContent); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public function testXpQueryThrowsOnMalformedExpression(): void |
| 82 | + { |
| 83 | + $doc = new \DOMDocument(); |
| 84 | + $doc->loadXML('<root><x/></root>'); |
| 85 | + $xp = XPath::getXPath($doc); |
| 86 | + |
| 87 | + // If xpQuery throws a specific exception, put that class here instead of \Throwable. |
| 88 | + $this->expectException(\Throwable::class); |
| 89 | + // Keep message assertion resilient to libxml version differences. |
| 90 | + $this->expectExceptionMessageMatches('/(XPath|expression).*invalid|malformed|error/i'); |
| 91 | + |
| 92 | + // Malformed XPath: missing closing bracket |
| 93 | + $root = $doc->documentElement; |
| 94 | + $this->assertInstanceOf(\DOMElement::class, $root); |
| 95 | + |
| 96 | + // Avoid emitting a PHP warning; let xpQuery surface it as an exception. |
| 97 | + \libxml_use_internal_errors(true); |
| 98 | + try { |
| 99 | + XPath::xpQuery($root, '//*[', $xp); |
| 100 | + } finally { |
| 101 | + $errors = \libxml_get_errors(); |
| 102 | + self::assertCount(1, $errors); |
| 103 | + self::assertEquals("Invalid expression\n", $errors[0]->message); |
| 104 | + \libxml_clear_errors(); |
| 105 | + \libxml_use_internal_errors(false); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments