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;
class WebformHandlerRemotePostTest extends WebformBrowserTestBase {
public static $modules = [
'file',
'webform',
'webform_test_handler_remote_post',
];
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',
];
public function testRemotePostHandler() {
global $base_url;
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_handler_remote_post');
$sid = $this
->postSubmission($webform);
$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.');
$this
->assertRaw('Your confirmation number is ' . $webform_submission
->getElementData('confirmation_number') . '.');
$this
->assertRaw('{"headers":{"Accept-Language":"en","custom_header":"true"}');
sleep(1);
$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.');
$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.');
$this
->drupalLogout();
$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.');
$this
->drupalLogin($this->rootUser);
$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.');
$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.');
$this
->postSubmission($webform, [
'response_type' => '200',
]);
$this
->assertRaw('This is a custom 200 success message.');
$this
->assertRaw('Processed completed request.');
$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.');
$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');
$this
->postSubmission($webform, [
'response_type' => '201',
]);
$this
->assertNoRaw('Processed created request.');
$this
->assertNoRaw('This is a custom 404 not found 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.');
$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>');
$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>');
$webform
->setSetting('results_disabled', TRUE);
$webform
->save();
$sid = $this
->postSubmission($webform);
$this
->assertNull($sid);
preg_match('/"confirmation_number":"([a-zA-z0-9]+)"/', $this
->getRawContent(), $match);
$this
->assertRaw('Your confirmation number is ' . $match[1] . '.');
$handler = $webform
->getHandler('remote_post');
$handler
->setSetting('error_url', $webform
->toUrl('canonical', [
'query' => [
'error' => '1',
],
])
->toString());
$webform
->save();
$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());
$webform = Webform::load('test_handler_remote_put');
$this
->postSubmission($webform);
$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");
$webform = Webform::load('test_handler_remote_get');
$this
->postSubmission($webform);
$this
->assertRaw("method: get\nstatus: success\nmessage: 'Processed completed request.'\noptions:\n headers:\n custom_header: 'true'");
$this
->assertRaw("http://webform-test-handler-remote-post/completed?custom_completed=1&custom_data=1&response_type=200&first_name=John&last_name=Smith");
$this
->assertRaw("message: 'Processed completed request.'");
preg_match('/"confirmation_number":"([a-zA-z0-9]+)"/', $this
->getRawContent(), $match);
$this
->assertRaw('Your confirmation number is ' . $match[1] . '.');
$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();
$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==");
$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}");
$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");
$webform = Webform::load('test_handler_remote_post_error');
$this
->postSubmission($webform);
$this
->assertEqual($base_url . '/error_url', $this
->getUrl());
}
}