comparison core/modules/jsonapi/tests/src/Unit/JsonApiSpecTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Unit;
4
5 use Drupal\jsonapi\JsonApiSpec;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9 * @coversDefaultClass \Drupal\jsonapi\JsonApiSpec
10 * @group jsonapi
11 *
12 * @internal
13 */
14 class JsonApiSpecTest extends UnitTestCase {
15
16 /**
17 * Ensures that member names are properly validated.
18 *
19 * @dataProvider providerTestIsValidMemberName
20 * @covers ::isValidMemberName
21 */
22 public function testIsValidMemberName($member_name, $expected) {
23 $this->assertSame($expected, JsonApiSpec::isValidMemberName($member_name));
24 }
25
26 /**
27 * Data provider for testIsValidMemberName.
28 */
29 public function providerTestIsValidMemberName() {
30 // Copied from http://jsonapi.org/format/upcoming/#document-member-names.
31 $data = [];
32 $data['alphanumeric-lowercase'] = ['12kittens', TRUE];
33 $data['alphanumeric-uppercase'] = ['12KITTENS', TRUE];
34 $data['alphanumeric-mixed'] = ['12KiTtEnS', TRUE];
35 $data['unicode-above-u+0080'] = ['12🐱🐱', TRUE];
36 $data['hyphen-start'] = ['-kittens', FALSE];
37 $data['hyphen-middle'] = ['kitt-ens', TRUE];
38 $data['hyphen-end'] = ['kittens-', FALSE];
39 $data['lowline-start'] = ['_kittens', FALSE];
40 $data['lowline-middle'] = ['kitt_ens', TRUE];
41 $data['lowline-end'] = ['kittens_', FALSE];
42 $data['space-start'] = [' kittens', FALSE];
43 $data['space-middle'] = ['kitt ens', TRUE];
44 $data['space-end'] = ['kittens ', FALSE];
45
46 // Additional test cases.
47 // @todo When D8 requires PHP >= 7, convert to \u{10FFFF}.
48 $data['unicode-above-u+0080-highest-allowed'] = ["12􏿿", TRUE];
49 $data['single-character'] = ['a', TRUE];
50
51 $unsafe_chars = [
52 '+',
53 ',',
54 '.',
55 '[',
56 ']',
57 '!',
58 '"',
59 '#',
60 '$',
61 '%',
62 '&',
63 '\'',
64 '(',
65 ')',
66 '*',
67 '/',
68 ':',
69 ';',
70 '<',
71 '=',
72 '>',
73 '?',
74 '@',
75 '\\',
76 '^',
77 '`',
78 '{',
79 '|',
80 '}',
81 '~',
82 ];
83 foreach ($unsafe_chars as $unsafe_char) {
84 $data['unsafe-' . $unsafe_char] = ['kitt' . $unsafe_char . 'ens', FALSE];
85 }
86
87 // The ASCII control characters are in the range 0x00 to 0x1F plus 0x7F.
88 for ($ascii = 0; $ascii <= 0x1F; $ascii++) {
89 $data['unsafe-ascii-control-' . $ascii] = ['kitt' . chr($ascii) . 'ens', FALSE];
90 }
91 $data['unsafe-ascii-control-' . 0x7F] = ['kitt' . chr(0x7F) . 'ens', FALSE];
92
93 return $data;
94 }
95
96 /**
97 * Provides test cases.
98 *
99 * @dataProvider providerTestIsValidCustomQueryParameter
100 * @covers ::isValidCustomQueryParameter
101 * @covers ::isValidMemberName
102 */
103 public function testIsValidCustomQueryParameter($custom_query_parameter, $expected) {
104 $this->assertSame($expected, JsonApiSpec::isValidCustomQueryParameter($custom_query_parameter));
105 }
106
107 /**
108 * Data provider for testIsValidCustomQueryParameter.
109 */
110 public function providerTestIsValidCustomQueryParameter() {
111 $data = $this->providerTestIsValidMemberName();
112
113 // All valid member names are also valid custom query parameters, except for
114 // single-character ones.
115 $data['single-character'][1] = FALSE;
116
117 // Custom query parameter test cases.
118 $data['custom-query-parameter-lowercase'] = ['foobar', FALSE];
119 $data['custom-query-parameter-dash'] = ['foo-bar', TRUE];
120 $data['custom-query-parameter-underscore'] = ['foo_bar', TRUE];
121 $data['custom-query-parameter-camelcase'] = ['fooBar', TRUE];
122
123 return $data;
124 }
125
126 }