You are here

public function WebformRestSubmissionTest::testWebformRestPatchSubmission in Webform REST 4.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/WebformRestSubmissionTest.php \Drupal\Tests\webform_rest\Functional\WebformRestSubmissionTest::testWebformRestPatchSubmission()

Test method PATCH submission resource.

File

tests/src/Functional/WebformRestSubmissionTest.php, line 88

Class

WebformRestSubmissionTest
Test the webform rest endpoints for submissions.

Namespace

Drupal\Tests\webform_rest\Functional

Code

public function testWebformRestPatchSubmission() {
  $webform = Webform::load('webform_rest_test');
  $this
    ->drupalLogin($this->rootUser);
  $sid = $this
    ->postSubmissionTest($webform, [
    'first_name' => 'John',
    'last_name' => 'Smith',
  ]);

  // Get webform submission.
  $webform_submission = WebformSubmission::load($sid);
  $uuid = $webform_submission
    ->uuid();
  $body = [
    'webform_id' => 'webform_rest_test',
    'first_name' => 'Daniel',
    'last_name' => 'Hopkins',
  ];
  $client = new Client();
  $token = $this
    ->drupalGet("/session/token", [
    'query' => [
      '_format' => 'hal_json',
    ],
  ]);
  $response = $this
    ->request('PATCH', $this->baseUrl . "/webform_rest/webform_rest_test/submission/{$uuid}?_format=json", [
    'body' => Json::encode($body),
    'headers' => [
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'X-CSRF-Token' => $token,
    ],
  ]);
  $created_response = Json::decode((string) $response
    ->getBody());
  $this
    ->assertResponse(200);
  $this
    ->assertArrayHasKey('sid', $created_response);
  $this
    ->assertNotEmpty($created_response['sid']);
  $uuid = $created_response['sid'];

  // Get webform submission.
  $result = $this
    ->drupalGet("/webform_rest/webform_rest_test/submission/{$uuid}", [
    'query' => [
      '_format' => 'hal_json',
    ],
  ]);
  $this
    ->assertResponse(200);
  $this
    ->assertRaw('"data":{"first_name":"Daniel","last_name":"Hopkins"}');
}