function NodeResourceTestBase::testPatchPath

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/Rest/NodeResourceTestBase.php \Drupal\Tests\node\Functional\Rest\NodeResourceTestBase::testPatchPath()
  2. 8.9.x core/modules/node/tests/src/Functional/Rest/NodeResourceTestBase.php \Drupal\Tests\node\Functional\Rest\NodeResourceTestBase::testPatchPath()
  3. 11.x core/modules/node/tests/src/Functional/Rest/NodeResourceTestBase.php \Drupal\Tests\node\Functional\Rest\NodeResourceTestBase::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\rest\Functional\EntityResource\Term\TermResourceTestBase::testPatchPath()

File

core/modules/node/tests/src/Functional/Rest/NodeResourceTestBase.php, line 254

Class

NodeResourceTestBase

Namespace

Drupal\Tests\node\Functional\Rest

Code

public function testPatchPath() : void {
  $this->initAuthentication();
  $this->provisionEntityResource();
  $this->setUpAuthorization('GET');
  $this->setUpAuthorization('PATCH');
  $url = $this->getEntityResourceUrl()
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  // GET node's current normalization.
  $response = $this->request('GET', $url, $this->getAuthenticationRequestOptions('GET'));
  $normalization = $this->serializer
    ->decode((string) $response->getBody(), static::$format);
  // Change node's path alias.
  $normalization['path'][0]['alias'] .= 's-rule-the-world';
  // Create node PATCH request.
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
  $request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('PATCH'));
  $request_options[RequestOptions::BODY] = $this->serializer
    ->encode($normalization, static::$format);
  // PATCH request: 403 when creating URL aliases unauthorized. Before
  // asserting the 403 response, assert that the stored path alias remains
  // unchanged.
  $response = $this->request('PATCH', $url, $request_options);
  $this->assertSame('/llama', $this->entityStorage
    ->loadUnchanged($this->entity
    ->id())
    ->get('path')->alias);
  $this->assertResourceErrorResponse(403, "Access denied on updating field 'path'. " . static::$patchProtectedFieldNames['path'], $response);
  // Make sure the role save below properly invalidates cache tags.
  $this->refreshVariables();
  // 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 = $this->serializer
    ->decode((string) $response->getBody(), static::$format);
  $this->assertSame($normalization['path'], $updated_normalization['path']);
}

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