You are here

public function FieldEncryptApiHooksTest::testHooks in Field Encryption 3.0.x

Tests field_encrypt hooks.

File

tests/src/Functional/FieldEncryptApiHooksTest.php, line 61

Class

FieldEncryptApiHooksTest
Tests Field Encrypt's API hooks.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testHooks() {
  FieldStorageConfig::loadByName('node', 'field_test_single')
    ->setThirdPartySetting('field_encrypt', 'placeholders', [
    'value' => '✨',
  ])
    ->save();
  \Drupal::state()
    ->set('field_encrypt_test.hook_field_encrypt_unencrypted_storage_value_alter', TRUE);
  $this
    ->setBaseFieldStorageSettings(TRUE);
  $this
    ->setFieldStorageSettings(TRUE);

  // Save test entity.
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertSession()
    ->fieldExists('title[0][value]')
    ->setValue('Test title');
  $this
    ->assertSession()
    ->fieldExists('field_test_single[0][value]')
    ->setValue('Test single value field');
  $this
    ->assertSession()
    ->buttonExists('Save')
    ->press();

  // Check the node title is displayed unencrypted.
  $this
    ->assertSession()
    ->elementTextContains('css', 'h1', "Test title");
  $result = \Drupal::database()
    ->query("SELECT title FROM {node_field_data} WHERE nid = 1")
    ->fetchField();
  $this
    ->assertEquals('🐒', $result);
  $result = \Drupal::database()
    ->query("SELECT field_test_single_value FROM {node__field_test_single} WHERE entity_id = 1")
    ->fetchField();
  $this
    ->assertEquals('✨', $result);
  \Drupal::state()
    ->set('field_encrypt_test.hook_field_encrypt_unencrypted_storage_value_alter', FALSE);
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  \Drupal::state()
    ->set('field_encrypt_test.hook_field_encrypt_allow_encryption', TRUE);
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertSession()
    ->buttonExists('Save')
    ->press();
  $result = \Drupal::database()
    ->query("SELECT title FROM {node_field_data} WHERE nid = 1")
    ->fetchField();
  $this
    ->assertEquals('Test title', $result);

  // Check the node title is displayed unencrypted.
  $this
    ->assertSession()
    ->elementTextContains('css', 'h1', "Test title");

  // Assert the arguments passed to hook are as expected.
  $this
    ->assertSession()
    ->pageTextContains("Allow encryption hook: Entity title: Test title");
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertSession()
    ->fieldExists('status[value]')
    ->uncheck();
  $this
    ->assertSession()
    ->buttonExists('Save')
    ->press();
  $result = \Drupal::database()
    ->query("SELECT title FROM {node_field_data} WHERE nid = 1")
    ->fetchField();
  $this
    ->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $result);

  // Check the node title is displayed unencrypted.
  $this
    ->assertSession()
    ->elementTextContains('css', 'h1', "Test title");

  // Assert the arguments passed to hook are as expected.
  $this
    ->assertSession()
    ->pageTextContains("Allow encryption hook: Entity title: Test title");
}