Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AssertionURIRef element (#319)
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML2\XML\saml; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\StringElementTrait; | ||
|
||
/** | ||
* Class representing a saml:AssertionURIRef element. | ||
* | ||
* @package simplesaml/saml2 | ||
*/ | ||
final class AssertionURIRef extends AbstractSamlElement | ||
{ | ||
use StringElementTrait; | ||
|
||
|
||
/** | ||
* @param string $content | ||
*/ | ||
public function __construct(string $content) | ||
{ | ||
$this->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::validURI($content, SchemaViolationException::class); // Covers the empty string | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into an AssertionURIRef | ||
* | ||
* @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, 'AssertionURIRef', InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, AssertionURIRef::NS, InvalidDOMElementException::class); | ||
|
||
return new static($xml->textContent); | ||
} | ||
|
||
|
||
/** | ||
* Convert this AssertionURIRef to XML. | ||
* | ||
* @param \DOMElement $parent The element we are converting to XML. | ||
* @return \DOMElement The XML element after adding the data corresponding to this Condition. | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$element = $this->instantiateParentElement($parent); | ||
$element->textContent = $this->getContent(); | ||
|
||
return $element; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML2\Test\SAML2\XML\saml; | ||
|
||
use DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\SAML2\XML\saml\AssertionURIRef; | ||
use SimpleSAML\Test\XML\SchemaValidationTestTrait; | ||
use SimpleSAML\Test\XML\SerializableElementTestTrait; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\SAML2\XML\saml\AssertionURIRefTest | ||
* | ||
* @covers \SimpleSAML\SAML2\XML\saml\AssertionURIRef | ||
* @covers \SimpleSAML\SAML2\XML\saml\AbstractSamlElement | ||
* | ||
* @package simplesamlphp/saml2 | ||
*/ | ||
final class AssertionURIRefTest extends TestCase | ||
{ | ||
use SchemaValidationTestTrait; | ||
use SerializableElementTestTrait; | ||
|
||
/** | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->schema = dirname(__FILE__, 5) . '/schemas/saml-schema-assertion-2.0.xsd'; | ||
|
||
$this->testedClass = AssertionURIRef::class; | ||
|
||
$this->xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 4) . '/resources/xml/saml_AssertionURIRef.xml', | ||
); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$assertionURIRef = new AssertionURIRef('urn:x-simplesamlphp:reference'); | ||
|
||
$assertionURIRefElement = $assertionURIRef->toXML(); | ||
$this->assertEquals('urn:x-simplesamlphp:reference', $assertionURIRefElement->textContent); | ||
|
||
$this->assertEquals( | ||
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), | ||
strval($assertionURIRef), | ||
); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testUnmarshalling(): void | ||
{ | ||
$assertionURIRef = AssertionURIRef::fromXML($this->xmlRepresentation->documentElement); | ||
|
||
$this->assertEquals( | ||
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), | ||
strval($assertionURIRef), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<saml:AssertionURIRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:x-simplesamlphp:reference</saml:AssertionURIRef> |