function JsonApiPatchRegressionTest::testPatchInvalidFieldPropertyFromIssue3127883

Same name in other branches
  1. 10 core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiPatchRegressionTest::testPatchInvalidFieldPropertyFromIssue3127883()

Ensure PATCHing a non-existing field property results in a helpful error.

See also

https://www.drupal.org/project/drupal/issues/3127883

File

core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php, line 401

Class

JsonApiPatchRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchInvalidFieldPropertyFromIssue3127883() : void {
    $this->config('jsonapi.settings')
        ->set('read_only', FALSE)
        ->save(TRUE);
    // Set up data model.
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $this->rebuildAll();
    // Create data.
    $node = Node::create([
        'title' => 'foo',
        'type' => 'page',
        'body' => [
            'format' => 'plain_text',
            'value' => 'Hello World',
        ],
    ]);
    $node->save();
    // Test.
    $user = $this->drupalCreateUser([
        'bypass node access',
    ]);
    $url = Url::fromUri('internal:/jsonapi/node/page/' . $node->uuid());
    $request_options = [
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/vnd.api+json',
            'Accept' => 'application/vnd.api+json',
        ],
        RequestOptions::AUTH => [
            $user->getAccountName(),
            $user->pass_raw,
        ],
        RequestOptions::JSON => [
            'data' => [
                'type' => 'node--page',
                'id' => $node->uuid(),
                'attributes' => [
                    'title' => 'Updated title',
                    'body' => [
                        'value' => 'Hello World … still.',
                        // Intentional typo in the property name!
'form' => 'plain_text',
                        // Another intentional typo.
                        // cSpell:disable-next-line
'sumary' => 'Boring old "Hello World".',
                        // And finally, one that is completely absurd.
'foobar' => '<script>alert("HI!");</script>',
                    ],
                ],
            ],
        ],
    ];
    $response = $this->request('PATCH', $url, $request_options);
    // Assert a helpful error response is present.
    $data = $this->getDocumentFromResponse($response, FALSE);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertNotNull($data);
    // cSpell:disable-next-line
    $this->assertSame("The properties 'form', 'sumary', 'foobar' do not exist on the 'body' field of type 'text_with_summary'. Writable properties are: 'value', 'format', 'summary'.", $data['errors'][0]['detail']);
    $request_options = [
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/vnd.api+json',
            'Accept' => 'application/vnd.api+json',
        ],
        RequestOptions::AUTH => [
            $user->getAccountName(),
            $user->pass_raw,
        ],
        RequestOptions::JSON => [
            'data' => [
                'type' => 'node--page',
                'id' => $node->uuid(),
                'attributes' => [
                    'title' => 'Updated title',
                    'body' => [
                        'value' => 'Hello World … still.',
                        // Intentional typo in the property name!
'form' => 'plain_text',
                        // Another intentional typo.
                        // cSpell:disable-next-line
'sumary' => 'Boring old "Hello World".',
                    ],
                ],
            ],
        ],
    ];
    $response = $this->request('PATCH', $url, $request_options);
    // Assert a helpful error response is present.
    $data = $this->getDocumentFromResponse($response, FALSE);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertNotNull($data);
    // cSpell:disable-next-line
    $this->assertSame("The properties 'form', 'sumary' do not exist on the 'body' field of type 'text_with_summary'. Did you mean 'format', 'summary'?", $data['errors'][0]['detail']);
}

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