Namespace
Drupal\Tests\session_example\Functional
File
-
modules/session_example/tests/src/Functional/SessionExampleTest.php
View source
<?php
namespace Drupal\Tests\session_example\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class SessionExampleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'session_example',
'block',
];
protected function setUp() : void {
parent::setUp();
$this->drupalPlaceBlock('local_tasks_block', [
'region' => 'content',
]);
$this->drupalPlaceBlock('system_menu_block:tools', []);
$this->drupalLogin($this->createUser([
'access content',
]));
}
public function testSessionExampleLinks() {
$assert = $this->assertSession();
$routes_with_menu_links = [
'session_example.form' => [
'Save',
'Clear Session',
],
];
$this->drupalGet('');
foreach (array_keys($routes_with_menu_links) as $route) {
$assert->linkByHrefExists(Url::fromRoute($route)->getInternalPath());
}
$routes = [
'session_example.view' => [],
];
$routes = array_merge($routes_with_menu_links, $routes);
foreach ($routes as $route => $buttons) {
$url = Url::fromRoute($route);
$this->drupalGet($url);
$assert->statusCodeEquals(200);
foreach ($buttons as $button) {
$this->drupalGet($url);
$this->submitForm([], $button);
$assert->statusCodeEquals(200);
}
}
}
public function testSessionExample() {
$assert = $this->assertSession();
$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?"');
$this->clickLink('View');
$assert->pageTextContains('No name');
$assert->pageTextContains('No email');
$assert->pageTextContains('No quest');
$assert->pageTextContains('No color');
$this->drupalGet(Url::fromRoute('session_example.form'));
$this->submitForm([], 'Save');
$assert->pageTextContains('The session has been saved successfully.');
$this->clickLink('Check here');
$assert->pageTextContains('No name');
$assert->pageTextContains('No email');
$assert->pageTextContains('No quest');
$assert->pageTextContains('No color');
$form_data = [
'name' => 'Sir Lancelot',
'quest' => 'To seek the Grail',
'color' => 'blue',
];
$this->drupalGet(Url::fromRoute('session_example.form'));
$this->submitForm($form_data, 'Save');
$this->clickLink('Check here');
foreach ($form_data as $value) {
$assert->pageTextContains($value);
}
$this->drupalGet(Url::fromRoute('session_example.form'));
$this->submitForm([], 'Clear Session');
$assert->pageTextContains('Session is cleared.');
$this->clickLink('View');
$assert->pageTextContains('No name');
$assert->pageTextContains('No email');
$assert->pageTextContains('No quest');
$assert->pageTextContains('No color');
}
public function testUserIsolation() {
$assert = $this->assertSession();
$form_data = [
'name' => 'Sir Lancelot',
'quest' => 'To seek the Grail',
'color' => 'blue',
];
$this->drupalGet(Url::fromRoute('session_example.form'));
$this->submitForm($form_data, 'Save');
$this->clickLink('Check here');
foreach ($form_data as $value) {
$assert->pageTextContains($value);
}
$this->drupalLogin($this->createUser([
'access content',
]));
$this->drupalGet(Url::fromRoute('session_example.view'));
$assert->statusCodeEquals(200);
$assert->pageTextContains('No name');
$assert->pageTextContains('No email');
$assert->pageTextContains('No quest');
$assert->pageTextContains('No color');
}
}
Classes
| Title |
Deprecated |
Summary |
| SessionExampleTest |
|
Tests the basic functions of the Session Example module. |