class FieldEncryptApiHooksTest in Field Encryption 3.0.x
Tests Field Encrypt's API hooks.
@group field_encrypt
Hierarchy
- class \Drupal\Tests\field_encrypt\Functional\FieldEncryptTestBase extends \Drupal\Tests\encrypt\Functional\EncryptTestBase uses NodeCreationTrait
- class \Drupal\Tests\field_encrypt\Functional\FieldEncryptApiHooksTest uses NodeCreationTrait, CronRunTrait
Expanded class hierarchy of FieldEncryptApiHooksTest
See also
hook_field_encrypt_unencrypted_storage_value_alter()
hook_field_encrypt_allow_encryption()
File
- tests/
src/ Functional/ FieldEncryptApiHooksTest.php, line 17
Namespace
Drupal\Tests\field_encrypt\FunctionalView source
class FieldEncryptApiHooksTest extends FieldEncryptTestBase {
use CronRunTrait;
use NodeCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = [
'node',
'field',
'text',
'key',
'encrypt',
'encrypt_test',
'field_encrypt',
'field_encrypt_test',
];
/**
* Set up base fields for test.
*
* @param bool $encryption
* Whether or not the fields should be encrypted. Defaults to TRUE.
*/
protected function setBaseFieldStorageSettings($encryption = TRUE) {
// Set up storage settings for first field.
$this
->drupalGet('admin/config/system/field-encrypt/entity-types');
$this
->assertSession()
->fieldExists('entity_type')
->selectOption('Content');
$this
->submitForm([], 'Save configuration');
if ($encryption) {
$this
->assertSession()
->fieldExists('base_fields[title]')
->check();
}
else {
$this
->assertSession()
->fieldExists('base_fields[title]')
->uncheck();
}
$this
->submitForm([], 'Save configuration');
$this
->assertSession()
->pageTextContains('Updated encryption settings for Content base fields.');
$this
->rebuildAll();
}
/**
* Tests field_encrypt hooks.
*/
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");
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CronRunTrait:: |
protected | function | Runs cron on the test site. | |
FieldEncryptApiHooksTest:: |
public static | property |
Overrides FieldEncryptTestBase:: |
|
FieldEncryptApiHooksTest:: |
protected | function | Set up base fields for test. | |
FieldEncryptApiHooksTest:: |
public | function | Tests field_encrypt hooks. | |
FieldEncryptTestBase:: |
protected | property | The entity manager service. | 1 |
FieldEncryptTestBase:: |
protected | property | The page node type. | |
FieldEncryptTestBase:: |
protected | property | A test node. | |
FieldEncryptTestBase:: |
protected | function | Creates a test node. | 1 |
FieldEncryptTestBase:: |
protected | function | Set up storage settings for test fields. | 1 |
FieldEncryptTestBase:: |
protected | function | Set up translation settings for content translation test. | |
FieldEncryptTestBase:: |
protected | function | @TODO: Simplify setUp() by extending EncryptTestBase when https://www.drupal.org/node/2692387 lands. | 1 |
NodeCreationTrait:: |
protected | function | Creates a node based on default settings. | |
NodeCreationTrait:: |
public | function | Get a node from the database based on its title. |