You are here

WebformHandlerRemotePostTest.php in Webform 8.5

Same filename and directory in other branches
  1. 6.x tests/src/Functional/Handler/WebformHandlerRemotePostTest.php

File

tests/src/Functional/Handler/WebformHandlerRemotePostTest.php
View source
<?php

namespace Drupal\Tests\webform\Functional\Handler;

use Drupal\file\Entity\File;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;

/**
 * Tests for remote post webform handler functionality.
 *
 * @group webform
 */
class WebformHandlerRemotePostTest extends WebformBrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'file',
    'webform',
    'webform_test_handler_remote_post',
  ];

  /**
   * Webforms to load.
   *
   * @var array
   */
  protected static $testWebforms = [
    'test_handler_remote_post',
    'test_handler_remote_put',
    'test_handler_remote_get',
    'test_handler_remote_post_file',
    'test_handler_remote_post_cast',
  ];

  /**
   * Test remote post handler.
   */
  public function testRemotePostHandler() {
    global $base_url;
    $this
      ->drupalLogin($this->rootUser);

    /**************************************************************************/

    // POST.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_post');

    // Check 'completed' operation.
    $sid = $this
      ->postSubmission($webform);

    // Check POST response.
    $this
      ->assertRaw("method: post\nstatus: success\nmessage: 'Processed completed request.'\noptions:\n  headers:\n    Accept-Language: en\n    custom_header: 'true'\n  form_params:\n    custom_completed: true\n    custom_data: true\n    response_type: '200'\n    first_name: John\n    last_name: Smith");
    $webform_submission = WebformSubmission::load($sid);
    $this
      ->assertRaw("form_params:\n  custom_completed: true\n  custom_data: true\n  response_type: '200'\n  first_name: John\n  last_name: Smith");
    $this
      ->assertRaw('This is a custom 200 success message.');

    // Check confirmation number is set via the
    // [webform:handler:remote_post:completed:confirmation_number] token.
    $this
      ->assertRaw('Your confirmation number is ' . $webform_submission
      ->getElementData('confirmation_number') . '.');

    // Check custom header.
    $this
      ->assertRaw('{&quot;headers&quot;:{&quot;Accept-Language&quot;:&quot;en&quot;,&quot;custom_header&quot;:&quot;true&quot;}');

    // Sleep for 1 second to make sure submission timestamp is updated.
    sleep(1);

    // Check 'updated' operation.
    $this
      ->drupalPostForm("admin/structure/webform/manage/test_handler_remote_post/submission/{$sid}/edit", [], 'Save');
    $this
      ->assertRaw("form_params:\n  custom_updated: true\n  custom_data: true\n  response_type: '200'\n  first_name: John\n  last_name: Smith");
    $this
      ->assertRaw('Processed updated request.');

    // Check 'deleted`' operation.
    $this
      ->drupalPostForm("admin/structure/webform/manage/test_handler_remote_post/submission/{$sid}/delete", [], 'Delete');
    $this
      ->assertRaw("form_params:\n  custom_deleted: true\n  custom_data: true\n  first_name: John\n  last_name: Smith\n  response_type: '200'");
    $this
      ->assertRaw('Processed deleted request.');

    // Switch anonymous user.
    $this
      ->drupalLogout();

    // Check 'draft' operation.
    $this
      ->postSubmission($webform, [], 'Save Draft');
    $this
      ->assertRaw("form_params:\n  custom_draft_created: true\n  custom_data: true\n  response_type: '200'\n  first_name: John\n  last_name: Smith");
    $this
      ->assertRaw('Processed draft_created request.');

    // Login root user.
    $this
      ->drupalLogin($this->rootUser);

    // Check 'convert' operation.
    $this
      ->assertRaw("form_params:\n  custom_converted: true\n  custom_data: true\n  first_name: John\n  last_name: Smith\n  response_type: '200'");
    $this
      ->assertRaw('Processed converted request.');
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');

    // Check excluded data.
    $webform
      ->getHandler('remote_post')
      ->setSetting('excluded_data', [
      'last_name' => 'last_name',
    ]);
    $webform
      ->save();
    $sid = $this
      ->postSubmission($webform);
    $this
      ->assertRaw('first_name: John');
    $this
      ->assertNoRaw('last_name: Smith');
    $this
      ->assertRaw("sid: '{$sid}'");
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');

    // Check 200 Success Error.
    $this
      ->postSubmission($webform, [
      'response_type' => '200',
    ]);
    $this
      ->assertRaw('This is a custom 200 success message.');
    $this
      ->assertRaw('Processed completed request.');

    // Check 500 Internal Server Error.
    $this
      ->postSubmission($webform, [
      'response_type' => '500',
    ]);
    $this
      ->assertNoRaw('Processed completed request.');
    $this
      ->assertRaw('Failed to process completed request.');
    $this
      ->assertRaw('Unable to process this submission. Please contact the site administrator.');

    // Check default custom response message.
    $handler = $webform
      ->getHandler('remote_post');
    $handler
      ->setSetting('message', 'This is a custom response message');
    $webform
      ->save();
    $this
      ->postSubmission($webform, [
      'response_type' => '500',
    ]);
    $this
      ->assertRaw('Failed to process completed request.');
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');
    $this
      ->assertRaw('This is a custom response message');

    // Check 201 Completed with no custom message.
    $this
      ->postSubmission($webform, [
      'response_type' => '201',
    ]);
    $this
      ->assertNoRaw('Processed created request.');
    $this
      ->assertNoRaw('This is a custom 404 not found message.');

    // Check 404 Not Found with custom message.
    $this
      ->postSubmission($webform, [
      'response_type' => '404',
    ]);
    $this
      ->assertRaw('File not found');
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');
    $this
      ->assertRaw('This is a custom 404 not found message.');

    // Check 401 Unauthorized with custom message and token.
    $this
      ->postSubmission($webform, [
      'response_type' => '401',
    ]);
    $this
      ->assertRaw('Unauthorized');
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');
    $this
      ->assertRaw('This is a message token <strong>Unauthorized to process completed request.</strong>');

    // Check 405 Method Not Allowed with custom message and token.
    $this
      ->postSubmission($webform, [
      'response_type' => '405',
    ]);
    $this
      ->assertRaw('Method Not Allowed');
    $this
      ->assertNoRaw('Unable to process this submission. Please contact the site administrator.');
    $this
      ->assertRaw('This is a array token <strong>[webform:handler:remote_post:options]</strong>');

    // Disable saving of results.
    $webform
      ->setSetting('results_disabled', TRUE);
    $webform
      ->save();

    // Check confirmation number when results disabled.
    $sid = $this
      ->postSubmission($webform);
    $this
      ->assertNull($sid);

    // Get confirmation number from JSON packet.
    preg_match('/&quot;confirmation_number&quot;:&quot;([a-zA-z0-9]+)&quot;/', $this
      ->getRawContent(), $match);
    $this
      ->assertRaw('Your confirmation number is ' . $match[1] . '.');

    // Set remote post error URL to homepage.
    $handler = $webform
      ->getHandler('remote_post');
    $handler
      ->setSetting('error_url', $webform
      ->toUrl('canonical', [
      'query' => [
        'error' => '1',
      ],
    ])
      ->toString());
    $webform
      ->save();

    // Check 404 Not Found with custom error uri.
    $this
      ->postSubmission($webform, [
      'response_type' => '404',
    ]);
    $this
      ->assertRaw('This is a custom 404 not found message.');
    $this
      ->assertUrl($webform
      ->toUrl('canonical', [
      'query' => [
        'error' => '1',
      ],
    ])
      ->setAbsolute()
      ->toString());

    /**************************************************************************/

    // PUT.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_put');
    $this
      ->postSubmission($webform);

    // Check PUT response.
    $this
      ->assertRaw("method: put\nstatus: success\nmessage: 'Processed completed request.'\noptions:\n  headers:\n    custom_header: 'true'\n  form_params:\n    custom_completed: true\n    custom_data: true\n    response_type: '200'\n    first_name: John\n    last_name: Smith");

    /**************************************************************************/

    // GET.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_get');
    $this
      ->postSubmission($webform);

    // Check GET response.
    $this
      ->assertRaw("method: get\nstatus: success\nmessage: 'Processed completed request.'\noptions:\n  headers:\n    custom_header: 'true'");

    // Check request URL contains query string.
    $this
      ->assertRaw("http://webform-test-handler-remote-post/completed?custom_completed=1&amp;custom_data=1&amp;response_type=200&amp;first_name=John&amp;last_name=Smith");

    // Check response data.
    $this
      ->assertRaw("message: 'Processed completed request.'");

    // Get confirmation number from JSON packet.
    preg_match('/&quot;confirmation_number&quot;:&quot;([a-zA-z0-9]+)&quot;/', $this
      ->getRawContent(), $match);
    $this
      ->assertRaw('Your confirmation number is ' . $match[1] . '.');

    /**************************************************************************/

    // POST File.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_post_file');
    $sid = $this
      ->postSubmissionTest($webform);
    $webform_submission = WebformSubmission::load($sid);
    $file_data = $webform_submission
      ->getElementData('file');
    $file = File::load($file_data);
    $file_id = $file
      ->id();
    $file_uuid = $file
      ->uuid();
    $files_data = $webform_submission
      ->getElementData('files');
    $file = File::load(reset($files_data));
    $files_id = $file
      ->id();
    $files_uuid = $file
      ->uuid();

    // Check the file name, uri, and data is appended to form params.
    $this
      ->assertRaw("form_params:\n  file: 1\n  files:\n    - 2\n  _file:\n    id: {$file_id}\n    name: file.txt\n    uri: 'private://webform/test_handler_remote_post_file/{$sid}/file.txt'\n    mime: text/plain\n    uuid: {$file_uuid}\n    data: dGhpcyBpcyBhIHNhbXBsZSB0eHQgZmlsZQppdCBoYXMgdHdvIGxpbmVzCg==\n  file__id: {$file_id}\n  file__name: file.txt\n  file__uri: 'private://webform/test_handler_remote_post_file/{$sid}/file.txt'\n  file__mime: text/plain\n  file__uuid: {$file_uuid}\n  file__data: dGhpcyBpcyBhIHNhbXBsZSB0eHQgZmlsZQppdCBoYXMgdHdvIGxpbmVzCg==\n  _files:\n    -\n      id: {$files_id}\n      name: files.txt\n      uri: 'private://webform/test_handler_remote_post_file/{$sid}/files.txt'\n      mime: text/plain\n      uuid: {$files_uuid}\n      data: dGhpcyBpcyBhIHNhbXBsZSB0eHQgZmlsZQppdCBoYXMgdHdvIGxpbmVzCg==");

    // Check the file data is NOT appended to form params.
    $handler = $webform
      ->getHandler('remote_post');
    $handler
      ->setSetting('file_data', FALSE);
    $webform
      ->save();
    $this
      ->drupalPostForm("/admin/structure/webform/manage/test_handler_remote_post_file/submission/{$sid}/edit", [], t('Save'));
    $this
      ->assertRaw("form_params:\n  file: 1\n  files:\n    - 2\n  _file:\n    id: {$file_id}\n    name: file.txt\n    uri: 'private://webform/test_handler_remote_post_file/{$sid}/file.txt'\n    mime: text/plain\n    uuid: {$file_uuid}\n  file__id: {$file_id}\n  file__name: file.txt\n  file__uri: 'private://webform/test_handler_remote_post_file/{$sid}/file.txt'\n  file__mime: text/plain\n  file__uuid: {$file_uuid}\n  _files:\n    -\n      id: {$files_id}\n      name: files.txt\n      uri: 'private://webform/test_handler_remote_post_file/{$sid}/files.txt'\n      mime: text/plain\n      uuid: {$files_uuid}");

    /**************************************************************************/

    // POST cast.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_post_cast');
    $this
      ->postSubmission($webform);
    $this
      ->assertRaw("form_params:\n  boolean_true: true\n  integer: 100\n  float: 100.01\n  checkbox: false\n  number: ''\n  number_multiple: {  }\n  custom_composite:\n    -\n      textfield: ''\n      number: !!float 0\n      checkbox: false");
    $edit = [
      'checkbox' => TRUE,
      'number' => '10',
      'number_multiple[items][0][_item_]' => '10.5',
      'custom_composite[items][0][textfield]' => 'text',
      'custom_composite[items][0][checkbox]' => TRUE,
      'custom_composite[items][0][number]' => '20.5',
    ];
    $this
      ->postSubmission($webform, $edit);
    $this
      ->assertRaw("form_params:\n  boolean_true: true\n  integer: 100\n  float: 100.01\n  checkbox: true\n  number: !!float 10\n  number_multiple:\n    - 10.5\n  custom_composite:\n    -\n      textfield: text\n      checkbox: true\n      number: 20.5");

    /**************************************************************************/

    // POST error.

    /**************************************************************************/

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = Webform::load('test_handler_remote_post_error');
    $this
      ->postSubmission($webform);
    $this
      ->assertEqual($base_url . '/error_url', $this
      ->getUrl());
  }

}

Classes

Namesort descending Description
WebformHandlerRemotePostTest Tests for remote post webform handler functionality.