Skip to content

Commit

Permalink
Add mex classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 28, 2024
1 parent 4dc2ec5 commit 458a21b
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/XML/mex/Metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\mex;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class defining the Metadata element
*
* @package tvdijen/ws-security
*/
final class Metadata extends AbstractMexElement
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* Metadata constructor
*
* @param array<\SimpleSAML\WSSecurity\XML\mex\MetadataSection> $metadataSection
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected array $metadataSection = [],
array $children = [],
array $namespacedAttributes = []
) {
$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the child property.
*
* @return array<\SimpleSAML\WSSecurity\XML\mex\MetadataSection>
*/
public function getMetadataSection(): array
{
return $this->metadataSection;
}


/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @return bool
*/
public function isEmptyElement(): bool
{
return empty($this->getMetadataSection())
&& empty($this->getElements())
&& empty($this->getAttributesNS());
}


/**
* Create an instance of this object from its XML representation.
*
* @param \DOMElement $xml
* @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, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$children = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

$children[] = new Chunk($child);
}

return new static(
MetadataSection::getChildrenOfClass($xml),
$children,
self::getAttributesNSFromXML($xml),
);
}


/**
* Add this Metadata to an XML element.
*
* @param \DOMElement $parent The element we should append this Metadata to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

foreach ($this->getMetadataSection() as $metadataSection) {
$metadataSection->toXML($e);
}

foreach ($this->getElements() as $elt) {
if (!$elt->isEmptyElement()) {
$elt->toXML($e);
}
}

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
87 changes: 87 additions & 0 deletions tests/WSSecurity/XML/mex/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\mex;

use DOMElement;
use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\mex\Location;
use SimpleSAML\WSSecurity\XML\mex\Metadata;
use SimpleSAML\WSSecurity\XML\mex\MetadataSection;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Tests for mex:Metadata.
*
* @covers \SimpleSAML\WSSecurity\XML\mex\Metadata
* @covers \SimpleSAML\WSSecurity\XML\wsa\AbstractMexElement
* @package tvdijen/ws-security
*/
final class MetadataTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = Metadata::class;

self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/MetadataExchange.xsd';

self::$xmlRepresentation = DOMDocumentFactory::FromFile(
dirname(__FILE__, 4) . '/resources/xml/mex_Metadata.xml'
);
}


// test marshalling


/**
* Test creating an Metadata object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1');
$attr2 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', 'testval2');

$child = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
);

$metadataSection = new MetadataSection(
new Location('urn:x-simplesamlphp:namespace'),
'urn:x-simplesamlphp:namespace',
'urn:x-simplesamlphp:namespace',
[$attr2],
);

$metadata = new Metadata([$metadataSection], [new Chunk($child->documentElement)], [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($metadata)
);
}


/**
*/
public function testMarshallingEmpty(): void
{
$metadata = new Metadata();

$this->assertTrue($metadata->isEmptyElement());
}
}
6 changes: 6 additions & 0 deletions tests/resources/xml/mex_Metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<mex:Metadata xmlns:mex="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<mex:MetadataSection Dialect="urn:x-simplesamlphp:namespace" Identifier="urn:x-simplesamlphp:namespace" ssp:attr2="testval2">
<mex:Location>urn:x-simplesamlphp:namespace</mex:Location>
</mex:MetadataSection>
<ssp:Chunk>Some</ssp:Chunk>
</mex:Metadata>

0 comments on commit 458a21b

Please sign in to comment.