function PageExampleTest::testPageExample

Same name in other branches
  1. 4.0.x modules/page_example/tests/src/Functional/PageExampleTest.php \Drupal\Tests\page_example\Functional\PageExampleTest::testPageExample()

Main test.

Login user, create an example node, and test page functionality through the admin and user interfaces.

File

modules/page_example/tests/src/Functional/PageExampleTest.php, line 124

Class

PageExampleTest
Creates page and render the content based on the arguments passed in the URL.

Namespace

Drupal\Tests\page_example\Functional

Code

public function testPageExample() {
    $assert_session = $this->assertSession();
    // Verify that anonymous user can't access the pages created by
    // page_example module.
    $this->pageExampleVerifyNoAccess('examples/page-example/simple');
    $this->pageExampleVerifyNoAccess('examples/page-example/arguments/1/2');
    // Create a regular user and login.
    $this->webUser = $this->drupalCreateUser();
    $this->drupalLogin($this->webUser);
    // Verify that regular user can't access the pages created by
    // page_example module.
    $this->pageExampleVerifyNoAccess('examples/page-example/simple');
    $this->pageExampleVerifyNoAccess('examples/page-example/arguments/1/2');
    // Create a user with permissions to access 'simple' page and login.
    $this->webUser = $this->drupalCreateUser([
        'access simple page',
    ]);
    $this->drupalLogin($this->webUser);
    // Verify that user can access simple content.
    $this->drupalGet('/examples/page-example/simple');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('The quick brown fox jumps over the lazy dog.');
    // Check if user can't access arguments page.
    $this->pageExampleVerifyNoAccess('examples/page-example/arguments/1/2');
    // Create a user with permissions to access 'simple' page and login.
    $this->webUser = $this->drupalCreateUser([
        'access arguments page',
    ]);
    $this->drupalLogin($this->webUser);
    // Verify that user can access arguments content.
    $first = self::randomNumber(3);
    $second = self::randomNumber(3);
    $this->drupalGet('/examples/page-example/arguments/' . $first . '/' . $second);
    $assert_session->statusCodeEquals(200);
    // Verify argument usage.
    $assert_session->pageTextContains((string) new FormattableMarkup('First number was @number.', [
        '@number' => $first,
    ]));
    $assert_session->pageTextContains((string) new FormattableMarkup('Second number was @number.', [
        '@number' => $second,
    ]));
    $assert_session->pageTextContains((string) new FormattableMarkup('The total was @number.', [
        '@number' => $first + $second,
    ]));
    // Verify incomplete argument call to arguments content.
    $this->drupalGet('/examples/page-example/arguments/' . $first . '/');
    $assert_session->statusCodeEquals(404);
    // Verify 403 for invalid second argument.
    $this->drupalGet('/examples/page-example/arguments/' . $first . '/non-numeric-argument');
    $assert_session->statusCodeEquals(403);
    // Verify 403 for invalid first argument.
    $this->drupalGet('/examples/page-example/arguments/non-numeric-argument/' . $second);
    $assert_session->statusCodeEquals(403);
    // Check if user can't access simple page.
    $this->pageExampleVerifyNoAccess('examples/page-example/simple');
}