Skip to content
Snippets Groups Projects
Commit 7ade62a6 authored by Kurucz István's avatar Kurucz István
Browse files

Issue #3298837 by nevergone: Refactor test coverage

parent e4e551fb
No related branches found
No related tags found
No related merge requests found
......@@ -3,11 +3,11 @@
namespace Drupal\Tests\redirect_after_logout\Functional;
/**
* Test simple logout, without tokens.
* Redirect testing.
*
* @group redirect_after_logout
*/
class SimpleLogoutTest extends TestBase {
class RedirectTest extends TestBase {
/**
* {@inheritdoc}
......@@ -17,23 +17,36 @@ class SimpleLogoutTest extends TestBase {
];
/**
* Test logout without tokens.
* Test redirecting
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testLogoutWithoutToken() {
$this->drupalLogin($this->adminUser);
$message = $this->randomMachineName();
$this->setRedirectConfig('', $message);
// Not redirect authenticated user.
$this->setRedirectConfig($this->authenticatedUser->toUrl()->toString());
$this->logoutNotRedirectHelper($this->authenticatedUser, $this->authenticatedUser->toUrl()->toString(), $message);
// Redirect regular user.
$this->setRedirectConfig($this->regularUser->toUrl()->toString());
$this->logoutRedirectHelper($this->regularUser, $this->regularUser->toUrl()->toString(), $message);
// Redirect regular user to external website.
// Redirect testing with regular user.
// Frontpage.
$this->setRedirectConfig('<front>', $message);
$this->logoutRedirectHelper($this->regularUser, '/', $message);
// External path.
$this->setRedirectConfig('http://example.com/');
$this->logoutRedirectHelper($this->regularUser, 'http://example.com/', '', FALSE);
// External path with token.
$this->setRedirectConfig('http://[site:name].com/');
$this->logoutRedirectHelper($this->regularUser, 'http://example.com/', '', FALSE);
// Valid node path.
$this->setRedirectConfig('/foobar-example', $message);
$this->logoutRedirectHelper($this->regularUser, '/foobar-example', $message);
// Path only token.
$this->setRedirectConfig('[current-user:url]', $message);
$this->logoutRedirectHelper($this->regularUser, $this->regularUser->toUrl()->toString(), $message);
// Path with token.
$this->setRedirectConfig('/foobar-[site:name]', $message);
$this->logoutRedirectHelper($this->regularUser, '/foobar-example', $message);
}
}
......@@ -17,7 +17,6 @@ class SettingsTest extends TestBase {
* {@inheritdoc}
*/
protected static $modules = [
'node',
'redirect_after_logout',
'token',
];
......@@ -53,25 +52,6 @@ class SettingsTest extends TestBase {
->fieldExists('edit-redirect-after-logout-message');
$this->assertSession()
->buttonExists('edit-submit');
// Check form value saving.
$redirect_after_logout_destination = '<front>';
$redirect_after_logout_message = $this->randomString();
$this->submitForm([
'redirect_after_logout_destination' => $redirect_after_logout_destination,
'redirect_after_logout_message' => $redirect_after_logout_message,
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
$this->assertSession()
->addressEquals('admin/config/system/redirect_after_logout');
$this->assertSession()
->fieldValueEquals('edit-redirect-after-logout-destination', $redirect_after_logout_destination);
$this->assertSession()
->fieldValueEquals('edit-redirect-after-logout-message', $redirect_after_logout_message);
// Check configuration values.
$config = $this->config('redirect_after_logout.settings');
$this::assertEquals($redirect_after_logout_destination, $config->get('destination'));
$this::assertEquals($redirect_after_logout_message, $config->get('message'));
}
/**
......@@ -81,15 +61,6 @@ class SettingsTest extends TestBase {
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function testSettingsFormDestinationValidation() {
// Test node.
$node = $this->drupalCreateNode([
'type' => 'page',
'title' => 'Node title',
]);
$this->createPathAlias($node->toUrl()->toString(), '/foobar-example');
$config = $this->config('system.site');
$config->set('name', 'example');
$config->save();
$this->drupalLogin($this->adminUser);
// Path without slash: invalid.
$this->drupalGet('admin/config/system/redirect_after_logout');
......@@ -105,48 +76,12 @@ class SettingsTest extends TestBase {
], 'Save configuration');
$this->assertSession()
->pageTextContains("Either the path '/$destination' is invalid or you do not have access to it.");
// External path.
$this->submitForm([
'redirect_after_logout_destination' => 'http://example.com/',
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
// External path with token.
$this->submitForm([
'redirect_after_logout_destination' => 'http://[site:name].com/',
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
// Valid node path.
$this->submitForm([
'redirect_after_logout_destination' => '/foobar-example',
], 'Save configuration');
$this->assertSession()
->pageTextContains("The configuration options have been saved.");
// Path only token.
$this->submitForm([
'redirect_after_logout_destination' => '[current-page:url]',
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
// Path with token.
$this->submitForm([
'redirect_after_logout_destination' => '/foobar-[site:name]',
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
// Totally invalid path without token.
$this->submitForm([
'redirect_after_logout_destination' => 'fooBarBaz://fooBarBaz',
], 'Save configuration');
$this->assertSession()
->pageTextContains("The path 'fooBarBaz://fooBarBaz' is invalid.");
// Token with slash: /http:\\example.com\.
$this->submitForm([
'redirect_after_logout_destination' => '/[current-page:url]',
], 'Save configuration');
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
}
}
......@@ -2,11 +2,14 @@
namespace Drupal\Tests\redirect_after_logout\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
/**
* Redirect after logout test base.
......@@ -16,6 +19,7 @@ use Drupal\Tests\BrowserTestBase;
abstract class TestBase extends BrowserTestBase {
use BlockCreationTrait;
use PathAliasTestTrait;
/**
* {@inheritdoc}
......@@ -29,7 +33,7 @@ abstract class TestBase extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stable';
protected $defaultTheme = 'stark';
/**
* The admin user.
......@@ -52,18 +56,43 @@ abstract class TestBase extends BrowserTestBase {
*/
protected $regularUser;
/**
* The test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $testNode;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['administer site configuration']);
$this->adminUser = $this->drupalCreateUser([
'administer site configuration',
'administer users',
]);
$this->authenticatedUser = $this->drupalCreateUser();
$this->regularUser = $this->drupalCreateUser(['redirect user after logout']);
$this->regularUser = $this->drupalCreateUser([
'redirect user after logout',
]);
// Add user login block.
$this->placeBlock('user_login_block', [
'id' => $this->defaultTheme . '_' . strtolower($this->randomMachineName(8)),
]);
// Add neccessary permission for anonymous user role.
$this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), ['access user profiles']);
// Create test node.
$this->createContentType(['type' => 'page']);
$this->testNode = $this->drupalCreateNode([
'type' => 'page',
'title' => 'Example node title',
]);
$this->createPathAlias($this->testNode->toUrl()->toString(), '/foobar-example');
// Create test configuration for tokens.
$config = $this->config('system.site');
$config->set('name', 'example');
$config->save();
}
/**
......@@ -86,7 +115,7 @@ abstract class TestBase extends BrowserTestBase {
/**
* Helper method for logout testing: redirect.
*
* @param \Drupal\Core\Session\AccountInterface $user
* @param \Drupal\user\Entity\User $user
* Tested user.
* @param string $path
* Excepted path.
......@@ -96,7 +125,7 @@ abstract class TestBase extends BrowserTestBase {
* If TRUE, use drupalLogout() method. Otherwise
* use drupalLogoutWithoutCheck() method.
*/
protected function logoutRedirectHelper(AccountInterface $user, $path, $message = '', $logout_check = TRUE) {
protected function logoutRedirectHelper(User $user, string $path, string $message = '', bool $logout_check = TRUE) {
$this->drupalLogin($user);
if ($logout_check) {
$this->drupalLogout();
......@@ -115,7 +144,7 @@ abstract class TestBase extends BrowserTestBase {
/**
* Helper method for logout testing: not redirect.
*
* @param \Drupal\Core\Session\AccountInterface $user
* @param \Drupal\user\Entity\User $user
* Tested user.
* @param string $path
* Not excepted path.
......@@ -125,7 +154,7 @@ abstract class TestBase extends BrowserTestBase {
* If TRUE, use drupalLogout() method. Otherwise
* use drupalLogoutWithoutCheck() method.
*/
protected function logoutNotRedirectHelper(AccountInterface $user, $path, $message = '', $logout_check = TRUE) {
protected function logoutNotRedirectHelper(User $user, string $path, string $message = '', bool $logout_check = TRUE) {
$this->drupalLogin($user);
if ($logout_check) {
$this->drupalLogout();
......@@ -149,15 +178,39 @@ abstract class TestBase extends BrowserTestBase {
* @param string $message
* Redirect message.
*/
protected function setRedirectConfig($destination = '', $message = '') {
protected function setRedirectConfig($destination, $message = '', $account = NULL) {
if ($account !== NULL) {
$this->drupalLogin($account);
}
else {
$this->drupalLogin($this->adminUser);
}
$this->drupalGet('admin/config/system/redirect_after_logout');
$edit = [];
if ($destination !== '') {
$edit['redirect_after_logout_destination'] = $destination;
}
if ($message !== '') {
$edit['edit-redirect-after-logout-message'] = $message;
}
$this->submitForm($edit, 'Save configuration');
// Configuration submitted.
$this->assertSession()
->pageTextContains('The configuration options have been saved.');
// Check submitted form and saved configuration.
$this->assertSession()
->addressEquals('admin/config/system/redirect_after_logout');
$config = $this->config('redirect_after_logout.settings');
if ($destination !== '') {
$config->set('destination', $destination);
$this->assertSession()
->fieldValueEquals('edit-redirect-after-logout-destination', $destination);
$this::assertEquals($destination, $config->get('destination'));
}
if ($message !== '') {
$config->set('message', $message);
$this->assertSession()
->fieldValueEquals('edit-redirect-after-logout-message', $message);
$this::assertEquals($message, $config->get('message'));
}
$config->save();
}
}
<?php
namespace Drupal\Tests\redirect_after_logout\Functional;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
/**
* Test simple logout with tokens.
*
* @group redirect_after_logout
*/
class TokenLogoutTest extends TestBase {
use PathAliasTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'redirect_after_logout',
'token',
];
/**
* Test logout with tokens.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testLogoutWithTokens() {
// Create test node with path alias.
$node = $this->drupalCreateNode([
'type' => 'page',
'title' => 'Node title',
]);
$this->createPathAlias($node->toUrl()->toString(), '/node-alias-' . $this->regularUser->id());
// Set redirect configuration.
$message = $this->randomMachineName();
$this->setRedirectConfig('/node-alias-[current-user:uid]', $message);
// Test redirect.
$this->logoutRedirectHelper($this->regularUser, '/node-alias-' . $this->regularUser->id(), $message);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment