View source
<?php
namespace Drupal\Tests\shield\Functional;
use Drupal\key\Entity\Key;
use Drupal\Tests\BrowserTestBase;
class ShieldTest extends BrowserTestBase {
public static $modules = [
'shield',
'key',
];
protected function setUp() {
parent::setUp();
$this
->config('shield.settings')
->set('shield_enable', FALSE)
->set('credential_provider', 'shield')
->set('credentials.shield.user', 'user')
->set('credentials.shield.pass', 'password')
->set('print', 'Hello world!')
->save();
Key::create([
'id' => 'shield_test',
'label' => 'Shield test',
'key_type' => "user_password",
'key_type_settings' => [],
'key_provider' => 'file',
'key_provider_settings' => [
'file_location' => drupal_get_path('module', 'shield') . '/tests/files/shield_test.key',
'strip_line_breaks' => FALSE,
],
])
->save();
}
public function testShieldEnable() {
$this
->drupalGet('user');
$this
->assertSession()
->statusCodeEquals(200);
$this
->config('shield.settings')
->set('shield_enable', TRUE)
->save();
$this
->drupalGet('user');
$this
->assertSession()
->statusCodeEquals(401);
}
public function testAuthMessage() {
$this
->config('shield.settings')
->set('shield_enable', TRUE)
->save();
$this
->drupalGet('user');
$this
->assertSession()
->responseHeaderContains('WWW-Authenticate', 'Basic realm="Hello world!"');
$this
->config('shield.settings')
->set('print', 'Hello entire world!')
->save();
$this
->drupalGet('user');
$this
->assertSession()
->responseHeaderContains('WWW-Authenticate', 'Basic realm="Hello entire world!"');
}
public function testShieldCred() {
$this
->config('shield.settings')
->set('shield_enable', TRUE)
->save();
$this
->drupalGet('user');
$this
->assertSession()
->statusCodeEquals(401);
$this
->drupalGet('user', [], [
'Authorization' => 'Basic ' . base64_encode('user:password'),
]);
$this
->assertSession()
->statusCodeEquals(200);
}
public function testShieldKey() {
$path_to_test = 'user';
$assert_session = $this
->assertSession();
$admin = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($admin);
$this
->drupalGet('admin/config/system/keys');
$assert_session
->pageTextContains('Shield Test');
$assert_session
->statusCodeEquals(200);
$shield_config = \Drupal::configFactory()
->getEditable('shield.settings');
$shield_config
->set('shield_enable', TRUE);
$shield_config
->set('credential_provider', 'multikey');
$shield_config
->set('credentials.multikey.user_pass_key', 'shield_test');
$shield_config
->set('print', 'Hello world!');
$shield_config
->save();
$this
->drupalGet($path_to_test);
$assert_session
->responseHeaderContains('WWW-Authenticate', 'Basic realm="Hello world!"');
$assert_session
->statusCodeEquals(401);
$key_values = \Drupal::service('key.repository')
->getKey('shield_test')
->getKeyValues();
$user_pass = $key_values['username'] . ':' . $key_values['password'];
$this
->drupalGet($path_to_test, [], [
'Authorization' => 'Basic ' . base64_encode("{$user_pass}"),
]);
$assert_session
->statusCodeEquals(200);
$this
->drupalGet('admin/config/system/shield');
$assert_session
->statusCodeEquals(200);
$assert_session
->fieldValueEquals('credentials[credential_provider]', 'multikey');
$assert_session
->fieldValueEquals('credentials[providers][multikey][user_pass_key]', 'shield_test');
$assert_session
->fieldValueEquals('shield_print', 'Hello world!');
}
}