function NodeTest::testPatchPath

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPatchPath()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPatchPath()
  3. 10 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 283

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchPath() : void {
    $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 = $this->getDocumentFromResponse($response);
    // 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);
    $updated_normalization = $this->getDocumentFromResponse($response);
    $this->assertResourceResponse(200, FALSE, $response);
    $this->assertSame($normalization['data']['attributes']['path']['alias'], $updated_normalization['data']['attributes']['path']['alias']);
}

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