View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
use Drupal\webform\WebformInterface;
class WebformSettingsAccessDeniedTest extends WebformBrowserTestBase {
public static $modules = [
'block',
'webform',
];
protected static $testWebforms = [
'test_form_access_denied',
];
protected function setUp() {
parent::setUp();
$this
->placeBlocks();
}
public function testWebformAccessDenied() {
$webform = Webform::load('test_form_access_denied');
$webform_edit_route_url = Url::fromRoute('entity.webform.edit_form', [
'webform' => $webform
->id(),
]);
$webform
->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_LOGIN);
$webform
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_access_denied');
$this
->assertRaw('Please login to access <b>Test: Webform: Access Denied</b>.');
$route_options = [
'query' => [
'destination' => $webform_edit_route_url
->toString(),
],
];
$this
->assertUrl(Url::fromRoute('user.login', [], $route_options));
$webform
->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_DEFAULT);
$webform
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_access_denied');
$this
->assertRaw('You are not authorized to access this page.');
$this
->assertNoRaw('Please login to access <b>Test: Webform: Access Denied</b>.');
$webform
->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_PAGE);
$webform
->setSetting('form_access_denied_title', 'Webform: Access denied');
$webform
->setSetting('form_access_denied_attributes', [
'style' => 'border: 1px solid red',
'class' => [],
'attributes' => [],
]);
$webform
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_access_denied');
$this
->assertRaw('<h1>Webform: Access denied</h1>');
$this
->assertRaw('<div style="border: 1px solid red" class="webform-access-denied">Please login to access <b>Test: Webform: Access Denied</b>.</div>');
$this
->drupalPlaceBlock('webform_block', [
'webform_id' => 'test_form_access_denied',
]);
$webform
->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_DEFAULT);
$webform
->save();
$this
->drupalGet('/<front>');
$this
->assertNoRaw('<div style="border: 1px solid red" class="webform-access-denied">Please login to access <b>Test: Webform: Access Denied</b>.</div>');
$webform
->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_MESSAGE);
$webform
->save();
$this
->drupalGet('/<front>');
$this
->assertRaw('<div style="border: 1px solid red" class="webform-access-denied">Please login to access <b>Test: Webform: Access Denied</b>.</div>');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/<front>');
$this
->assertNoRaw('<div class="webform-access-denied">Please login to access <b>Test: Webform: Access Denied</b>.</div>');
$this
->assertRaw('id="webform-submission-test-form-access-denied-user-1-add-form"');
}
public function testWebformSubmissionAccessDenied() {
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_form_access_denied');
$sid = $this
->postSubmission($webform);
$this
->drupalLogout();
$this
->drupalGet("admin/structure/webform/manage/test_form_access_denied/submission/{$sid}");
$this
->assertRaw("Please login to access <b>Test: Webform: Access Denied: Submission #{$sid}</b>.");
$webform
->setSetting('submission_access_denied', WebformInterface::ACCESS_DENIED_DEFAULT);
$webform
->save();
$this
->drupalGet("admin/structure/webform/manage/test_form_access_denied/submission/{$sid}");
$this
->assertRaw('You are not authorized to access this page.');
$this
->assertNoRaw("Please login to access <b>Test: Webform: Access Denied: Submission #{$sid}</b>.");
$webform
->setSetting('submission_access_denied', WebformInterface::ACCESS_DENIED_PAGE);
$webform
->setSetting('submission_access_denied_title', 'Webform submission: Access denied');
$webform
->setSetting('submission_access_denied_attributes', [
'style' => 'border: 1px solid red',
'class' => [],
'attributes' => [],
]);
$webform
->save();
$this
->drupalGet("admin/structure/webform/manage/test_form_access_denied/submission/{$sid}");
$this
->assertNoRaw('You are not authorized to access this page.');
$this
->assertRaw('<h1>Webform submission: Access denied</h1>');
$this
->assertRaw('<div style="border: 1px solid red" class="webform-submission-access-denied">Please login to access <b>Test: Webform: Access Denied: Submission #' . $sid . '</b>.</div>');
}
}