You are here

public function SessionExampleTest::testSessionExample in Examples for Developers 8

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

Functional tests for the session example.

File

session_example/tests/src/Functional/SessionExampleTest.php, line 80

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."', 'Name placeholder contains Your name');
  $assert
    ->responseContains('placeholder="Your email address."', 'Email placeholder contains Your email address.');
  $assert
    ->responseContains('placeholder="What is your quest?"', 'Quest placeholder contains 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
    ->drupalPostForm(Url::fromRoute('session_example.form'), [], '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
    ->drupalPostForm(Url::fromRoute('session_example.form'), $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
    ->drupalPostForm(Url::fromRoute('session_example.form'), [], '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');
}