|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2026 SURF B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace Surfnet\SamlBundle\Tests\Component\Metadata; |
| 20 | + |
| 21 | +use DOMDocument; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +class XsdValidatorTest extends TestCase |
| 25 | +{ |
| 26 | + private XsdValidator $validator; |
| 27 | + |
| 28 | + public function setUp(): void |
| 29 | + { |
| 30 | + $this->validator = new XsdValidator(); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_validates_valid_xml_document(): void |
| 34 | + { |
| 35 | + $xml = <<<XML |
| 36 | +<?xml version="1.0" encoding="UTF-8"?> |
| 37 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://example.com"> |
| 38 | + <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> |
| 39 | + <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://example.com/acs" index="0"/> |
| 40 | + </md:SPSSODescriptor> |
| 41 | +</md:EntityDescriptor> |
| 42 | +XML; |
| 43 | + |
| 44 | + $document = new DOMDocument(); |
| 45 | + $document->loadXML($xml); |
| 46 | + |
| 47 | + $errors = $this->validator->validate($document, __DIR__ . '/xsd/metadata.xsd'); |
| 48 | + |
| 49 | + self::assertEmpty($errors); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_is_valid_returns_true_for_valid_xml(): void |
| 53 | + { |
| 54 | + $xml = <<<XML |
| 55 | +<?xml version="1.0" encoding="UTF-8"?> |
| 56 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://example.com"> |
| 57 | + <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> |
| 58 | + <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://example.com/acs" index="0"/> |
| 59 | + </md:SPSSODescriptor> |
| 60 | +</md:EntityDescriptor> |
| 61 | +XML; |
| 62 | + |
| 63 | + $document = new DOMDocument(); |
| 64 | + $document->loadXML($xml); |
| 65 | + |
| 66 | + $isValid = $this->validator->isValid($document, __DIR__ . '/xsd/metadata.xsd'); |
| 67 | + |
| 68 | + self::assertTrue($isValid); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_detects_invalid_xml_document(): void |
| 72 | + { |
| 73 | + $xml = <<<XML |
| 74 | +<?xml version="1.0" encoding="UTF-8"?> |
| 75 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"> |
| 76 | + <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> |
| 77 | + <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://example.com/acs" index="0"/> |
| 78 | + </md:SPSSODescriptor> |
| 79 | +</md:EntityDescriptor> |
| 80 | +XML; |
| 81 | + |
| 82 | + $document = new DOMDocument(); |
| 83 | + $document->loadXML($xml); |
| 84 | + |
| 85 | + $errors = $this->validator->validate($document, __DIR__ . '/xsd/metadata.xsd'); |
| 86 | + |
| 87 | + self::assertNotEmpty($errors); |
| 88 | + self::assertIsArray($errors); |
| 89 | + } |
| 90 | + |
| 91 | + public function test_is_valid_returns_false_for_invalid_xml(): void |
| 92 | + { |
| 93 | + $xml = <<<XML |
| 94 | +<?xml version="1.0" encoding="UTF-8"?> |
| 95 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"> |
| 96 | + <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> |
| 97 | + <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://example.com/acs" index="0"/> |
| 98 | + </md:SPSSODescriptor> |
| 99 | +</md:EntityDescriptor> |
| 100 | +XML; |
| 101 | + |
| 102 | + $document = new DOMDocument(); |
| 103 | + $document->loadXML($xml); |
| 104 | + |
| 105 | + $isValid = $this->validator->isValid($document, __DIR__ . '/xsd/metadata.xsd'); |
| 106 | + |
| 107 | + self::assertFalse($isValid); |
| 108 | + } |
| 109 | + |
| 110 | + public function test_returns_error_for_missing_xsd_file(): void |
| 111 | + { |
| 112 | + $xml = <<<XML |
| 113 | +<?xml version="1.0" encoding="UTF-8"?> |
| 114 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://example.com"> |
| 115 | +</md:EntityDescriptor> |
| 116 | +XML; |
| 117 | + |
| 118 | + $document = new DOMDocument(); |
| 119 | + $document->loadXML($xml); |
| 120 | + |
| 121 | + $errors = $this->validator->validate($document, __DIR__ . '/xsd/non-existent.xsd'); |
| 122 | + |
| 123 | + self::assertNotEmpty($errors); |
| 124 | + self::assertStringContainsString('not found', $errors[0]); |
| 125 | + } |
| 126 | +} |
| 127 | + |
0 commit comments