View source
<?php
namespace Drupal\Tests\jsonapi\Unit;
use Drupal\jsonapi\JsonApiSpec;
use Drupal\Tests\UnitTestCase;
class JsonApiSpecTest extends UnitTestCase {
public function testIsValidMemberName($member_name, $expected) {
$this
->assertSame($expected, JsonApiSpec::isValidMemberName($member_name));
}
public function providerTestIsValidMemberName() {
$data = [];
$data['alphanumeric-lowercase'] = [
'12kittens',
TRUE,
];
$data['alphanumeric-uppercase'] = [
'12KITTENS',
TRUE,
];
$data['alphanumeric-mixed'] = [
'12KiTtEnS',
TRUE,
];
$data['unicode-above-u+0080'] = [
'12🐱🐱',
TRUE,
];
$data['hyphen-start'] = [
'-kittens',
FALSE,
];
$data['hyphen-middle'] = [
'kitt-ens',
TRUE,
];
$data['hyphen-end'] = [
'kittens-',
FALSE,
];
$data['lowline-start'] = [
'_kittens',
FALSE,
];
$data['lowline-middle'] = [
'kitt_ens',
TRUE,
];
$data['lowline-end'] = [
'kittens_',
FALSE,
];
$data['space-start'] = [
' kittens',
FALSE,
];
$data['space-middle'] = [
'kitt ens',
TRUE,
];
$data['space-end'] = [
'kittens ',
FALSE,
];
$data['unicode-above-u+0080-highest-allowed'] = [
"12",
TRUE,
];
$data['single-character'] = [
'a',
TRUE,
];
$unsafe_chars = [
'+',
',',
'.',
'[',
']',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'/',
':',
';',
'<',
'=',
'>',
'?',
'@',
'\\',
'^',
'`',
'{',
'|',
'}',
'~',
];
foreach ($unsafe_chars as $unsafe_char) {
$data['unsafe-' . $unsafe_char] = [
'kitt' . $unsafe_char . 'ens',
FALSE,
];
}
for ($ascii = 0; $ascii <= 0x1f; $ascii++) {
$data['unsafe-ascii-control-' . $ascii] = [
'kitt' . chr($ascii) . 'ens',
FALSE,
];
}
$data['unsafe-ascii-control-' . 0x7f] = [
'kitt' . chr(0x7f) . 'ens',
FALSE,
];
return $data;
}
public function testIsValidCustomQueryParameter($custom_query_parameter, $expected) {
$this
->assertSame($expected, JsonApiSpec::isValidCustomQueryParameter($custom_query_parameter));
}
public function providerTestIsValidCustomQueryParameter() {
$data = $this
->providerTestIsValidMemberName();
$data['single-character'][1] = FALSE;
$data['custom-query-parameter-lowercase'] = [
'foobar',
FALSE,
];
$data['custom-query-parameter-dash'] = [
'foo-bar',
TRUE,
];
$data['custom-query-parameter-underscore'] = [
'foo_bar',
TRUE,
];
$data['custom-query-parameter-camelcase'] = [
'fooBar',
TRUE,
];
return $data;
}
}