View source
<?php
namespace Drupal\Tests\webform_node\Functional\Access;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform_node\Functional\WebformNodeBrowserTestBase;
class WebformNodeAccessRulesTest extends WebformNodeBrowserTestBase {
public static $modules = [
'webform',
'webform_node',
];
public function testAccessRules() {
$access_rules_manager = \Drupal::service('webform.access_rules_manager');
$default_access_rules = $access_rules_manager
->getDefaultAccessRules();
$webform = Webform::load('contact');
$node = $this
->createWebformNode('contact');
$nid = $node
->id();
$account = $this
->drupalCreateUser([
'access content',
]);
$rid = $account
->getRoles(TRUE)[0];
$uid = $account
->id();
$this
->drupalLogin($account);
$edit = [
'name' => '{name}',
'email' => 'example@example.com',
'subject' => '{subject}',
'message' => '{message',
];
$sid = $this
->postNodeSubmission($node, $edit);
$webform
->setAccessRules($default_access_rules)
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertFieldByName('name', $account
->getAccountName());
$this
->assertFieldByName('email', $account
->getEmail());
$access_rules = [
'create' => [
'roles' => [],
'users' => [],
],
] + $default_access_rules;
$webform
->setAccessRules($access_rules)
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoFieldByName('name', $account
->getAccountName());
$this
->assertNoFieldByName('email', $account
->getEmail());
$any_tests = [
'node/{node}/webform/results/submissions' => 'view_any',
'node/{node}/webform/results/download' => 'view_any',
'node/{node}/webform/results/clear' => 'purge_any',
'node/{node}/webform/submission/{webform_submission}' => 'view_any',
'node/{node}/webform/submission/{webform_submission}/text' => 'view_any',
'node/{node}/webform/submission/{webform_submission}/yaml' => 'view_any',
'node/{node}/webform/submission/{webform_submission}/edit' => 'update_any',
'node/{node}/webform/submission/{webform_submission}/delete' => 'delete_any',
];
foreach ($any_tests as $path => $permission) {
$path = str_replace('{node}', $nid, $path);
$path = str_replace('{webform_submission}', $sid, $path);
$this
->drupalGet($path);
$this
->assertResponse(403, 'Webform returns access denied');
}
foreach ($any_tests as $path => $permission) {
$path = str_replace('{node}', $nid, $path);
$path = str_replace('{webform_submission}', $sid, $path);
$access_rules = [
$permission => [
'roles' => [
$rid,
],
'users' => [],
],
] + $default_access_rules;
$webform
->setAccessRules($access_rules)
->save();
$this
->drupalGet($path);
$this
->assertResponse(200, 'Webform allows access via role access rules');
$access_rules = [
$permission => [
'roles' => [],
'users' => [
$uid,
],
],
] + $default_access_rules;
$webform
->setAccessRules($access_rules)
->save();
$this
->drupalGet($path);
$this
->assertResponse(200, 'Webform allows access via user access rules');
}
}
}