From db3aa10537b33e7635ae2614004d13e78534417f Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Sun, 15 Sep 2024 17:52:09 +0200 Subject: [PATCH] Add element fed:FederationMetadata --- .../fed/AbstractFederationMetadataType.php | 38 +++++-------------- .../XML/fed/FederationMetadataTest.php | 32 +++++++++++++--- .../resources/xml/fed_FederationMetadata.xml | 8 ++-- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/XML/fed/AbstractFederationMetadataType.php b/src/XML/fed/AbstractFederationMetadataType.php index 6ec13cce..6deb207f 100644 --- a/src/XML/fed/AbstractFederationMetadataType.php +++ b/src/XML/fed/AbstractFederationMetadataType.php @@ -6,9 +6,7 @@ use DOMElement; use SimpleSAML\Assert\Assert; -use SimpleSAML\XML\Chunk; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\ExtendableAttributesTrait; use SimpleSAML\XML\ExtendableElementTrait; use SimpleSAML\XML\XsNamespace as NS; @@ -33,28 +31,27 @@ abstract class AbstractFederationMetadataType extends AbstractFedElement /** * AbstractFederationMetadataType constructor * - * @param \SimpleSAML\WSSecurity\XML\fed\Federation[] $federation - * @param \SimpleSAML\XML\SerializableElementInterface[] $children - * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes + * @param list<\SimpleSAML\XML\SerializableElementInterface> $children + * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes */ final public function __construct( - protected array $federation = [], array $children = [], array $namespacedAttributes = [], ) { - Assert::minCount($federation, 1, SchemaViolationException::class); - $this->setElements($children); $this->setAttributesNS($namespacedAttributes); } /** - * @return \SimpleSAML\WSSecurity\XML\fed\Federation[] + * Test if an object, at the state it's in, would produce an empty XML-element + * + * @return bool */ - public function getFederation(): array + public function isEmptyElement(): bool { - return $this->federation; + return empty($this->getElements()) + && empty($this->getAttributesNS()); } @@ -72,21 +69,8 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); - $children = $federation = []; - foreach ($xml->childNodes as $child) { - if (!($child instanceof DOMElement)) { - continue; - } elseif ($child->namespaceURI === static::NS && $child->localName === 'Federation') { - $federation[] = Federation::fromXML($child); - continue; - } - - $children[] = new Chunk($child); - } - return new static( - $federation, - $children, + self::getChildElementsFromXML($xml), self::getAttributesNSFromXML($xml), ); } @@ -102,10 +86,6 @@ public function toXML(DOMElement $parent = null): DOMElement { $e = parent::instantiateParentElement($parent); - foreach ($this->getFederation() as $fed) { - $fed->toXML($e); - } - foreach ($this->getAttributesNS() as $attr) { $attr->toXML($e); } diff --git a/tests/WSSecurity/XML/fed/FederationMetadataTest.php b/tests/WSSecurity/XML/fed/FederationMetadataTest.php index 4d5f4bf9..385926ca 100644 --- a/tests/WSSecurity/XML/fed/FederationMetadataTest.php +++ b/tests/WSSecurity/XML/fed/FederationMetadataTest.php @@ -39,10 +39,10 @@ final class FederationMetadataTest extends TestCase */ public static function setUpBeforeClass(): void { - self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-federation.xsd'; - self::$testedClass = FederationMetadata::class; + self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-federation.xsd'; + self::$xmlRepresentation = DOMDocumentFactory::fromFile( dirname(__FILE__, 4) . '/resources/xml/fed_FederationMetadata.xml', ); @@ -59,16 +59,36 @@ public function testMarshalling(): void { $attr1 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1'); $attr2 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', 'testval2'); - $child = DOMDocumentFactory::fromString( - 'SomeChunk', + $some = DOMDocumentFactory::fromString( + 'Some', + ); + $other = DOMDocumentFactory::fromString( + 'Other', + ); + + $federation = new Federation( + 'urn:some:uri', + [new Chunk($some->documentElement)], + [$attr1], ); - $federation = new Federation('urn:some:uri', [new Chunk($child->documentElement)], [$attr2]); - $federationMetadata = new FederationMetadata([$federation], [new Chunk($child->documentElement)], [$attr1]); + $federationMetadata = new FederationMetadata( + [$federation, new Chunk($other->documentElement)], + [$attr2], + ); $this->assertEquals( self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($federationMetadata), ); } + + + /** + */ + public function testMarshallingEmpty(): void + { + $federationMetadata = new FederationMetadata(); + $this->assertTrue($federationMetadata->isEmptyElement()); + } } diff --git a/tests/resources/xml/fed_FederationMetadata.xml b/tests/resources/xml/fed_FederationMetadata.xml index 22113910..811078fa 100644 --- a/tests/resources/xml/fed_FederationMetadata.xml +++ b/tests/resources/xml/fed_FederationMetadata.xml @@ -1,6 +1,6 @@ - - - SomeChunk + + + Some - SomeChunk + Other