Skip to content

Commit

Permalink
Raise coverage & fix bug in auth:ClaimType
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 23, 2023
1 parent 0392ba5 commit 8cdb647
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/XML/auth/AbstractClaimType.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ public static function fromXML(DOMElement $xml): static

$otherValue = [];
foreach ($xml->childNodes as $child) {
if ($child->namespaceURI !== static::NS) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI !== static::NS) {
$otherValue[] = new Chunk($child);
}
}
Expand Down
124 changes: 124 additions & 0 deletions tests/WSSecurity/XML/auth/ClaimTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\auth;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\Utils\XPath;
use SimpleSAML\WSSecurity\XML\auth\ClaimType;
use SimpleSAML\WSSecurity\XML\auth\Description;
use SimpleSAML\WSSecurity\XML\auth\DisplayName;
use SimpleSAML\WSSecurity\XML\auth\DisplayValue;
use SimpleSAML\WSSecurity\XML\auth\Value;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Tests for auth:ClaimType.
*
* @covers \SimpleSAML\WSSecurity\XML\auth\ClaimType
* @covers \SimpleSAML\WSSecurity\XML\auth\AbstractClaimType
* @covers \SimpleSAML\WSSecurity\XML\auth\AbstractAuthElement
* @package tvdijen/ws-security
*/
final class ClaimTypeTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-authorization.xsd';

self::$testedClass = ClaimType::class;

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


// test marshalling


/**
* Test creating a ClaimType object from scratch.
*/
public function testMarshalling(): void
{
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');

Check failure on line 61 in tests/WSSecurity/XML/auth/ClaimTypeTest.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

tests/WSSecurity/XML/auth/ClaimTypeTest.php:61:34: UndefinedClass: Class, interface or enum named SimpleSAML\Test\WSSecurity\Constants does not exist (see https://psalm.dev/019)
$claimType = new ClaimType(
C::NAMESPACE,

Check failure on line 63 in tests/WSSecurity/XML/auth/ClaimTypeTest.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

tests/WSSecurity/XML/auth/ClaimTypeTest.php:63:13: UndefinedClass: Class, interface or enum named SimpleSAML\Test\WSSecurity\Constants does not exist (see https://psalm.dev/019)
true,
new DisplayName('someDisplayName'),
new Description('someDescription'),
new DisplayValue('someDisplayValue'),
new Value('someValue'),
[$attr],
);

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


/**
*/
public function testMarshallingElementOrdering(): void
{
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');

Check failure on line 83 in tests/WSSecurity/XML/auth/ClaimTypeTest.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

tests/WSSecurity/XML/auth/ClaimTypeTest.php:83:34: UndefinedClass: Class, interface or enum named SimpleSAML\Test\WSSecurity\Constants does not exist (see https://psalm.dev/019)
$claimType = new ClaimType(
C::NAMESPACE,

Check failure on line 85 in tests/WSSecurity/XML/auth/ClaimTypeTest.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

tests/WSSecurity/XML/auth/ClaimTypeTest.php:85:13: UndefinedClass: Class, interface or enum named SimpleSAML\Test\WSSecurity\Constants does not exist (see https://psalm.dev/019)
true,
new DisplayName('someDisplayName'),
new Description('someDescription'),
new DisplayValue('someDisplayValue'),
new Value('someValue'),
[$attr],
);
$claimTypeElement = $claimType->toXML();

// Test for a DisplayName
$xpCache = XPath::getXPath($claimTypeElement);

Check failure on line 96 in tests/WSSecurity/XML/auth/ClaimTypeTest.php

View workflow job for this annotation

GitHub Actions / Quality control

UndefinedClass

tests/WSSecurity/XML/auth/ClaimTypeTest.php:96:20: UndefinedClass: Class, interface or enum named SimpleSAML\WSSecurity\Utils\XPath does not exist (see https://psalm.dev/019)
$claimTypeElements = XPath::xpQuery($claimTypeElement, './auth:DisplayName', $xpCache);
$this->assertCount(1, $claimTypeElements);

// Test ordering of ClaimType contents
/** @psalm-var \DOMElement[] $claimTypeElements */
$claimTypeElements = XPath::xpQuery($claimTypeElement, './auth:DisplayName/following-sibling::*', $xpCache);
$this->assertCount(3, $claimTypeElements);
$this->assertEquals('auth:Description', $claimTypeElements[0]->tagName);
$this->assertEquals('auth:DisplayValue', $claimTypeElements[1]->tagName);
$this->assertEquals('auth:Value', $claimTypeElements[2]->tagName);
}


// test unmarshalling


/**
* Test creating a ClaimType from XML.
*/
public function testUnmarshalling(): void
{
$claimType = ClaimType::fromXML(self::$xmlRepresentation->documentElement);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($claimType),
);
}
}
6 changes: 6 additions & 0 deletions tests/resources/xml/auth_ClaimType.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<auth:ClaimType xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" Uri="urn:x-simplesamlphp:namespace" Optional="true" ssp:attr1="value1">
<auth:DisplayName>someDisplayName</auth:DisplayName>
<auth:Description>someDescription</auth:Description>
<auth:DisplayValue>someDisplayValue</auth:DisplayValue>
<auth:Value>someValue</auth:Value>
</auth:ClaimType>

0 comments on commit 8cdb647

Please sign in to comment.