You are here

public function NodeTest::testPatchPath in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPatchPath()
  2. 9 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPatchPath()

Tests PATCHing a node's path with and without 'create url aliases'.

For a positive test, see the similar test coverage for Term.

See also

\Drupal\Tests\jsonapi\Functional\TermTest::testPatchPath()

\Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase::testPatchPath()

File

core/modules/jsonapi/tests/src/Functional/NodeTest.php, line 279

Class

NodeTest
JSON:API integration test for the "Node" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchPath() {
  $this
    ->setUpAuthorization('GET');
  $this
    ->setUpAuthorization('PATCH');
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    'entity' => $this->entity
      ->uuid(),
  ]);

  // $url = $this->entity->toUrl('jsonapi');
  // GET node's current normalization.
  $response = $this
    ->request('GET', $url, $this
    ->getAuthenticationRequestOptions());
  $normalization = Json::decode((string) $response
    ->getBody());

  // Change node's path alias.
  $normalization['data']['attributes']['path']['alias'] .= 's-rule-the-world';

  // Create node PATCH request.
  $request_options = $this
    ->getAuthenticationRequestOptions();
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options[RequestOptions::BODY] = Json::encode($normalization);

  // PATCH request: 403 when creating URL aliases unauthorized.
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(403, "The current user is not allowed to PATCH the selected field (path). The following permissions are required: 'create url aliases' OR 'administer url aliases'.", $url, $response, '/data/attributes/path');

  // Grant permission to create URL aliases.
  $this
    ->grantPermissionsToTestedRole([
    'create url aliases',
  ]);

  // Repeat PATCH request: 200.
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceResponse(200, FALSE, $response);
  $updated_normalization = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame($normalization['data']['attributes']['path']['alias'], $updated_normalization['data']['attributes']['path']['alias']);
}