InitialInstallBehaviorsTest.php in Password Policy 8.3
File
tests/src/Functional/InitialInstallBehaviorsTest.php
View source
<?php
namespace Drupal\Tests\password_policy\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\password_policy\Entity\PasswordPolicy;
use Drupal\user\Entity\User;
class InitialInstallBehaviorsTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'system',
'user',
'node',
'dblog',
'ctools',
'config',
'field',
'datetime',
];
public function testInitialInstallBehaviors() {
$user = $this
->drupalCreateUser();
$this->container
->get('module_installer')
->install([
'password_policy',
]);
$this
->assertNotNull(\Drupal::state()
->get('password_policy.install_time'), 'Install time state variable not set during install.');
$user_instance = User::load($user
->id());
$this
->assertNull($user_instance
->get('field_last_password_reset')->value, 'Existing user last password reset was set on module install.');
$this
->assertNull($user_instance
->get('field_password_expiration')->value, 'Existing user password expiration field was set on module install.');
$policy = PasswordPolicy::create([
'id' => 'test',
'label' => 'test',
'password_reset' => '1',
'roles' => [
'authenticated',
],
]);
$policy
->save();
\Drupal::service('cron')
->run();
$user_instance = User::load($user
->id());
$this
->assertNull($user_instance
->get('field_last_password_reset')->value, 'Existing user last password reset was set by cron run.');
$this
->assertNull($user_instance
->get('field_password_expiration')->value, 'Existing user password expiration field was set by cron run.');
$timestamp = \Drupal::service("date.formatter")
->format(0, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE);
\Drupal::state()
->set('password_policy.install_time', $timestamp);
\Drupal::service('cron')
->run();
$user_instance = User::load($user
->id());
$this
->assertEquals($user_instance
->get('field_password_expiration')->value, 1, 'Password not reset by cron run.');
}
}