You are here

class EncryptUserTest in Field Encryption 3.0.x

Tests user field encryption.

@group field_encrypt

Hierarchy

Expanded class hierarchy of EncryptUserTest

File

tests/src/Functional/EncryptUserTest.php, line 15

Namespace

Drupal\Tests\field_encrypt\Functional
View source
class EncryptUserTest extends EncryptTestBase {
  use EntityDefinitionTestTrait;
  use CronRunTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'field',
    'field_ui',
    'text',
    'key',
    'encrypt',
    'encrypt_test',
    'field_encrypt',
    'field_encrypt_test',
  ];

  /**
   * {@inheritdoc}
   *
   * @TODO: Simplify setUp() by extending EncryptTestBase when https://www.drupal.org/node/2692387 lands.
   */
  protected function setUp() {
    parent::setUp();

    // Create an admin user.
    $this->adminUser = $this
      ->drupalCreateUser([
      'access administration pages',
      'administer encrypt',
      'administer keys',
      'administer field encryption',
    ], NULL, TRUE);

    // Set a value for init so it gets encrypted.
    $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();
  }

  /**
   * Set up base fields for test.
   *
   * @param bool $encryption
   *   Whether or not the fields should be encrypted. Defaults to TRUE.
   */
  protected function setFieldStorageSettings($encryption = TRUE) {

    // Set up storage settings for first field.
    $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();
  }

  /**
   * Test encrypting user fields.
   *
   * This test covers encrypting non-revisionable entities as well.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CronRunTrait::cronRun protected function Runs cron on the test site.
EncryptUserTest::$modules public static property
EncryptUserTest::setFieldStorageSettings protected function Set up base fields for test.
EncryptUserTest::setUp protected function @TODO: Simplify setUp() by extending EncryptTestBase when https://www.drupal.org/node/2692387 lands.
EncryptUserTest::testEncryptFieldNormal public function Test encrypting user fields.
EntityDefinitionTestTrait::addBaseField protected function Adds a new base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addBaseFieldIndex protected function Adds a single-field index to the base field.
EntityDefinitionTestTrait::addBundleField protected function Adds a new bundle field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addEntityIndex protected function Adds an index to the 'entity_test_update' entity type's base table.
EntityDefinitionTestTrait::addLongNameBaseField protected function Adds a long-named base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addRevisionableBaseField protected function Adds a new revisionable base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::applyEntityUpdates protected function Applies all the detected valid changes.
EntityDefinitionTestTrait::deleteEntityType protected function Removes the entity type.
EntityDefinitionTestTrait::doEntityUpdate protected function Performs an entity type definition update.
EntityDefinitionTestTrait::doFieldUpdate protected function Performs a field storage definition update.
EntityDefinitionTestTrait::enableNewEntityType protected function Enables a new entity type definition.
EntityDefinitionTestTrait::getUpdatedEntityTypeDefinition protected function Returns an entity type definition, possibly updated to be rev or mul.
EntityDefinitionTestTrait::getUpdatedFieldStorageDefinitions protected function Returns the required rev / mul field definitions for an entity type.
EntityDefinitionTestTrait::makeBaseFieldEntityKey protected function Promotes a field to an entity key.
EntityDefinitionTestTrait::modifyBaseField protected function Modifies the new base field from 'string' to 'text'.
EntityDefinitionTestTrait::modifyBundleField protected function Modifies the new bundle field from 'string' to 'text'.
EntityDefinitionTestTrait::removeBaseField protected function Removes the new base field from the 'entity_test_update' entity type.
EntityDefinitionTestTrait::removeBaseFieldIndex protected function Removes the index added in addBaseFieldIndex().
EntityDefinitionTestTrait::removeBundleField protected function Removes the new bundle field from the 'entity_test_update' entity type.
EntityDefinitionTestTrait::removeEntityIndex protected function Removes the index added in addEntityIndex().
EntityDefinitionTestTrait::renameBaseTable protected function Renames the base table to 'entity_test_update_new'.
EntityDefinitionTestTrait::renameDataTable protected function Renames the data table to 'entity_test_update_data_new'.
EntityDefinitionTestTrait::renameRevisionBaseTable protected function Renames the revision table to 'entity_test_update_revision_new'.
EntityDefinitionTestTrait::renameRevisionDataTable protected function Renames the revision data table to 'entity_test_update_revision_data_new'.
EntityDefinitionTestTrait::resetEntityType protected function Resets the entity type definition.
EntityDefinitionTestTrait::updateEntityTypeToNotRevisionable protected function Updates the 'entity_test_update' entity type not revisionable.
EntityDefinitionTestTrait::updateEntityTypeToNotTranslatable protected function Updates the 'entity_test_update' entity type to not translatable.
EntityDefinitionTestTrait::updateEntityTypeToRevisionable protected function Updates the 'entity_test_update' entity type to revisionable.
EntityDefinitionTestTrait::updateEntityTypeToRevisionableAndTranslatable protected function Updates the 'entity_test_update' entity type to revisionable and translatable.
EntityDefinitionTestTrait::updateEntityTypeToTranslatable protected function Updates the 'entity_test_update' entity type to translatable.