You are here

public function JsonApiRegressionTest::testFilterByIdFromIssue3015759 in JSON:API 8.2

Ensures that filtering by a sequential internal ID named 'id' is possible.

See also

https://www.drupal.org/project/jsonapi/issues/3015759

File

tests/src/Functional/JsonApiRegressionTest.php, line 617

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testFilterByIdFromIssue3015759() {

  // Set up data model.
  $this
    ->assertTrue($this->container
    ->get('module_installer')
    ->install([
    'shortcut',
  ], TRUE), 'Installed modules.');
  $this
    ->rebuildAll();

  // Create data.
  $shortcut = Shortcut::create([
    'shortcut_set' => 'default',
    'title' => $this
      ->randomMachineName(),
    'weight' => -20,
    'link' => [
      'uri' => 'internal:/user/logout',
    ],
  ]);
  $shortcut
    ->save();

  // Test.
  $user = $this
    ->drupalCreateUser([
    'access shortcuts',
    'customize shortcut links',
  ]);
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/shortcut/default?filter[drupal_internal__id]=' . $shortcut
    ->id()), [
    RequestOptions::AUTH => [
      $user
        ->getUsername(),
      $user->pass_raw,
    ],
  ]);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $doc = Json::decode((string) $response
    ->getBody());
  $this
    ->assertNotEmpty($doc['data']);
  $this
    ->assertSame($doc['data'][0]['id'], $shortcut
    ->uuid());
  $this
    ->assertSame($doc['data'][0]['attributes']['drupal_internal__id'], (int) $shortcut
    ->id());
  $this
    ->assertSame($doc['data'][0]['attributes']['title'], $shortcut
    ->label());
}