Passed
Pull Request — master (#60)
by Tim
02:12
created

SignatureTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 31
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 49
rs 9.424
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use PHPUnit\Framework\Attributes\{CoversClass, Group};
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\{Chunk, DOMDocumentFactory};
10
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
11
use SimpleSAML\XML\Type\IDValue;
12
use SimpleSAML\XMLSecurity\Utils\XPath;
13
use SimpleSAML\XMLSecurity\XML\ds\{AbstractDsElement, DsObject, KeyInfo};
14
use SimpleSAML\XMLSecurity\XML\ds\{Signature, SignatureValue, SignedInfo};
15
16
use function dirname;
17
use function strval;
18
19
/**
20
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\SignatureTest
21
 *
22
 * @package simplesamlphp/xml-security
23
 */
24
#[Group('ds')]
25
#[CoversClass(AbstractDsElement::class)]
26
#[CoversClass(Signature::class)]
27
final class SignatureTest extends TestCase
28
{
29
    use SchemaValidationTestTrait;
30
    use SerializableElementTestTrait;
31
32
    /**
33
     * Set up the test.
34
     */
35
    public static function setUpBeforeClass(): void
36
    {
37
        self::$testedClass = Signature::class;
38
39
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
40
            dirname(__FILE__, 3) . '/resources/xml/ds_Signature.xml',
41
        );
42
    }
43
44
45
    /**
46
     * Test creating a SignatureValue from scratch.
47
     */
48
    public function testMarshalling(): void
49
    {
50
        $signature = new Signature(
51
            SignedInfo::fromXML(
52
                DOMDocumentFactory::fromFile(
53
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
54
                )->documentElement,
55
            ),
56
            SignatureValue::fromXML(
57
                DOMDocumentFactory::fromFile(
58
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
59
                )->documentElement,
60
            ),
61
            KeyInfo::fromXML(
62
                DOMDocumentFactory::fromFile(
63
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
64
                )->documentElement,
65
            ),
66
            [
67
                new DsObject(
68
                    null,
69
                    null,
70
                    null,
71
                    [
72
                        new Chunk(
73
                            DOMDocumentFactory::fromString(
74
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
75
                            )->documentElement,
76
                        ),
77
                    ],
78
                ),
79
            ],
80
            IDValue::fromString('def456'),
81
        );
82
83
        $this->assertEquals(
84
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
85
            strval($signature),
86
        );
87
    }
88
89
90
    /**
91
     */
92
    public function testMarshallingElementOrdering(): void
93
    {
94
        $signature = new Signature(
95
            SignedInfo::fromXML(
96
                DOMDocumentFactory::fromFile(
97
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
98
                )->documentElement,
99
            ),
100
            SignatureValue::fromXML(
101
                DOMDocumentFactory::fromFile(
102
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
103
                )->documentElement,
104
            ),
105
            KeyInfo::fromXML(
106
                DOMDocumentFactory::fromFile(
107
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
108
                )->documentElement,
109
            ),
110
            [
111
                new DsObject(
112
                    null,
113
                    null,
114
                    null,
115
                    [
116
                        new Chunk(
117
                            DOMDocumentFactory::fromString(
118
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
119
                            )->documentElement,
120
                        ),
121
                    ],
122
                ),
123
            ],
124
            IDValue::fromString('def456'),
125
        );
126
127
        $signatureElement = $signature->toXML();
128
        $xpCache = XPath::getXPath($signatureElement);
129
130
        $signedInfo = XPath::xpQuery($signatureElement, './ds:SignedInfo', $xpCache);
131
        $this->assertCount(1, $signedInfo);
132
133
        /** @var \DOMElement[] $signatureElements */
134
        $signatureElements = XPath::xpQuery($signatureElement, './ds:SignedInfo/following-sibling::*', $xpCache);
135
136
        // Test ordering of Signature contents
137
        $this->assertCount(3, $signatureElements);
138
        $this->assertEquals('ds:SignatureValue', $signatureElements[0]->tagName);
139
        $this->assertEquals('ds:KeyInfo', $signatureElements[1]->tagName);
140
        $this->assertEquals('ds:Object', $signatureElements[2]->tagName);
141
    }
142
}
143