You are here

public function EncryptingExistingDataTest::testEncryptingExistingData in Field Encryption 3.0.x

Tests that existing entities can be encrypted.

File

tests/src/Functional/EncryptingExistingDataTest.php, line 24

Class

EncryptingExistingDataTest
Tests encrypting fields on entities that already exist.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEncryptingExistingData() {
  $this
    ->config('field_encrypt.settings')
    ->set('batch_size', 40)
    ->save();
  for ($i = 0; $i < 20; $i++) {
    $this
      ->createTestNode();
  }
  $this
    ->setFieldStorageSettings(TRUE);
  $this
    ->clickLink('run this process manually');
  $this
    ->assertSession()
    ->pageTextContains('There are 80 entities queued for updating to use the latest field encryption settings.');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Process updates');
  $this
    ->checkForMetaRefresh();
  foreach ($this->testNodes as $node) {
    $this
      ->assertConfigurableFieldEncrypted($node
      ->id());
  }
  for ($i = 0; $i < 10; $i++) {
    $this
      ->createTestNode();
  }
  $this
    ->setBaseFieldStorageSettings();
  $this
    ->clickLink('run this process manually');
  $this
    ->assertSession()
    ->pageTextContains('There are 60 entities queued for updating to use the latest field encryption settings.');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Process updates');
  $this
    ->checkForMetaRefresh();
  foreach ($this->testNodes as $node) {
    $this
      ->assertConfigurableFieldEncrypted($node
      ->id());
    $this
      ->assertBaseFieldEncrypted($node
      ->id());
  }
  foreach ($this->testNodes as $idx => $node) {
    $this
      ->drupalGet($node
      ->toUrl());
    $this
      ->assertSession()
      ->pageTextContains('Test node revision' . ($idx + 1));
    $this
      ->assertSession()
      ->pageTextContains('Lorem ipsum dolor sit amet.');
    $this
      ->assertSession()
      ->pageTextContains('one lorem');
    $this
      ->assertSession()
      ->pageTextContains('two lorem');
    $this
      ->assertSession()
      ->pageTextContains('three lorem');
  }

  // Decrypt one of the configurable fields and the base field using the UI.
  $this
    ->drupalGet('admin/config/system/field-encrypt/field-overview');
  $this
    ->xpath('//main//table/tbody/tr[1]')[0]
    ->clickLink('Decrypt');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to remove encryption for field field_test_multi on node?');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Remove field encryption');
  $this
    ->xpath('//main//table/tbody/tr[2]')[0]
    ->clickLink('Decrypt');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to remove encryption for field title on node?');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Remove field encryption');

  // Edit a node before processing the queues. Do this via the UI to avoid
  // issues with static caches in the runner.
  $this
    ->drupalGet($this->testNodes[0]
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->fieldExists('title[0][value]')
    ->setValue('Test node revision1 updated');
  $this
    ->assertSession()
    ->buttonExists('Save')
    ->press();
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('Test node revision1 updated');
  $this
    ->assertSession()
    ->pageTextContains('Lorem ipsum dolor sit amet.');
  $this
    ->assertSession()
    ->pageTextContains('one lorem');
  $this
    ->assertSession()
    ->pageTextContains('two lorem');
  $this
    ->assertSession()
    ->pageTextContains('three lorem');
  $this
    ->drupalGet('admin/config/system/field-encrypt/process-queues');
  $this
    ->assertSession()
    ->pageTextContains('There are 120 entities queued for updating to use the latest field encryption settings.');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Process updates');
  $this
    ->checkForMetaRefresh();
  foreach ($this->testNodes as $idx => $node) {
    $this
      ->drupalGet($node
      ->toUrl());
    $this
      ->assertSession()
      ->pageTextContains('Test node revision' . ($idx + 1) . $idx === 0 ? ' updated' : '');
    $this
      ->assertSession()
      ->pageTextContains('Lorem ipsum dolor sit amet.');
    $this
      ->assertSession()
      ->pageTextContains('one lorem');
    $this
      ->assertSession()
      ->pageTextContains('two lorem');
    $this
      ->assertSession()
      ->pageTextContains('three lorem');
  }
}