class ContentHubUserSessionTest in Acquia Content Hub 8
PHPUnit test for the ContentHubUserSession class.
@coversDefaultClass \Drupal\acquia_contenthub\Session\ContentHubUserSession
@group acquia_contenthub
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\Session\ContentHubUserSessionTest
Expanded class hierarchy of ContentHubUserSessionTest
File
- tests/
src/ Unit/ Session/ ContentHubUserSessionTest.php, line 16
Namespace
Drupal\Tests\acquia_contenthub\Unit\SessionView source
class ContentHubUserSessionTest extends UnitTestCase {
/**
* Enable or disable the backup and restoration of the $GLOBALS array.
*
* @var bool
*/
protected $backupGlobals = FALSE;
/**
* The mock config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* The mock value for the user_role setting.
*
* @var string
*/
protected $userRole;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->method('get')
->willReturnCallback(function ($argument) {
if ($argument == 'acquia_contenthub.entity_config') {
$contenthub_entity_config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMockForAbstractClass();
$contenthub_entity_config
->method('get')
->willReturnCallback(function ($argument) {
if ($argument == 'user_role') {
return $this->userRole;
}
return NULL;
});
return $contenthub_entity_config;
}
return NULL;
});
}
/**
* Test overridden methods.
*
* Test overridden methods and constructor for an anonymous role.
*
* @covers ::isAnonymous
* @covers ::isAuthenticated
* @covers ::__construct
*/
public function testAnonymousRole() {
$this->userRole = AccountInterface::ANONYMOUS_ROLE;
$render_account = new ContentHubUserSession(AccountInterface::ANONYMOUS_ROLE);
$this
->assertTrue($render_account
->isAnonymous());
$this
->assertFalse($render_account
->isAuthenticated());
$expected = [
AccountInterface::ANONYMOUS_ROLE,
];
$this
->assertEquals($render_account
->getRoles(), $expected);
}
/**
* Test overridden methods.
*
* Test overridden methods and constructor for an authenticated role.
*
* @covers ::isAnonymous
* @covers ::isAuthenticated
* @covers ::__construct
*/
public function testAuthenticatedRole() {
$render_account = new ContentHubUserSession(AccountInterface::AUTHENTICATED_ROLE);
$this
->assertFalse($render_account
->isAnonymous());
$this
->assertTrue($render_account
->isAuthenticated());
$expected = [
AccountInterface::AUTHENTICATED_ROLE,
];
$this
->assertEquals($render_account
->getRoles(), $expected);
}
/**
* Test overridden methods.
*
* Test overridden methods and constructor for a custom role.
*
* @covers ::isAnonymous
* @covers ::isAuthenticated
* @covers ::__construct
*/
public function testCustomRole() {
$render_account = new ContentHubUserSession('test_role');
$this
->assertFalse($render_account
->isAnonymous());
$this
->assertTrue($render_account
->isAuthenticated());
$expected = [
AccountInterface::AUTHENTICATED_ROLE,
'test_role',
];
$this
->assertEquals($render_account
->getRoles(), $expected);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentHubUserSessionTest:: |
protected | property | Enable or disable the backup and restoration of the $GLOBALS array. | |
ContentHubUserSessionTest:: |
protected | property | The mock config factory. | |
ContentHubUserSessionTest:: |
protected | property | The mock value for the user_role setting. | |
ContentHubUserSessionTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ContentHubUserSessionTest:: |
public | function | Test overridden methods. | |
ContentHubUserSessionTest:: |
public | function | Test overridden methods. | |
ContentHubUserSessionTest:: |
public | function | Test overridden methods. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |