View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Url;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\Tests\BrowserTestBase;
class UserBlocksTest extends BrowserTestBase {
protected static $modules = [
'block',
'views',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('user_login_block', [
'id' => 'user_blocks_test_user_login_block',
]);
$this
->drupalLogout($this->adminUser);
}
public function testUserLoginBlockVisibility() {
$paths = [
'node' => TRUE,
'user/login' => FALSE,
'user/register' => TRUE,
'user/password' => TRUE,
];
foreach ($paths as $path => $expected_visibility) {
$this
->drupalGet($path);
if ($expected_visibility) {
$this
->assertSession()
->elementExists('xpath', '//div[@id="block-user-blocks-test-user-login-block" and @role="form"]');
}
else {
$this
->assertSession()
->elementNotExists('xpath', '//div[@id="block-user-blocks-test-user-login-block" and @role="form"]');
}
}
}
public function testUserLoginBlock() {
$user = $this
->drupalCreateUser([
'administer permissions',
]);
$edit = [];
$edit['name'] = $user
->getAccountName();
$edit['pass'] = $user->passRaw;
$this
->drupalGet('admin/people/permissions');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextNotContains('User login');
$this
->assertSession()
->addressEquals(Url::fromRoute('user.admin_permissions'));
$this
->drupalLogout();
$this
->drupalGet('filter/tips');
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextNotContains('User login');
$this
->assertSession()
->responseMatches('!<title.*?Compose tips.*?</title>!');
$this
->drupalLogout();
$this
->drupalGet('filter/tips', [
'query' => [
'foo' => 'bar',
],
]);
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextNotContains('User login');
$this
->assertSession()
->responseMatches('!<title.*?Compose tips.*?</title>!');
$this
->assertStringContainsString('/filter/tips?foo=bar', $this
->getUrl(), 'Correct query arguments are displayed after login');
$this
->drupalLogout();
$this
->drupalGet('filter/tips', [
'query' => [
'foo' => 'baz',
],
]);
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextNotContains('User login');
$this
->assertSession()
->responseMatches('!<title.*?Compose tips.*?</title>!');
$this
->assertStringContainsString('/filter/tips?foo=baz', $this
->getUrl(), 'Correct query arguments are displayed after login');
$this
->drupalLogout();
$this
->drupalGet('http://example.com/', [
'external' => FALSE,
]);
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->addressEquals($user
->toUrl('canonical'));
$this
->drupalLogout();
$edit = [];
$edit['name'] = 'foo';
$edit['pass'] = 'invalid password';
$this
->drupalGet('filter/tips');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('Unrecognized username or password. Forgot your password?');
$this
->drupalGet('filter/tips');
$this
->assertSession()
->pageTextNotContains('Unrecognized username or password. Forgot your password?');
}
}