class JsonApiSpecTest

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/JsonApiSpecTest.php \Drupal\Tests\jsonapi\Unit\JsonApiSpecTest
  2. 8.9.x core/modules/jsonapi/tests/src/Unit/JsonApiSpecTest.php \Drupal\Tests\jsonapi\Unit\JsonApiSpecTest
  3. 10 core/modules/jsonapi/tests/src/Unit/JsonApiSpecTest.php \Drupal\Tests\jsonapi\Unit\JsonApiSpecTest

@coversDefaultClass \Drupal\jsonapi\JsonApiSpec @group jsonapi

@internal

Hierarchy

  • class \Drupal\Tests\jsonapi\Unit\JsonApiSpecTest extends \Drupal\Tests\UnitTestCase

Expanded class hierarchy of JsonApiSpecTest

File

core/modules/jsonapi/tests/src/Unit/JsonApiSpecTest.php, line 18

Namespace

Drupal\Tests\jsonapi\Unit
View source
class JsonApiSpecTest extends UnitTestCase {
    
    /**
     * Ensures that member names are properly validated.
     *
     * @dataProvider providerTestIsValidMemberName
     * @covers ::isValidMemberName
     */
    public function testIsValidMemberName($member_name, $expected) : void {
        $this->assertSame($expected, JsonApiSpec::isValidMemberName($member_name));
    }
    
    /**
     * Data provider for testIsValidMemberName.
     */
    public static function providerTestIsValidMemberName() {
        // Copied from http://jsonapi.org/format/upcoming/#document-member-names.
        $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['low-line-start'] = [
            '_kittens',
            FALSE,
        ];
        $data['low-line-middle'] = [
            'kitt_ens',
            TRUE,
        ];
        $data['low-line-end'] = [
            'kittens_',
            FALSE,
        ];
        $data['space-start'] = [
            ' kittens',
            FALSE,
        ];
        $data['space-middle'] = [
            'kitt ens',
            TRUE,
        ];
        $data['space-end'] = [
            'kittens ',
            FALSE,
        ];
        // Additional test cases.
        // @todo When D8 requires PHP >= 7, convert to \u{10FFFF}.
        $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,
            ];
        }
        // The ASCII control characters are in the range 0x00 to 0x1F plus 0x7F.
        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;
    }
    
    /**
     * Provides test cases.
     *
     * @dataProvider providerTestIsValidCustomQueryParameter
     * @covers ::isValidCustomQueryParameter
     * @covers ::isValidMemberName
     */
    public function testIsValidCustomQueryParameter($custom_query_parameter, $expected) : void {
        $this->assertSame($expected, JsonApiSpec::isValidCustomQueryParameter($custom_query_parameter));
    }
    
    /**
     * Data provider for testIsValidCustomQueryParameter.
     */
    public static function providerTestIsValidCustomQueryParameter() {
        $data = static::providerTestIsValidMemberName();
        // All valid member names are also valid custom query parameters, except for
        // single-character ones.
        $data['single-character'][1] = FALSE;
        // Custom query parameter test cases.
        $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-camel-case'] = [
            'fooBar',
            TRUE,
        ];
        return $data;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
JsonApiSpecTest::providerTestIsValidCustomQueryParameter public static function Data provider for testIsValidCustomQueryParameter.
JsonApiSpecTest::providerTestIsValidMemberName public static function Data provider for testIsValidMemberName.
JsonApiSpecTest::testIsValidCustomQueryParameter public function Provides test cases.
JsonApiSpecTest::testIsValidMemberName public function Ensures that member names are properly validated.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.