public function JsonapiResourceTest::testAuthorContentResource in JSON:API Resources 8
Tests the Author Content resource.
File
- tests/
src/ Functional/ JsonapiResourceTest.php, line 194
Class
- JsonapiResourceTest
- Tests JSON:API Resource processors.
Namespace
Drupal\Tests\jsonapi_resources\FunctionalCode
public function testAuthorContentResource() {
$author_user = $this->account;
$node1 = Node::create([
'type' => 'article',
'title' => $this
->randomString(),
'status' => 1,
'uid' => $author_user
->id(),
]);
$node1
->save();
$node2 = Node::create([
'type' => 'article',
'title' => $this
->randomString(),
'status' => 1,
'uid' => $author_user
->id(),
]);
$node2
->save();
$this
->grantPermissionsToTestedRole([
'access content',
]);
$url = Url::fromUri(sprintf('internal:/jsonapi/user/%s/content', $author_user
->id()));
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions());
$response = $this
->request('GET', $url, $request_options);
$this
->assertSame(200, $response
->getStatusCode(), var_export(Json::decode((string) $response
->getBody()), TRUE));
$response_document = Json::decode((string) $response
->getBody());
$this
->assertCount(2, $response_document['data']);
$this
->assertArrayHasKey('included', $response_document);
$this
->assertNotEmpty($response_document['included']);
$url = Url::fromUri(sprintf('internal:/jsonapi/user/%s/content?page[limit]=1', $author_user
->id()));
$response = $this
->request('GET', $url, $request_options);
$this
->assertSame(200, $response
->getStatusCode(), var_export(Json::decode((string) $response
->getBody()), TRUE));
$response_document = Json::decode((string) $response
->getBody());
$this
->assertCount(1, $response_document['data']);
$this
->assertArrayHasKey('included', $response_document);
$this
->assertNotEmpty($response_document['included']);
$this
->assertArrayHasKey('next', $response_document['links']);
$this
->assertArrayHasKey('last', $response_document['links']);
$this
->assertSame($node1
->uuid(), $response_document['data'][0]['id']);
$url = Url::fromUri($response_document['links']['next']['href']);
$response = $this
->request('GET', $url, $request_options);
$this
->assertSame(200, $response
->getStatusCode(), var_export(Json::decode((string) $response
->getBody()), TRUE));
$response_document = Json::decode((string) $response
->getBody());
$this
->assertCount(1, $response_document['data']);
$this
->assertSame($node2
->uuid(), $response_document['data'][0]['id']);
}