public function NodeTest::testPatchPath in JSON:API 8
Same name and namespace in other branches
- 8.2 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
- tests/
src/ Functional/ NodeTest.php, line 244
Class
- NodeTest
- JSON API integration test for the "Node" content entity type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPatchPath() {
$this
->setUpAuthorization('GET');
$this
->setUpAuthorization('PATCH');
// @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463.
$url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
static::$entityTypeId => $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::BODY] = Json::encode($normalization);
// PATCH request: 403 when creating URL aliases unauthorized.
$response = $this
->request('PATCH', $url, $request_options);
// @todo Remove $expected + assertResourceResponse() in favor of the commented line below once https://www.drupal.org/project/jsonapi/issues/2943176 lands.
$expected_document = [
'errors' => [
[
'title' => 'Forbidden',
'status' => 403,
'detail' => "The current user is not allowed to PATCH the selected field (path). The following permissions are required: 'create url aliases' OR 'administer url aliases'.",
'links' => [
'info' => HttpExceptionNormalizer::getInfoUrl(403),
],
'code' => 0,
'id' => '/node--camelids/' . $this->entity
->uuid(),
'source' => [
'pointer' => '/data/attributes/path',
],
],
],
];
$this
->assertResourceResponse(403, $expected_document, $response);
/* $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'.", $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']);
}