You are here

public function JsonapiResourceTest::testAddReminderResource in JSON:API Resources 8

Tests the custom Add Reminder resource.

File

tests/src/Functional/JsonapiResourceTest.php, line 119

Class

JsonapiResourceTest
Tests JSON:API Resource processors.

Namespace

Drupal\Tests\jsonapi_resources\Functional

Code

public function testAddReminderResource() {
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $this
    ->grantPermissionsToTestedRole([
    'access content',
    'create reminder content',
  ]);
  $url = Url::fromUri(sprintf('internal:/jsonapi/user/%s/reminders', $this->account
    ->id()));
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());
  $request_options[RequestOptions::JSON] = [
    'data' => [
      'type' => 'node--reminder',
      'attributes' => [
        'title' => "Don't panic.",
      ],
    ],
  ];
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertSame(201, $response
    ->getStatusCode(), $response
    ->getBody());
  $this
    ->assertTrue($response
    ->hasHeader('Location'));
  $created_url = Url::fromUri($response
    ->getHeader('Location')[0]);
  $response = $this
    ->request('GET', $created_url, $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode(), $response
    ->getBody());
  $document = Json::decode((string) $response
    ->getBody());
  $exists = FALSE;
  $owner_id = NestedArray::getValue($document, explode('/', 'data/relationships/uid/data/id'), $exists);
  $this
    ->assertTrue($exists);
  $this
    ->assertSame(User::load($this->account
    ->id())
    ->uuid(), $owner_id);
}