-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from openeuropa/OPENEUROPA-166
OPENEUROPA-166: Added Login/Logout Block.
- Loading branch information
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\oe_authentication\Plugin\Block; | ||
|
||
use Drupal\Core\Block\BlockBase; | ||
use Drupal\Core\Cache\Cache; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\Core\Session\AccountProxyInterface; | ||
use Drupal\Core\Url; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides a block that renders links to login or logout. | ||
* | ||
* @Block( | ||
* id = "oe_authentication_login_block", | ||
* admin_label = @Translation("EU Login Link Block"), | ||
* ) | ||
*/ | ||
class LoginBlock extends BlockBase implements ContainerFactoryPluginInterface { | ||
|
||
/** | ||
* Drupal\Core\Session\AccountProxy definition. | ||
* | ||
* @var \Drupal\Core\Session\AccountProxyInterface | ||
*/ | ||
protected $currentUser; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $current_user) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
$this->currentUser = $current_user; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('current_user') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function build() { | ||
if ($this->currentUser->isAnonymous()) { | ||
return [ | ||
'#type' => 'link', | ||
'#title' => $this->t('Log in'), | ||
'#url' => Url::fromRoute('user.login'), | ||
]; | ||
} | ||
|
||
return [ | ||
'#type' => 'link', | ||
'#title' => $this->t('Log out'), | ||
'#url' => Url::fromRoute('user.logout'), | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheContexts() { | ||
return Cache::mergeContexts(parent::getCacheContexts(), ['user.roles:anonymous']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\oe_authentication\Kernel; | ||
|
||
use Drupal\Core\Url; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\user\Entity\User; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
/** | ||
* Test login block rendering. | ||
*/ | ||
class LoginBlockTest extends KernelTestBase { | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
public static $modules = [ | ||
'block', | ||
'oe_authentication', | ||
'system', | ||
'user', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() { | ||
parent::setUp(); | ||
$this->installConfig(['system', 'block', 'user']); | ||
$this->installEntitySchema('user'); | ||
$this->installSchema('system', ['sequences']); | ||
} | ||
|
||
/** | ||
* Test login block rendering. | ||
*/ | ||
public function testLoginBlockRendering(): void { | ||
|
||
// Setup and render login block. | ||
$block_manager = $this->container->get('plugin.manager.block'); | ||
$renderer = $this->container->get('renderer'); | ||
|
||
/** @var \Drupal\oe_authentication\Plugin\Block\LoginBlock $plugin_block */ | ||
$plugin_block = $block_manager->createInstance('oe_authentication_login_block'); | ||
$build = $plugin_block->build(); | ||
$block = (string) $renderer->renderRoot($build); | ||
|
||
$crawler = new Crawler($block); | ||
|
||
// Make sure the login link is present. | ||
$link = $crawler->filter('a'); | ||
$this->assertEquals(t('Log in'), $link->text()); | ||
$this->assertEquals(Url::fromRoute('user.login')->toString(), $link->attr('href')); | ||
|
||
// Create a user to login. | ||
$user1 = User::create([ | ||
'name' => 'oe_user1', | ||
'mail' => '[email protected]', | ||
]); | ||
|
||
$user1->activate(); | ||
$user1->save(); | ||
|
||
// Simulate a login of this user. | ||
$this->container->get('current_user')->setAccount($user1); | ||
|
||
// Render the block again. | ||
$build = $plugin_block->build(); | ||
$block = (string) $renderer->renderRoot($build); | ||
|
||
// Asserts if the text changed, and log out text is now present. | ||
$crawler = new Crawler($block); | ||
$link = $crawler->filter('a'); | ||
$this->assertEquals(t('Log out'), $link->text()); | ||
$this->assertEquals(Url::fromRoute('user.logout')->toString(), $link->attr('href')); | ||
} | ||
|
||
} |