function SessionExampleTest::testSessionExample

Same name in other branches
  1. 4.0.x modules/session_example/tests/src/Functional/SessionExampleTest.php \Drupal\Tests\session_example\Functional\SessionExampleTest::testSessionExample()

Functional tests for the session example.

File

modules/session_example/tests/src/Functional/SessionExampleTest.php, line 81

Class

SessionExampleTest
Tests the basic functions of the Session Example module.

Namespace

Drupal\Tests\session_example\Functional

Code

public function testSessionExample() {
    $assert = $this->assertSession();
    // Get the form and verify that it has placeholders.
    $this->drupalGet(Url::fromRoute('session_example.form'));
    $assert->responseContains('placeholder="Your name."');
    $assert->responseContains('placeholder="Your email address."');
    $assert->responseContains('placeholder="What is your quest?"');
    // Get the report and verify that it doesn't show any session information.
    $this->clickLink('View');
    $assert->pageTextContains('No name');
    $assert->pageTextContains('No email');
    $assert->pageTextContains('No quest');
    $assert->pageTextContains('No color');
    // Save an empty session submission.
    $this->drupalGet(Url::fromRoute('session_example.form'));
    $this->submitForm([], 'Save');
    $assert->pageTextContains('The session has been saved successfully.');
    // Make sure an empty session submission still has no reported information.
    $this->clickLink('Check here');
    $assert->pageTextContains('No name');
    $assert->pageTextContains('No email');
    $assert->pageTextContains('No quest');
    $assert->pageTextContains('No color');
    // Submit some session information.
    $form_data = [
        'name' => 'Sir Lancelot',
        'quest' => 'To seek the Grail',
        'color' => 'blue',
    ];
    $this->drupalGet(Url::fromRoute('session_example.form'));
    $this->submitForm($form_data, 'Save');
    // Check that the report shows our information.
    $this->clickLink('Check here');
    foreach ($form_data as $value) {
        $assert->pageTextContains($value);
    }
    // Clear the session.
    $this->drupalGet(Url::fromRoute('session_example.form'));
    $this->submitForm([], 'Clear Session');
    $assert->pageTextContains('Session is cleared.');
    // Verify that the session information doesn't show Sir Lancelot (or anyone
    // else).
    $this->clickLink('View');
    $assert->pageTextContains('No name');
    $assert->pageTextContains('No email');
    $assert->pageTextContains('No quest');
    $assert->pageTextContains('No color');
}