Skip to content

Commit

Permalink
Merge pull request #16 from openeuropa/OPENEUROPA-166
Browse files Browse the repository at this point in the history
OPENEUROPA-166: Added Login/Logout Block.
  • Loading branch information
voidtek authored Aug 24, 2018
2 parents 1c16330 + 72e605d commit 0e52061
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Plugin/Block/LoginBlock.php
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']);
}

}
83 changes: 83 additions & 0 deletions tests/Kernel/LoginBlockTest.php
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'));
}

}

0 comments on commit 0e52061

Please sign in to comment.