Skip to content

Commit

Permalink
Bugfix: wsa:RelatesTo can have AnyURI content
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 11, 2024
1 parent 49bdee1 commit 6e4a83a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
20 changes: 7 additions & 13 deletions src/XML/wsa_200508/RelatesTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\URIElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function is_null;

/**
* Class representing a wsa:RelatesTo element.
*
Expand All @@ -21,6 +20,7 @@
final class RelatesTo extends AbstractWsaElement
{
use ExtendableAttributesTrait;
use URIElementTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
Expand All @@ -29,15 +29,18 @@ final class RelatesTo extends AbstractWsaElement
/**
* Initialize a wsa:RelatesTo
*
* @param string $content
* @param string|null $RelationshipType
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
public function __construct(
string $content,
protected ?string $RelationshipType = 'http://www.w3.org/2005/08/addressing/reply',
array $namespacedAttributes = [],
) {
Assert::nullOrValidURI($RelationshipType, SchemaViolationException::class);

$this->setContent($content);
$this->setAttributesNS($namespacedAttributes);
}

Expand All @@ -53,17 +56,6 @@ public function getRelationshipType(): ?string
}


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


/*
* Convert XML into an RelatesTo element
*
Expand All @@ -79,6 +71,7 @@ public static function fromXML(DOMElement $xml): static
Assert::same($xml->namespaceURI, RelatesTo::NS, InvalidDOMElementException::class);

return new static(
$xml->textContent,
self::getOptionalAttribute($xml, 'RelationshipType', null),
self::getAttributesNSFromXML($xml),
);
Expand All @@ -94,6 +87,7 @@ public static function fromXML(DOMElement $xml): static
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = $this->getContent();

if ($this->getRelationshipType() !== null) {
$e->setAttribute('RelationshipType', $this->getRelationshipType());
Expand Down
20 changes: 5 additions & 15 deletions tests/WSSecurity/XML/wsa_200508/RelatesToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,15 @@ public function testMarshalling(): void
{
$domAttr = new Attribute('urn:test:something', 'test', 'attr1', 'testval1');

$relatesTo = new RelatesTo('http://www.w3.org/2005/08/addressing/reply', [$domAttr]);
$this->assertFalse($relatesTo->isEmptyElement());

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($relatesTo),
$relatesTo = new RelatesTo(
'urn:x-simplesamlphp:namespace',
'http://www.w3.org/2005/08/addressing/reply',
[$domAttr],
);
}


/**
*/
public function testMarshallingWithNoContent(): void
{
$relatesTo = new RelatesTo(null, []);
$this->assertEquals(
'<wsa10:RelatesTo xmlns:wsa10="http://www.w3.org/2005/08/addressing"/>',
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($relatesTo),
);
$this->assertTrue($relatesTo->isEmptyElement());
}
}
2 changes: 1 addition & 1 deletion tests/resources/xml/wsa/200508/RelatesTo.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<wsa10:RelatesTo xmlns:wsa10="http://www.w3.org/2005/08/addressing" RelationshipType="http://www.w3.org/2005/08/addressing/reply" xmlns:test="urn:test:something" test:attr1="testval1" />
<wsa10:RelatesTo xmlns:wsa10="http://www.w3.org/2005/08/addressing" RelationshipType="http://www.w3.org/2005/08/addressing/reply" xmlns:test="urn:test:something" test:attr1="testval1">urn:x-simplesamlphp:namespace</wsa10:RelatesTo>

0 comments on commit 6e4a83a

Please sign in to comment.