Skip to content

Commit

Permalink
Add element fed:FederationMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 15, 2024
1 parent 1eec9db commit db3aa10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
38 changes: 9 additions & 29 deletions src/XML/fed/AbstractFederationMetadataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}


Expand All @@ -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),
);
}
Expand All @@ -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);
}
Expand Down
32 changes: 26 additions & 6 deletions tests/WSSecurity/XML/fed/FederationMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand All @@ -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(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">SomeChunk</ssp:Chunk>',
$some = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
);
$other = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Other</ssp:Chunk>',
);

$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());
}
}
8 changes: 4 additions & 4 deletions tests/resources/xml/fed_FederationMetadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<fed:FederationMetadata xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<fed:Federation FederationID="urn:some:uri" ssp:attr2="testval2">
<ssp:Chunk>SomeChunk</ssp:Chunk>
<fed:FederationMetadata xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr2="testval2">
<fed:Federation xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" FederationID="urn:some:uri" ssp:attr1="testval1">
<ssp:Chunk>Some</ssp:Chunk>
</fed:Federation>
<ssp:Chunk>SomeChunk</ssp:Chunk>
<ssp:Chunk>Other</ssp:Chunk>
</fed:FederationMetadata>

0 comments on commit db3aa10

Please sign in to comment.