View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\Tests\webform\Traits\WebformSubmissionViewAccessTrait;
use Drupal\user\Entity\User;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformInterface;
class WebformSubmissionViewsAccessTest extends WebformBrowserTestBase {
use WebformSubmissionViewAccessTrait;
public static $modules = [
'system',
'user',
'views',
'webform',
'webform_test_views',
];
public function testEntityAccess() {
$webform = Webform::load('contact');
$any_user = $this
->drupalCreateUser([
'access webform overview',
]);
$own_user = $this
->drupalCreateUser([
'access webform overview',
]);
$without_access_user = $this
->drupalCreateUser([
'access webform overview',
]);
$webform
->setAccessRules([
'view_any' => [
'users' => [
$any_user
->id(),
],
],
'view_own' => [
'users' => [
$own_user
->id(),
],
],
])
->save();
$accounts = [
'any_user' => $any_user,
'own_user' => $own_user,
'without_access' => $without_access_user,
];
$this
->createSubmissions($webform, $accounts);
$this
->checkUserSubmissionAccess($webform, $accounts);
$webform
->setAccessRules([])
->save();
$this
->checkUserSubmissionAccess($webform, $accounts);
}
public function testPermissionAccess() {
$webform = Webform::load('contact');
$anonymous_user = User::getAnonymousUser();
user_role_grant_permissions('anonymous', [
'access webform overview',
'view own webform submission',
]);
$own_webform_user = $this
->drupalCreateUser([
'access webform overview',
'edit own webform',
]);
$webform
->setOwner($own_webform_user)
->save();
$any_submission_user = $this
->drupalCreateUser([
'access webform overview',
'view any webform submission',
]);
$own_submission_user = $this
->drupalCreateUser([
'access webform overview',
'view own webform submission',
]);
$without_access_user = $this
->drupalCreateUser([
'access webform overview',
]);
$accounts = [
'anonymous_user' => $anonymous_user,
'own_webform_user' => $own_webform_user,
'any_submission_user' => $any_submission_user,
'own_submission_user' => $own_submission_user,
'without_access' => $without_access_user,
];
$this
->createSubmissions($webform, $accounts);
$this
->checkUserSubmissionAccess($webform, $accounts);
foreach ($accounts as $account_type => &$account) {
if ($account_type === 'anonymous_user') {
$rid = 'anonymous';
}
else {
$roles = $account
->getRoles(TRUE);
$rid = reset($roles);
}
user_role_revoke_permissions($rid, [
'view any webform submission',
'view own webform submission',
'edit own webform',
]);
}
$this
->checkUserSubmissionAccess($webform, $accounts);
}
protected function createSubmissions(WebformInterface $webform, array $accounts) {
$submission_generate = \Drupal::service('webform_submission.generate');
foreach ($accounts as $account) {
WebformSubmission::create([
'webform_id' => $webform
->id(),
'uid' => $account
->id(),
'data' => $submission_generate
->getData($webform),
])
->save();
}
}
}