Skip to content

Commit d10c5fe

Browse files
committed
Create assertion and type-class for xs:NCName
1 parent 0582bec commit d10c5fe

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

src/Assert/NCNameTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait NCNameTrait
13+
{
14+
/** @var string */
15+
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/Du';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validNCName(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$ncname_regex,
27+
$message ?: '%s is not a valid xs:NCName',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/NCNameTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\AssertionFailedException;
11+
use SimpleSAML\XML\Assert\Assert;
12+
13+
/**
14+
* Class \SimpleSAML\Test\XML\Assert\NCNameTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class NCNameTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $name
24+
*/
25+
#[DataProvider('provideNCName')]
26+
public function testValidNCName(bool $shouldPass, string $name): void
27+
{
28+
try {
29+
Assert::validNCName($name);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideNCName(): array
41+
{
42+
return [
43+
[true, 'Test'],
44+
// May start with an underscore
45+
[true, '_Test'],
46+
// May contain dashes
47+
[true, '_1950-10-04_10-00'],
48+
// May contain dots
49+
[true, 'Te.st'],
50+
// May contain diacriticals
51+
[true, 'fööbár'],
52+
// Prefixed v4 UUID
53+
[true, '_5425e58e-e799-4884-92cc-ca64ecede32f'],
54+
// An empty value is not valid, unless xsi:nil is used
55+
[false, ''],
56+
// Wildcards are not allowed
57+
[false, 'Te*st'],
58+
// May not start with a digit
59+
[false, '1Test'],
60+
// May not start with a dash
61+
[false, '-Test'],
62+
// May not contain a colon
63+
[false, 'Te:st'],
64+
// Trailing newlines are forbidden
65+
[false, "Test\n"],
66+
];
67+
}
68+
}

tests/Assert/NCNameValue.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class NCNameValue extends TokenValue
14+
{
15+
/**
16+
* Validate the value.
17+
*
18+
* @param string $value
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateValue(string $value): void
23+
{
24+
// Note: value must already be sanitized before validating
25+
Assert::validNCName($this->sanitizeValue($value), SchemaViolationException::class);
26+
}
27+
}

tests/Type/NCNameValueTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Type\NCNameValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\NCNameValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(NCNameValue::class)]
19+
final class NCNameValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $ncname
24+
*/
25+
#[DataProvider('provideNCName')]
26+
public function testNCName(bool $shouldPass, string $ncname): void
27+
{
28+
try {
29+
NCNameValue::fromString($ncname);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideNCName(): array
41+
{
42+
return [
43+
'valid' => [true, 'Test'],
44+
'valid starts with underscore' => [true, '_Test'],
45+
'valid contains dashes' => [true, '_1950-10-04_10-00'],
46+
'valid contains dots' => [true, 'Te.st'],
47+
'valid contains diacriticals' => [true, 'fööbár'],
48+
'valid prefixed v4 UUID' => [true, '_5425e58e-e799-4884-92cc-ca64ecede32f'],
49+
'invalid empty string' => [false, ''],
50+
'invalid contains wildcard' => [false, 'Te*st'],
51+
'invalid starts with dash' => [false, '-Test'],
52+
'invalid starts with digit' => [false, '1Test'],
53+
'invalid contains colon' => [false, 'Te:st'],
54+
'whitespace collapse' => [true, "foobar\n"],
55+
'normalization' => [true, ' foobar '],
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)