You are here

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

@covers ::remainingSkips

File

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

Class

TfaContextTest
@coversDefaultClass \Drupal\tfa\TfaContext

Namespace

Drupal\Tests\tfa\Unit

Code

public function testRemainingSkips() {

  // No allowed skips.
  $settings = $this
    ->prophesize(ImmutableConfig::class);
  $settings
    ->get('default_validation_plugin')
    ->willReturn(FALSE);
  $settings
    ->get('validation_skip')
    ->willReturn(0);
  $config_factory = $this
    ->prophesize(ConfigFactoryInterface::class);
  $config_factory
    ->get('tfa.settings')
    ->willReturn($settings
    ->reveal());
  $this->configFactory = $config_factory
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertFalse($fixture
    ->remainingSkips());

  // 3 allowed skips, user hasn't skipped any.
  $settings
    ->get('validation_skip')
    ->willReturn(3);
  $config_factory = $this
    ->prophesize(ConfigFactoryInterface::class);
  $config_factory
    ->get('tfa.settings')
    ->willReturn($settings
    ->reveal());
  $this->configFactory = $config_factory
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertEquals(3, $fixture
    ->remainingSkips());

  // 3 allowed skips, user has skipped 2.
  $user_data = $this
    ->prophesize(UserDataInterface::class);
  $user_data
    ->get('tfa', 3, 'tfa_user_settings')
    ->willReturn([
    'status' => 1,
    'saved' => FALSE,
    'data' => [
      'plugins' => [
        'foo',
      ],
    ],
    'validation_skipped' => 2,
  ]);
  $this->userData = $user_data
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertEquals(1, $fixture
    ->remainingSkips());

  // User has exceeded attempts, check for 0 return.
  $user_data
    ->get('tfa', 3, 'tfa_user_settings')
    ->willReturn([
    'status' => 1,
    'saved' => FALSE,
    'data' => [
      'plugins' => [
        'foo',
      ],
    ],
    'validation_skipped' => 9,
  ]);
  $this->userData = $user_data
    ->reveal();
  $fixture = $this
    ->getFixture();
  $this
    ->assertEquals(0, $fixture
    ->remainingSkips());
}