You are here

public function ProtectedPagesAccess::testProtectedPageAccess in Protected Pages 8

Test access to a Protected Page.

File

tests/src/Functional/ProtectedPagesAccess.php, line 43

Class

ProtectedPagesAccess
Provides functional Drupal tests for access to protected pages and config.

Namespace

Drupal\Tests\protected_pages\Functional

Code

public function testProtectedPageAccess() {

  // Create a node.
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  $node = $this
    ->drupalCreateNode();
  $this
    ->assertNotEmpty(Node::load($node
    ->id()), 'Node created.');

  // Protect created node.
  $page_data = [
    'password' => 'test_pass',
    'path' => '/node/' . $node
      ->id(),
  ];
  $storage = \Drupal::service('protected_pages.storage');
  $storage
    ->insertProtectedPage($page_data);
  $page_path = 'node/' . $node
    ->id();

  // Enable page caching.
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();
  drupal_flush_all_caches();

  // Create a user w/ permission
  // to 'access protected page password screen'.
  $user = $this
    ->drupalCreateUser([
    'access protected page password screen',
  ]);

  // View the node as an Anonymous user.
  // User should see Access Denied.
  $this
    ->drupalGet($page_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // View the node again as an Anonymous user to ensure that page cache
  // does not break page protection. See
  // https://www.drupal.org/project/protected_pages/issues/2973524.
  $this
    ->drupalGet($page_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Login as a user w/ permission to 'access protected page password screen'.
  // Ensure the user can use password screen.
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet($page_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('Enter Password');
  $this
    ->assertSession()
    ->addressEquals('protected-page?destination=/node/1&protected_page=1');
  $this
    ->drupalLogout($user);
}