View source
<?php
namespace Drupal\Tests\webform_access\Functional;
use Drupal\field\Entity\FieldConfig;
class WebformAccessTest extends WebformAccessBrowserTestBase {
public static $modules = [
'field_ui',
];
public function testWebformAccess() {
$nid = $this->nodes['contact_01']
->id();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertLink('employee_group');
$this
->assertLink('manager_group');
$this
->assertLink($this->nodes['contact_01']
->label());
foreach ($this->users as $account) {
$this
->drupalLogin($account);
$this
->drupalGet("/node/{$nid}/webform/results/submissions");
$this
->assertResponse(403);
}
$this
->drupalLogin($this->rootUser);
foreach ($this->groups as $name => $group) {
$this
->drupalPostForm("/admin/structure/webform/access/group/manage/{$name}", [
'users[]' => $this->users[$name]
->id(),
], 'Save');
}
foreach ([
'manager',
'employee',
] as $name) {
$account = $this->users[$name];
$this
->drupalLogin($account);
$this
->drupalGet("/node/{$nid}/webform/results/submissions");
$this
->assertResponse(200);
}
$this
->drupalLogin($this->users['employee']);
$this
->drupalGet("/node/{$nid}/webform/results/clear");
$this
->assertResponse(403);
$this
->drupalLogin($this->users['manager']);
$this
->drupalGet("/node/{$nid}/webform/results/clear");
$this
->assertResponse(200);
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm('/admin/structure/webform/access/group/manage/employee', [
'users[]' => 1,
], 'Save');
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm('/user/' . $this->users['employee']
->id() . '/edit', [
'webform_access_group[]' => 'manager',
], 'Save');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/types');
$this
->drupalGet('/admin/structure/types/manage/webform/fields');
$this
->drupalPostForm('/admin/structure/types/manage/webform/fields/node.webform.webform', [
'default_value_input[webform][0][target_id]' => 'contact',
'default_value_input[webform][0][settings][default_data]' => 'test: test',
'default_value_input[webform][0][settings][webform_access_group][]' => 'manager',
], 'Save settings');
$this
->drupalGet('/node/add/webform');
$this
->assertFieldByName('webform[0][settings][webform_access_group][]', 'manager');
$this
->drupalLogin($this->users['employee']);
$this
->drupalGet("/node/{$nid}/webform/results/clear");
$this
->assertResponse(200);
$this
->drupalLogin($this->rootUser);
foreach ($this->groups as $name => $group) {
$this
->drupalPostForm("/admin/structure/webform/access/group/manage/{$name}", [
'entities[]' => 'node:' . $this->nodes['contact_02']
->id() . ':webform:contact',
], 'Save');
}
$this
->drupalLogin($this->users['employee']);
$this
->drupalGet("/node/{$nid}/webform/results/clear");
$this
->assertResponse(403);
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm("/node/{$nid}/edit", [
'webform[0][settings][webform_access_group][]' => 'manager',
], 'Save');
$this
->drupalLogin($this->users['employee']);
$this
->drupalGet("/node/{$nid}/webform/results/clear");
$this
->assertResponse(200);
$this->groups['employee']
->delete();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertRaw('manager_type');
$this
->assertLink('manager_group');
$this
->assertLink('manager_user');
$this
->assertLink('employee_user');
$this
->assertLink('contact_01');
$this
->assertLink('contact_02');
\Drupal::entityTypeManager()
->getStorage('webform_access_group')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('webform_access_type')
->resetCache();
foreach ($this->types as $type) {
$type
->delete();
}
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertNoRaw('manager_type');
foreach ($this->users as $user) {
$user
->delete();
}
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertNoLink('manager_user');
$this
->assertNoLink('employee_user');
$this->nodes['contact_02']
->delete();
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertNoLink('contact_02');
FieldConfig::loadByName('node', 'webform', 'webform')
->delete();
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertNoLink('contact_02');
}
public function testWebformAdministratorAccess() {
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/structure/webform/access/group/manage/manager');
$this
->assertFieldByName('label');
$this
->assertFieldByName('description[value]');
$this
->assertFieldByName('type');
$this
->assertFieldByName('admins[]');
$this
->assertFieldByName('users[]');
$this
->assertFieldByName('entities[]');
$this
->assertFieldByName('permissions[administer]');
$this
->drupalLogout();
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertResponse(403);
$administrator = $this
->drupalCreateUser();
$this
->drupalLogin($administrator);
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertResponse(403);
$this->groups['manager']
->addAdminId($administrator
->id());
$this->groups['manager']
->save();
$this
->drupalGet('/admin/structure/webform/access/group/manage');
$this
->assertResponse(200);
$this
->assertLink('Manage');
$this
->assertNoLink('Edit');
$this
->clickLink('manager_group');
$this
->assertRaw('<details data-drupal-selector="edit-information" id="edit-information" class="js-form-wrapper form-wrapper">');
$this
->assertNoFieldByName('label');
$this
->assertNoFieldByName('description[value]');
$this
->assertNoFieldByName('type');
$this
->assertNoFieldByName('admins[]');
$this
->assertFieldByName('users[]');
$this
->assertNoFieldByName('entities[]');
$this
->assertNoFieldByName('permissions[administer]');
}
}