You are here

class ContentHubUserSessionTest in Acquia Content Hub 8

PHPUnit test for the ContentHubUserSession class.

@coversDefaultClass \Drupal\acquia_contenthub\Session\ContentHubUserSession

@group acquia_contenthub

Hierarchy

Expanded class hierarchy of ContentHubUserSessionTest

File

tests/src/Unit/Session/ContentHubUserSessionTest.php, line 16

Namespace

Drupal\Tests\acquia_contenthub\Unit\Session
View 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

Namesort descending Modifiers Type Description Overrides
ContentHubUserSessionTest::$backupGlobals protected property Enable or disable the backup and restoration of the $GLOBALS array.
ContentHubUserSessionTest::$configFactory protected property The mock config factory.
ContentHubUserSessionTest::$userRole protected property The mock value for the user_role setting.
ContentHubUserSessionTest::setUp protected function Overrides UnitTestCase::setUp
ContentHubUserSessionTest::testAnonymousRole public function Test overridden methods.
ContentHubUserSessionTest::testAuthenticatedRole public function Test overridden methods.
ContentHubUserSessionTest::testCustomRole public function Test overridden methods.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.