View source
<?php
namespace Drupal\Tests\securelogin\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class SecureLoginTestBlock extends SecureLoginTestBase {
use StringTranslationTrait;
protected $enableSecureLogin = FALSE;
protected $profile = 'testing_config_import';
public static $modules = [
'block',
'node',
'views',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'administer site configuration',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('user_login_block');
$this
->drupalLogout();
$this->container
->get('module_installer')
->install([
'securelogin',
]);
}
public function testUserLoginBlock() {
global $base_url;
global $base_path;
$maximumMetaRefreshCount = $this->maximumMetaRefreshCount;
$this->maximumMetaRefreshCount = 0;
if (method_exists($this
->getSession()
->getDriver(), 'getClient')) {
$this
->getSession()
->getDriver()
->getClient()
->followRedirects(FALSE);
}
$this
->drupalGet($this
->httpUrl('node'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/node'), 'Location header uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('node'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/node'), 'Location header uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('admin'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/admin'), 'Location header uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('admin/config'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/admin/config'), 'Location header uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('no-page-by-this-name'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/no-page-by-this-name'), 'Location header uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('nor-this-one'));
$this
->assertSession()
->statusCodeEquals(301);
$this
->assertSame(0, strpos($this
->getSession()
->getResponseHeader('Location'), str_replace('http://', 'https://', $base_url) . '/index.php/nor-this-one'), 'Location header uses the secure base URL.');
$this
->assertTrue($this
->config('securelogin.settings')
->get('secure_forms'), 'Secure forms settings is enabled by default.');
if ($this->isSecure) {
$this->maximumMetaRefreshCount = $maximumMetaRefreshCount;
if (method_exists($this
->getSession()
->getDriver(), 'getClient')) {
$this
->getSession()
->getDriver()
->getClient()
->followRedirects(TRUE);
}
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/people/securelogin');
$edit['secure_forms'] = FALSE;
$this
->submitForm($edit, $this
->t('Save configuration'));
$this->container
->get('config.factory')
->reset('securelogin.settings');
$this
->assertFalse($this
->config('securelogin.settings')
->get('secure_forms'), 'Secure forms is disabled.');
$this
->drupalGet('user/logout');
$this->maximumMetaRefreshCount = 0;
if (method_exists($this
->getSession()
->getDriver(), 'getClient')) {
$this
->getSession()
->getDriver()
->getClient()
->followRedirects(FALSE);
}
}
else {
$this->container
->get('config.typed')
->clearCachedDefinitions();
$this
->config('securelogin.settings')
->set('secure_forms', FALSE)
->save();
drupal_flush_all_caches();
}
$this
->drupalGet($this
->httpUrl('node'));
$this
->assertSession()
->statusCodeEquals(200);
$element = $this
->assertSession()
->elementAttributeExists('css', 'form', 'action');
$this
->assertSame(str_replace('http://', 'https://', $base_url) . "/index.php/node?destination={$base_path}index.php/node", $element
->getAttribute('action'), 'The action attribute uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('admin'));
$this
->assertSession()
->statusCodeEquals(403);
$element = $this
->assertSession()
->elementAttributeExists('css', 'form', 'action');
$this
->assertSame(str_replace('http://', 'https://', $base_url) . "/index.php/system/403?destination={$base_path}index.php/admin", $element
->getAttribute('action'), 'The action attribute uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('admin/config'));
$this
->assertSession()
->statusCodeEquals(403);
$element = $this
->assertSession()
->elementAttributeExists('css', 'form', 'action');
$this
->assertSame(str_replace('http://', 'https://', $base_url) . "/index.php/system/403?destination={$base_path}index.php/admin/config", $element
->getAttribute('action'), 'The action attribute uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('no-page-by-this-name'));
$this
->assertSession()
->statusCodeEquals(404);
$element = $this
->assertSession()
->elementAttributeExists('css', 'form', 'action');
$this
->assertSame(str_replace('http://', 'https://', $base_url) . "/index.php/system/404?destination={$base_path}index.php/", $element
->getAttribute('action'), 'The action attribute uses the secure base URL.');
$this
->drupalGet($this
->httpUrl('nor-this-one'));
$this
->assertSession()
->statusCodeEquals(404);
$element = $this
->assertSession()
->elementAttributeExists('css', 'form', 'action');
$this
->assertSame(str_replace('http://', 'https://', $base_url) . "/index.php/system/404?destination={$base_path}index.php/", $element
->getAttribute('action'), 'The action attribute uses the secure base URL.');
if (method_exists($this
->getSession()
->getDriver(), 'getClient')) {
$this
->getSession()
->getDriver()
->getClient()
->followRedirects(TRUE);
}
$this->maximumMetaRefreshCount = $maximumMetaRefreshCount;
}
}