File tree Expand file tree Collapse file tree 5 files changed +29
-17
lines changed
Expand file tree Collapse file tree 5 files changed +29
-17
lines changed Original file line number Diff line number Diff line change 1414use function file_exists ;
1515use function implode ;
1616use function libxml_get_errors ;
17- use function restore_error_handler ;
18- use function set_error_handler ;
1917use function sprintf ;
2018use function trim ;
2119
@@ -35,22 +33,11 @@ trait SchemaValidatableElementTrait
3533 public static function schemaValidate (DOMDocument $ document ): DOMDocument
3634 {
3735 $ schemaFile = self ::getSchemaFile ();
36+ // Must suppress the warnings here in order to throw them as an error below.
37+ $ result = @$ document ->schemaValidate ($ schemaFile );
3838
39- // Dirty trick to catch the warnings emitted by XML-DOMs schemaValidate
40- // This will turn the warning into an exception
41- set_error_handler (static function (int $ errno , string $ errstr ): never {
42- throw new SchemaViolationException ($ errstr , $ errno );
43- }, E_WARNING );
44-
45- try {
46- $ result = $ document ->schemaValidate ($ schemaFile );
47- } finally {
48- // Restore the error handler, whether we throw an exception or not
49- restore_error_handler ();
50- }
51-
52- $ msgs = [];
5339 if ($ result === false ) {
40+ $ msgs = [];
5441 foreach (libxml_get_errors () as $ err ) {
5542 $ msgs [] = trim ($ err ->message ) . ' on line ' . $ err ->line ;
5643 }
Original file line number Diff line number Diff line change 66
77use SimpleSAML \XML \AbstractElement ;
88use SimpleSAML \XML \URIElementTrait ;
9+ use SimpleSAML \XML \SchemaValidatableElementInterface ;
10+ use SimpleSAML \XML \SchemaValidatableElementTrait ;
911
1012/**
1113 * Empty shell class for testing URIElement.
1214 *
1315 * @package simplesaml/xml-common
1416 */
15- final class URIElement extends AbstractElement
17+ final class URIElement extends AbstractElement implements SchemaValidatableElementInterface
1618{
19+ use SchemaValidatableElementTrait;
1720 use URIElementTrait;
1821
1922 /** @var string */
@@ -22,6 +25,9 @@ final class URIElement extends AbstractElement
2225 /** @var string */
2326 public const NS_PREFIX = 'ssp ' ;
2427
28+ /** @var string */
29+ public const SCHEMA = 'tests/resources/schemas/simplesamlphp.xsd ' ;
30+
2531
2632 /**
2733 * @param string $content
Original file line number Diff line number Diff line change @@ -32,6 +32,16 @@ public function testSchemaValidationPasses(): void
3232 }
3333
3434
35+ public function testSchemaValidationFails (): void
36+ {
37+ $ file = 'tests/resources/xml/invalid_ExtendableElement.xml ' ;
38+ $ chunk = DOMDocumentFactory::fromFile ($ file );
39+
40+ $ this ->expectException (SchemaViolationException::class);
41+ $ document = StringElement::schemaValidate ($ chunk );
42+ }
43+
44+
3545 public function testSchemaValidationWrongElementFails (): void
3646 {
3747 $ file = 'tests/resources/xml/ssp_Base64Element.xml ' ;
Original file line number Diff line number Diff line change 4747
4848<!-- End StringElement -->
4949
50+ <!-- Start URIElement -->
51+
52+ <element name =" URIElement" type =" anyURI" />
53+
54+ <!-- End URIElement -->
55+
5056<!-- Start ExtendableAttributesElement -->
5157
5258<element name =" ExtendableAttributesElement" type =" ssp:ExtendableAttributesElementType" />
Original file line number Diff line number Diff line change 1+ <ssp : ExtendableElement xmlns : ssp =" urn:x-simplesamlphp:namespace" >
2+ <dummy : Chunk xmlns : dummy =" urn:custom:dummy" >some</dummy : Chunk >
3+ </ssp : ExtendableElement >
You can’t perform that action at this time.
0 commit comments