function JsonApiRegressionTest::testFilteringEntitiesByEntityReferenceTargetId

Tests that collections can be filtered by an entity reference target_id.

See also

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

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testFilteringEntitiesByEntityReferenceTargetId() {
    // Create two config entities to be the config targets of an entity
    // reference. In this case, the `roles` field.
    $role_llamalovers = $this->drupalCreateRole([], 'llamalovers', 'Llama Lovers');
    $role_catcuddlers = $this->drupalCreateRole([], 'catcuddlers', 'Cat Cuddlers');
    
    /** @var \Drupal\user\UserInterface[] $users */
    for ($i = 0; $i < 3; $i++) {
        // Create 3 users, one with the first role and two with the second role.
        $users[$i] = $this->drupalCreateUser();
        $users[$i]->addRole($i === 0 ? $role_llamalovers : $role_catcuddlers);
        $users[$i]->save();
        // For each user, create a node that is owned by that user. The node's
        // `uid` field will be used to test filtering by a content entity ID.
        Node::create([
            'type' => 'article',
            'uid' => $users[$i]->id(),
            'title' => 'Article created by ' . $users[$i]->uuid(),
        ])
            ->save();
    }
    // Create a user that will be used to execute the test HTTP requests.
    $account = $this->drupalCreateUser([
        'administer users',
        'bypass node access',
    ]);
    $request_options = [
        RequestOptions::AUTH => [
            $account->getAccountName(),
            $account->pass_raw,
        ],
    ];
    // Ensure that an entity can be filtered by a target machine name.
    $response = $this->request('GET', Url::fromUri('internal:/jsonapi/user/user?filter[roles.meta.drupal_internal__target_id]=llamalovers'), $request_options);
    $document = Json::decode((string) $response->getBody());
    $this->assertSame(200, $response->getStatusCode(), var_export($document, TRUE));
    // Only one user should have the first role.
    $this->assertCount(1, $document['data']);
    $this->assertSame($users[0]->uuid(), $document['data'][0]['id']);
    $response = $this->request('GET', Url::fromUri('internal:/jsonapi/user/user?sort=drupal_internal__uid&filter[roles.meta.drupal_internal__target_id]=catcuddlers'), $request_options);
    $document = Json::decode((string) $response->getBody());
    $this->assertSame(200, $response->getStatusCode(), var_export($document, TRUE));
    // Two users should have the second role. A sort is used on this request to
    // ensure a consistent ordering with different databases.
    $this->assertCount(2, $document['data']);
    $this->assertSame($users[1]->uuid(), $document['data'][0]['id']);
    $this->assertSame($users[2]->uuid(), $document['data'][1]['id']);
    // Ensure that an entity can be filtered by an target entity integer ID.
    $response = $this->request('GET', Url::fromUri('internal:/jsonapi/node/article?filter[uid.meta.drupal_internal__target_id]=' . $users[1]->id()), $request_options);
    $document = Json::decode((string) $response->getBody());
    $this->assertSame(200, $response->getStatusCode(), var_export($document, TRUE));
    // Only the node authored by the filtered user should be returned.
    $this->assertCount(1, $document['data']);
    $this->assertSame('Article created by ' . $users[1]->uuid(), $document['data'][0]['attributes']['title']);
}

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