From 278e0e02d1d074248b6bda82993d1be902e1c503 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Tue, 31 Jan 2023 20:55:47 +0100 Subject: [PATCH] Implement AssertionIDRef element (#318) --- src/SAML2/XML/saml/AssertionIDRef.php | 76 +++++++++++++++++++++ tests/SAML2/XML/saml/AssertionIDRefTest.php | 71 +++++++++++++++++++ tests/resources/xml/saml_AssertionIDRef.xml | 1 + 3 files changed, 148 insertions(+) create mode 100644 src/SAML2/XML/saml/AssertionIDRef.php create mode 100644 tests/SAML2/XML/saml/AssertionIDRefTest.php create mode 100644 tests/resources/xml/saml_AssertionIDRef.xml diff --git a/src/SAML2/XML/saml/AssertionIDRef.php b/src/SAML2/XML/saml/AssertionIDRef.php new file mode 100644 index 000000000..1581e6fd9 --- /dev/null +++ b/src/SAML2/XML/saml/AssertionIDRef.php @@ -0,0 +1,76 @@ +setContent($content); + } + + + /** + * Validate the content of the element. + * + * @param string $content The value to go in the XML textContent + * @throws \Exception on failure + * @return void + */ + protected function validateContent(string $content): void + { + Assert::validNCName($content, SchemaViolationException::class); // Covers the empty string + } + + + /** + * Convert XML into an AssertionIDRef + * + * @param \DOMElement $xml The XML element we should load + * @return static + * + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException + * If the qualified name of the supplied element is wrong + */ + public static function fromXML(DOMElement $xml): static + { + Assert::same($xml->localName, 'AssertionIDRef', InvalidDOMElementException::class); + Assert::same($xml->namespaceURI, AssertionIDRef::NS, InvalidDOMElementException::class); + + return new static($xml->textContent); + } + + + /** + * Convert this AssertionIDRef to XML. + * + * @param \DOMElement $parent The element we are converting to XML. + * @return \DOMElement The XML element after adding the data corresponding to this AssertionIDRef. + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $element = $this->instantiateParentElement($parent); + $element->textContent = $this->getContent(); + + return $element; + } +} diff --git a/tests/SAML2/XML/saml/AssertionIDRefTest.php b/tests/SAML2/XML/saml/AssertionIDRefTest.php new file mode 100644 index 000000000..b22c99fc3 --- /dev/null +++ b/tests/SAML2/XML/saml/AssertionIDRefTest.php @@ -0,0 +1,71 @@ +schema = dirname(__FILE__, 5) . '/schemas/saml-schema-assertion-2.0.xsd'; + + $this->testedClass = AssertionIDRef::class; + + $this->xmlRepresentation = DOMDocumentFactory::fromFile( + dirname(__FILE__, 4) . '/resources/xml/saml_AssertionIDRef.xml', + ); + } + + + /** + */ + public function testMarshalling(): void + { + $assertionIDRef = new AssertionIDRef('_Test'); + + $assertionIDRefElement = $assertionIDRef->toXML(); + $this->assertEquals('_Test', $assertionIDRefElement->textContent); + + $this->assertEquals( + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), + strval($assertionIDRef), + ); + } + + + /** + */ + public function testUnmarshalling(): void + { + $assertionIDRef = AssertionIDRef::fromXML($this->xmlRepresentation->documentElement); + + $this->assertEquals( + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), + strval($assertionIDRef), + ); + } +} diff --git a/tests/resources/xml/saml_AssertionIDRef.xml b/tests/resources/xml/saml_AssertionIDRef.xml new file mode 100644 index 000000000..e6d7c593a --- /dev/null +++ b/tests/resources/xml/saml_AssertionIDRef.xml @@ -0,0 +1 @@ +_Test