|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\SAML2\XML\ds; |
| 6 | + |
| 7 | +use DOMDocument; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\SAML2\Constants; |
| 10 | +use SimpleSAML\Test\XML\SerializableXMLTestTrait; |
| 11 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 12 | +use SimpleSAML\XML\Chunk; |
| 13 | +use SimpleSAML\XML\Utils as XMLUtils; |
| 14 | +use SimpleSAML\XMLSecurity\XML\ds\Transform; |
| 15 | +use SimpleSAML\XMLSecurity\XML\ds\Transforms; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class \SimpleSAML\XMLSecurity\Test\XML\ds\TransformsTest |
| 19 | + * |
| 20 | + * @covers \SimpleSAML\XMLSecurity\XML\ds\Transforms |
| 21 | + * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement |
| 22 | + * |
| 23 | + * @package simplesamlphp/xml-security |
| 24 | + */ |
| 25 | +final class TransformsTest extends TestCase |
| 26 | +{ |
| 27 | + use SerializableXMLTestTrait; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + */ |
| 32 | + public function setUp(): void |
| 33 | + { |
| 34 | + $this->testedClass = Transforms::class; |
| 35 | + |
| 36 | + $this->xmlRepresentation = DOMDocumentFactory::fromFile( |
| 37 | + dirname(dirname(dirname(__FILE__))) . '/resources/xml/ds_Transforms.xml' |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + */ |
| 44 | + public function testMarshalling(): void |
| 45 | + { |
| 46 | + $transforms = new Transforms( |
| 47 | + [ |
| 48 | + new Transform( |
| 49 | + 'http://www.w3.org/TR/1999/REC-xpath-19991116', |
| 50 | + [ |
| 51 | + new Chunk( |
| 52 | + DOMDocumentFactory::fromString( |
| 53 | + '<ds:XPath xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">self::xenc:CipherValue[@Id="example1"]</ds:XPath>' |
| 54 | + )->documentElement |
| 55 | + ) |
| 56 | + ] |
| 57 | + ) |
| 58 | + ] |
| 59 | + ); |
| 60 | + |
| 61 | + $this->assertEquals( |
| 62 | + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), |
| 63 | + strval($transforms) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + */ |
| 70 | + public function testUnmarshalling(): void |
| 71 | + { |
| 72 | + $transforms = Transforms::fromXML($this->xmlRepresentation->documentElement); |
| 73 | + $transform = $transforms->getTransform(); |
| 74 | + $this->assertCount(1, $transform); |
| 75 | + |
| 76 | + $transform = array_pop($transform); |
| 77 | + $this->assertEquals('http://www.w3.org/TR/1999/REC-xpath-19991116', $transform->getAlgorithm()); |
| 78 | + |
| 79 | + $elements = $transform->getElements(); |
| 80 | + $this->assertCount(1, $elements); |
| 81 | + |
| 82 | + $this->assertInstanceOf(Chunk::class, $elements[0]); |
| 83 | + |
| 84 | + $this->assertEquals( |
| 85 | + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), |
| 86 | + strval($transforms) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Adding an empty Transforms element should yield an empty element. |
| 93 | + */ |
| 94 | + public function testMarshallingEmptyElement(): void |
| 95 | + { |
| 96 | + $ds_ns = Transforms::NS; |
| 97 | + $transforms = new Transforms([]); |
| 98 | + $this->assertEquals( |
| 99 | + "<ds:Transforms xmlns:ds=\"$ds_ns\"/>", |
| 100 | + strval($transforms) |
| 101 | + ); |
| 102 | + $this->assertTrue($transforms->isEmptyElement()); |
| 103 | + } |
| 104 | +} |
0 commit comments