You are here

public function TfaContextTest::testIsTfaRequired in Two-factor Authentication (TFA) 8

@covers ::isTfaRequired

File

tests/src/Unit/TfaContextTest.php, line 140

Class

TfaContextTest
@coversDefaultClass \Drupal\tfa\TfaContext

Namespace

Drupal\Tests\tfa\Unit

Code

public function testIsTfaRequired() {

  // User has setup TFA.
  $user_data = $this
    ->prophesize(UserDataInterface::class);
  $user_data
    ->get('tfa', 3, 'tfa_user_settings')
    ->willReturn([
    'status' => 1,
    'saved' => FALSE,
    'data' => [
      'plugins' => [
        'foo',
      ],
    ],
    'validation_skipped' => 1,
  ]);
  $this->userData = $user_data
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertTrue($fixture
    ->isTfaRequired());

  // Not setup, no required roles matching the user.
  $user_data
    ->get('tfa', 3, 'tfa_user_settings')
    ->willReturn([
    'status' => 0,
    'saved' => FALSE,
    'data' => [
      'plugins' => [
        'foo',
      ],
    ],
    'validation_skipped' => 1,
  ]);
  $this->userData = $user_data
    ->reveal();
  $settings = $this
    ->prophesize(ImmutableConfig::class);
  $settings
    ->get('default_validation_plugin')
    ->willReturn('foo');
  $settings
    ->get('required_roles')
    ->willReturn([
    'foo' => 'foo',
  ]);
  $config_factory = $this
    ->prophesize(ConfigFactoryInterface::class);
  $config_factory
    ->get('tfa.settings')
    ->willReturn($settings
    ->reveal());
  $this->configFactory = $config_factory
    ->reveal();
  $user = $this
    ->prophesize(UserInterface::class);
  $user
    ->id()
    ->willReturn(3);
  $user
    ->getRoles()
    ->willReturn([
    'bar' => 'bar',
  ]);
  $this->user = $user
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertFalse($fixture
    ->isTfaRequired());

  // Setup, matching roles.
  $user_data
    ->get('tfa', 3, 'tfa_user_settings')
    ->willReturn([
    'status' => 1,
    'saved' => FALSE,
    'data' => [
      'plugins' => [
        'foo',
      ],
    ],
    'validation_skipped' => 1,
  ]);
  $this->userData = $user_data
    ->reveal();
  $user = $this
    ->prophesize(UserInterface::class);
  $user
    ->id()
    ->willReturn(3);
  $user
    ->getRoles()
    ->willReturn([
    'foo' => 'foo',
    'bar' => 'bar',
  ]);
  $this->user = $user
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertTrue($fixture
    ->isTfaRequired());
}