You are here

public function WebformSubmissionViewsAccessTest::testEntityAccess in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/WebformSubmissionViewsAccessTest.php \Drupal\Tests\webform\Functional\WebformSubmissionViewsAccessTest::testEntityAccess()

Test webform submission entity access in a view query.

File

tests/src/Functional/WebformSubmissionViewsAccessTest.php, line 36

Class

WebformSubmissionViewsAccessTest
Tests access rules in the context of webform submission views access.

Namespace

Drupal\Tests\webform\Functional

Code

public function testEntityAccess() {

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load('contact');

  // Create any access user, own access user, and no (anonymous) access user.
  $any_user = $this
    ->drupalCreateUser([
    'access webform overview',
  ]);
  $own_user = $this
    ->drupalCreateUser([
    'access webform overview',
  ]);
  $without_access_user = $this
    ->drupalCreateUser([
    'access webform overview',
  ]);

  // Grant any and own access to submissions.
  $webform
    ->setAccessRules([
    'view_any' => [
      'users' => [
        $any_user
          ->id(),
      ],
    ],
    'view_own' => [
      'users' => [
        $own_user
          ->id(),
      ],
    ],
  ])
    ->save();

  // Create an array of the accounts.
  $accounts = [
    'any_user' => $any_user,
    'own_user' => $own_user,
    'without_access' => $without_access_user,
  ];

  // Create test submissions.
  $this
    ->createSubmissions($webform, $accounts);

  // Check user submission access.
  $this
    ->checkUserSubmissionAccess($webform, $accounts);

  // Clear webform access rules.
  $webform
    ->setAccessRules([])
    ->save();

  // Check user submission access cache is cleared.
  $this
    ->checkUserSubmissionAccess($webform, $accounts);
}