function JsonApiRegressionTest::testFilterByIdFromIssue3015759

Same name and namespace in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testFilterByIdFromIssue3015759()

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

See also

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

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 629

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->getAccountName(),
            $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());
}

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