EncryptUserTest.php in Field Encryption 3.0.x
File
tests/src/Functional/EncryptUserTest.php
View source
<?php
namespace Drupal\Tests\field_encrypt\Functional;
use Drupal\field_encrypt\ProcessEntities;
use Drupal\Tests\encrypt\Functional\EncryptTestBase;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
use Drupal\Tests\Traits\Core\CronRunTrait;
class EncryptUserTest extends EncryptTestBase {
use EntityDefinitionTestTrait;
use CronRunTrait;
public static $modules = [
'field',
'field_ui',
'text',
'key',
'encrypt',
'encrypt_test',
'field_encrypt',
'field_encrypt_test',
];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer encrypt',
'administer keys',
'administer field encryption',
], NULL, TRUE);
$this->adminUser->init = 'test@example.com';
$this->adminUser
->save();
$this
->drupalLogin($this->adminUser);
$this
->config('field_encrypt.settings')
->set('encryption_profile', 'encryption_profile_1')
->save();
}
protected function setFieldStorageSettings($encryption = TRUE) {
$this
->drupalGet('admin/config/system/field-encrypt/entity-types');
$this
->assertSession()
->fieldExists('entity_type')
->selectOption('User');
$this
->submitForm([], 'Save configuration');
if ($encryption) {
$this
->assertSession()
->fieldExists('base_fields[init]')
->check();
}
else {
$this
->assertSession()
->fieldExists('base_fields[init]')
->uncheck();
}
$this
->submitForm([], 'Save configuration');
$this
->assertSession()
->pageTextContains('Updated encryption settings for User base fields.');
$this
->rebuildAll();
}
public function testEncryptFieldNormal() {
$this
->setFieldStorageSettings(TRUE);
$this
->drupalGet('admin/config/system/field-encrypt/process-queues');
$this
->assertSession()
->pageTextContains('There are 4 entities queued for updating to use the latest field encryption settings.');
$this
->cronRun();
$this
->drupalGet('admin/config/system/field-encrypt/process-queues');
$this
->assertSession()
->pageTextContains('There are 0 entities queued for updating to use the latest field encryption settings.');
$result = \Drupal::database()
->query("SELECT init FROM {users_field_data} WHERE uid = :uid", [
':uid' => $this->adminUser
->id(),
])
->fetchAll();
$this
->assertCount(1, $result);
foreach ($result as $record) {
$this
->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $record->init);
}
}
}