UserBlockTest.php in Rules 8.3
File
tests/src/Unit/Integration/RulesAction/UserBlockTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Drupal\user\UserInterface;
class UserBlockTest extends RulesEntityIntegrationTestBase {
const AUTHENTICATED = TRUE;
const ANONYMOUS = FALSE;
const ACTIVE = TRUE;
const BLOCKED = FALSE;
protected $action;
protected $sessionManager;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('user');
$this->sessionManager = $this
->prophesize(SessionManagerInterface::class);
$this->container
->set('session_manager', $this->sessionManager
->reveal());
$this->action = $this->actionManager
->createInstance('rules_user_block');
}
public function testSummary() {
$this
->assertEquals('Block a user', $this->action
->summary());
}
public function testBlockUserWithValidUser() {
$user = $this
->getUserMock(self::ACTIVE, self::AUTHENTICATED);
$user
->block()
->shouldBeCalledTimes(1);
$user
->id()
->willReturn('123')
->shouldBeCalledTimes(1);
$this->sessionManager
->delete('123')
->shouldBeCalledTimes(1);
$this->action
->setContextValue('user', $user
->reveal());
$this->action
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
public function testBlockUserWithActiveAnonymousUser() {
$user = $this
->getUserMock(self::ACTIVE, self::ANONYMOUS);
$user
->block()
->shouldNotBeCalled();
$this->sessionManager
->delete()
->shouldNotBeCalled();
$this->action
->setContextValue('user', $user
->reveal());
$this->action
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [], 'Action returns nothing for auto saving since the user has not been altered.');
}
public function testBlockUserWithBlockedAuthenticatedUser() {
$user = $this
->getUserMock(self::BLOCKED, self::AUTHENTICATED);
$user
->block()
->shouldNotBeCalled();
$this->sessionManager
->delete()
->shouldNotBeCalled();
$this->action
->setContextValue('user', $user
->reveal());
$this->action
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [], 'Action returns nothing for auto saving since the user has not been altered.');
}
public function testBlockUserWithBlockedAnonymousUser() {
$user = $this
->getUserMock(self::BLOCKED, self::ANONYMOUS);
$user
->block()
->shouldNotBeCalled();
$this->sessionManager
->delete()
->shouldNotBeCalled();
$this->action
->setContextValue('user', $user
->reveal());
$this->action
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [], 'Action returns nothing for auto saving since the user has not been altered.');
}
protected function getUserMock($active, $authenticated) {
$user = $this
->prophesizeEntity(UserInterface::class);
$user
->isActive()
->willReturn($active);
$user
->isAuthenticated()
->willReturn($authenticated);
return $user;
}
}