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 {
public static $modules = [
'block',
'views',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('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);
$elements = $this
->xpath('//div[contains(@class,"block-user-login-block") and @role="form"]');
if ($expected_visibility) {
$this
->assertTrue(!empty($elements), 'User login block in path "' . $path . '" should be visible');
}
else {
$this
->assertTrue(empty($elements), 'User login block in path "' . $path . '" should not be visible');
}
}
}
public function testUserLoginBlock() {
$user = $this
->drupalCreateUser([
'administer permissions',
]);
$edit = [];
$edit['name'] = $user
->getAccountName();
$edit['pass'] = $user->passRaw;
$this
->drupalPostForm('admin/people/permissions', $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertUrl(Url::fromRoute('user.admin_permissions', [], [
'absolute' => TRUE,
])
->toString(), [], 'Still on the same page after login for access denied page');
$this
->drupalLogout();
$this
->drupalGet('filter/tips');
$this
->assertEqual('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER));
$this
->drupalPostForm(NULL, $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertPattern('!<title.*?Compose tips.*?</title>!');
$this
->drupalLogout();
$this
->drupalGet('filter/tips', [
'query' => [
'foo' => 'bar',
],
]);
$this
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER));
$this
->drupalPostForm(NULL, $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertPattern('!<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
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER));
$this
->drupalPostForm(NULL, $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertPattern('!<title.*?Compose tips.*?</title>!');
$this
->assertStringContainsString('/filter/tips?foo=baz', $this
->getUrl(), 'Correct query arguments are displayed after login');
$this
->drupalLogout();
$this
->drupalPostForm('http://example.com/', $edit, t('Log in'), [
'external' => FALSE,
]);
$this
->assertUrl($user
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), [], 'Redirected to user profile page after login from the frontpage');
$this
->drupalLogout();
$edit = [];
$edit['name'] = 'foo';
$edit['pass'] = 'invalid password';
$this
->drupalPostForm('filter/tips', $edit, t('Log in'));
$this
->assertText(t('Unrecognized username or password. Forgot your password?'));
$this
->drupalGet('filter/tips');
$this
->assertNoText(t('Unrecognized username or password. Forgot your password?'));
}
}