You are here

class CustomQueryParameterNamesAccessCheckTest in JSON:API 8

@coversDefaultClass \Drupal\jsonapi\Access\CustomQueryParameterNamesAccessCheck @group jsonapi

@internal

Hierarchy

Expanded class hierarchy of CustomQueryParameterNamesAccessCheckTest

File

tests/src/Unit/Access/CustomQueryParameterNamesAccessCheckTest.php, line 15

Namespace

Drupal\Tests\jsonapi\Unit\Access
View source
class CustomQueryParameterNamesAccessCheckTest extends UnitTestCase {

  /**
   * Ensures that query params are properly validated.
   *
   * @dataProvider providerTestAccess
   * @covers ::access
   * @covers ::validate
   */
  public function testAccess($name, $valid) {
    $access_checker = new CustomQueryParameterNamesAccessCheck();
    $request = new Request();
    $request->attributes
      ->set('_json_api_params', [
      $name => '123',
    ]);
    $result = $access_checker
      ->access($request);
    if ($valid) {
      $this
        ->assertTrue($result
        ->isAllowed());
    }
    else {
      $this
        ->assertFalse($result
        ->isAllowed());
    }
  }

  /**
   * Data provider for testAccess.
   */
  public function providerTestAccess() {
    $data = [];
    $data['Official query parameter: sort'] = [
      'sort',
      TRUE,
    ];
    $data['Official query parameter: page'] = [
      'page',
      TRUE,
    ];
    $data['Official query parameter: filter'] = [
      'filter',
      TRUE,
    ];
    $data['Valid member, but invalid custom query parameter'] = [
      'foobar',
      FALSE,
    ];
    $data['Valid custom query parameter: dash'] = [
      'foo-bar',
      TRUE,
    ];
    $data['Valid custom query parameter: underscore'] = [
      'foo_bar',
      TRUE,
    ];
    $data['Valid custom query parameter: camelcase'] = [
      'fooBar',
      TRUE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomQueryParameterNamesAccessCheckTest::providerTestAccess public function Data provider for testAccess.
CustomQueryParameterNamesAccessCheckTest::testAccess public function Ensures that query params are properly validated.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340