View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementAccessTest extends WebformElementBrowserTestBase {
public static $modules = [
'webform',
'webform_ui',
];
protected static $testWebforms = [
'test_element_access',
];
public function testAccess() {
$normal_user = $this
->drupalCreateUser([
'access user profiles',
]);
$admin_submission_user = $this
->drupalCreateUser([
'access user profiles',
'administer webform submission',
]);
$own_submission_user = $this
->drupalCreateUser([
'access user profiles',
'access webform overview',
'create webform',
'edit own webform',
'delete own webform',
'view own webform submission',
'edit own webform submission',
'delete own webform submission',
]);
$webform = Webform::load('test_element_access');
$elements = $webform
->get('elements');
$elements = str_replace(' - 1', ' - ' . $admin_submission_user
->id(), $elements);
$elements = str_replace('USER:1', 'USER:' . $admin_submission_user
->id(), $elements);
$webform
->set('elements', $elements);
$webform
->save();
$this
->drupalLogin($normal_user);
$sid = $this
->postSubmission($webform);
$webform_submission = WebformSubmission::load($sid);
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/webform/manage/test_element_access/element/access_create_roles_anonymous/edit');
$this
->assertFieldById('edit-properties-access-create-roles-anonymous', NULL);
$this
->drupalLogin($own_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/test_element_access/element/access_create_roles_anonymous/edit');
$this
->assertNoFieldById('edit-properties-access-create-roles-anonymous', NULL);
$this
->drupalLogout();
$this
->drupalGet('/webform/test_element_access');
$this
->assertFieldByName('access_create_roles_anonymous');
$this
->assertNoFieldByName('access_create_roles_authenticated');
$this
->assertNoFieldByName('access_create_users');
$this
->assertNoFieldByName('access_create_permissions');
$this
->drupalLogin($normal_user);
$this
->drupalGet('/webform/test_element_access');
$this
->assertNoFieldByName('access_create_roles_anonymous');
$this
->assertFieldByName('access_create_roles_authenticated');
$this
->assertNoFieldByName('access_create_users');
$this
->assertFieldByName('access_create_permissions');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/webform/test_element_access');
$this
->assertNoFieldByName('access_create_roles_anonymous');
$this
->assertFieldByName('access_create_roles_authenticated');
$this
->assertFieldByName('access_create_users');
$this
->assertFieldByName('access_create_permissions');
$this
->drupalLogout();
$this
->drupalGet($webform_submission
->getTokenUrl());
$this
->assertFieldByName('access_update_roles_anonymous');
$this
->assertNoFieldByName('access_update_roles_authenticated');
$this
->assertNoFieldByName('access_update_users');
$this
->assertNoFieldByName('access_update_permissions');
$this
->drupalLogin($normal_user);
$this
->drupalGet("/webform/test_element_access/submissions/{$sid}/edit");
$this
->assertNoFieldByName('access_update_roles_anonymous');
$this
->assertFieldByName('access_update_roles_authenticated');
$this
->assertNoFieldByName('access_update_users');
$this
->assertFieldByName('access_update_permissions');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet("/admin/structure/webform/manage/test_element_access/submission/{$sid}/edit");
$this
->assertNoFieldByName('access_update_roles_anonymous');
$this
->assertFieldByName('access_update_roles_authenticated');
$this
->assertFieldByName('access_update_users');
$this
->assertFieldByName('access_update_permissions');
$urls = [
[
'path' => "/admin/structure/webform/manage/test_element_access/submission/{$sid}",
],
[
'path' => '/admin/structure/webform/manage/test_element_access/results/submissions',
],
[
'path' => '/admin/structure/webform/manage/test_element_access/results/download',
],
[
'path' => '/admin/structure/webform/manage/test_element_access/results/download',
'options' => [
'query' => [
'download' => 1,
],
],
],
];
foreach ($urls as $url) {
$url += [
'options' => [],
];
$this
->drupalLogout();
$this
->drupalGet($url['path'], $url['options']);
$this
->assertRaw('access_view_roles (anonymous)');
$this
->assertNoRaw('access_view_roles (authenticated)');
$this
->assertNoRaw('access_view_users (USER:' . $admin_submission_user
->id() . ')');
$this
->assertNoRaw('access_view_permissions (access user profiles)');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet($url['path'], $url['options']);
$this
->assertNoRaw('access_view_roles (anonymous)');
$this
->assertRaw('access_view_roles (authenticated)');
$this
->assertNoRaw('access_view_users (USER:' . $admin_submission_user
->id() . ')');
$this
->assertRaw('access_view_permissions (access user profiles)');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet($url['path'], $url['options']);
$this
->assertNoRaw('access_view_roles (anonymous)');
$this
->assertRaw('access_view_roles (authenticated)');
$this
->assertRaw('access_view_users (USER:' . $admin_submission_user
->id() . ')');
$this
->assertRaw('access_view_permissions (access user profiles)');
}
$urls = [
'<td>token</td>' => [
'path' => '/admin/structure/webform/manage/test_element_access/results/download',
],
',Token,' => [
'path' => '/admin/structure/webform/manage/test_element_access/results/download',
'options' => [
'query' => [
'download' => 1,
'excluded_columns' => '',
],
],
],
];
foreach ($urls as $raw => $url) {
$url += [
'options' => [],
];
$this
->drupalLogout();
$this
->drupalGet($url['path'], $url['options']);
$this
->assertNoRaw($raw, 'Anonymous user can not access token');
$this
->drupalLogin($normal_user);
$this
->drupalGet($url['path'], $url['options']);
$this
->assertNoRaw($raw, 'Authenticated user can not access token');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet($url['path'], $url['options']);
$this
->assertRaw($raw, 'Admin webform user can access token');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet($url['path'], $url['options']);
$this
->assertRaw($raw, 'Admin submission user can access token');
}
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/webform/manage/test_element_access');
$this
->assertCssSelect('[data-webform-key="textfield_access_property"].webform-ui-element-disabled');
$this
->assertCssSelect('[data-webform-key="fieldset_access_property"].webform-ui-element-disabled');
$this
->assertCssSelect('[data-webform-key="fieldset_textfield_access"]');
$this
->assertNoCssSelect('[data-webform-key="fieldset_textfield_access"].webform-ui-element-disabled');
$this
->drupalGet('/admin/structure/webform/manage/test_element_access/results/submissions');
$this
->assertNoRaw('textfield_access_property');
$this
->assertNoRaw('fieldset_access_property');
$this
->assertRaw('fieldset_textfield_access');
$this
->drupalGet('/admin/structure/webform/manage/test_element_access/results/download');
$this
->assertNoRaw('textfield_access_property');
$this
->assertNoRaw('fieldset_access_property');
$this
->assertRaw('fieldset_textfield_access');
}
}