|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XML\Test\Assert; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use SimpleSAML\Assert\AssertionFailedException; |
| 12 | +use SimpleSAML\XML\Assert\Assert as XMLAssert; |
| 13 | +use SimpleSAML\XML\Constants as C; |
| 14 | + |
| 15 | +use function str_pad; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class \SimpleSAML\XML\Assert\XPathFilterTest |
| 19 | + * |
| 20 | + * @package simplesamlphp/xml-common |
| 21 | + */ |
| 22 | +#[CoversClass(XMLAssert::class)] |
| 23 | +final class XPathFilterTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param boolean $shouldPass |
| 27 | + * @param string $filter |
| 28 | + */ |
| 29 | + #[DataProvider('provideXPathFilter')] |
| 30 | + public function testDefaultAllowedXPathFilter( |
| 31 | + bool $shouldPass, |
| 32 | + string $filter, |
| 33 | + array $axes = C::DEFAULT_ALLOWED_AXES, |
| 34 | + array $functions = C::DEFAULT_ALLOWED_FUNCTIONS, |
| 35 | + ): void { |
| 36 | + try { |
| 37 | + XMLAssert::allowedXPathFilter($filter, $axes, $functions); |
| 38 | + $this->assertTrue($shouldPass); |
| 39 | + } catch (AssertionFailedException $e) { |
| 40 | + $this->assertFalse($shouldPass); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * @return array<string, array{0: bool, 1: string}> |
| 47 | + */ |
| 48 | + public static function provideXPathFilter(): array |
| 49 | + { |
| 50 | + return [ |
| 51 | + // Axes |
| 52 | + 'ancestor' => [true, 'ancestor::book'], |
| 53 | + 'ancestor-or-self' => [true, 'ancestor-or-self::book'], |
| 54 | + 'attribute' => [true, 'attribute::book'], |
| 55 | + 'child' => [true, 'child::book'], |
| 56 | + 'descendant' => [true, 'descendant::book'], |
| 57 | + 'descendant-or-self' => [true, 'descendant-or-self::book'], |
| 58 | + 'following' => [true, 'following::book'], |
| 59 | + 'following-sibling' => [true, 'following-sibling::book'], |
| 60 | + 'namespace' => [false, 'namespace::book'], |
| 61 | + 'namespace' => [true, 'namespace::book', ['namespace']], |
| 62 | + 'parent' => [true, 'parent::book'], |
| 63 | + 'preceding' => [true, 'preceding::book'], |
| 64 | + 'preceding-sibling' => [true, 'preceding-sibling::book'], |
| 65 | + 'self' => [true, 'self::book'], |
| 66 | + |
| 67 | + // Functions |
| 68 | + 'boolean' => [false, 'boolean(Data/username/text())'], |
| 69 | + 'ceiling' => [false, 'ceiling(//items/item[1]/price)'], |
| 70 | + 'concat' => [false, "concat('A', '_', 'B')"], |
| 71 | + 'contains' => [false, "contains(//username, 'o')"], |
| 72 | + 'count' => [false, "count(//Sales.Order[Sales.Customer_Order/Sales.Customer/Name = 'Jansen'])"], |
| 73 | + 'false' => [false, '//Sales.Customer[IsGoldCustomer = false()]'], |
| 74 | + 'floor' => [false, 'floor(//items/item[1]/price)'], |
| 75 | + 'id' => [false, 'SalesInvoiceLines[id(1)]'], |
| 76 | + 'lang' => [false, 'lang("en-US")'], |
| 77 | + 'last' => [false, 'last()'], |
| 78 | + 'local-name' => [false, 'local-name(SalesInvoiceLines) '], |
| 79 | + 'name' => [false, 'name(SalesInvoiceLines)'], |
| 80 | + 'namespace-uri' => [false, 'namespace-uri(ReportData)'], |
| 81 | + 'normalize-space' => [false, 'normalize-space(" Hello World ")'], |
| 82 | + 'not' => [true, "//Sales.Customer[not(Name = 'Jansen')]"], |
| 83 | + 'number' => [false, 'number("123")'], |
| 84 | + 'position' => [false, 'position()'], |
| 85 | + 'round' => [false, 'round(//items/item[1]/price)'], |
| 86 | + 'starts-with' => [false, "//Sales.Customer[starts-with(Name, 'Jans')]"], |
| 87 | + 'string' => [false, 'string(123)'], |
| 88 | + 'string-length' => [false, 'string-length(//email)string-length(//email)'], |
| 89 | + 'substring' => [false, "/bookstore/book[substring(title,1,5)='Harry']"], |
| 90 | + 'substring-after' => [false, "/bookstore/book[substring-after(title,1,5)='Harry']"], |
| 91 | + 'substring-before' => [false, "/bookstore/book[substring-before(title,1,5)='Harry']"], |
| 92 | + 'sum' => [false, 'sum(//Sales.Order/TotalPrice)'], |
| 93 | + 'text' => [false, '//lastname/text()'], |
| 94 | + 'translate' => [false, "translate(//email, '@', '_')"], |
| 95 | + 'true' => [false, '//Sales.Customer[IsGoldCustomer = true()]'], |
| 96 | + |
| 97 | + // Edge-cases |
| 98 | + 'unknown axis' => [false, 'unknown::book'], |
| 99 | + 'unknown function' => [false, 'unknown()'], |
| 100 | + 'too long' => [false, str_pad('a', 120, 'a')], |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments