View source
<?php
namespace Drupal\Tests\field_encrypt\Functional;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\field_encrypt\ProcessEntities;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\encrypt\Functional\EncryptTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
use Drupal\Tests\Traits\Core\CronRunTrait;
class BaseFieldTest extends EncryptTestBase {
use EntityDefinitionTestTrait;
use CronRunTrait;
use NodeCreationTrait;
public static $modules = [
'node',
'field',
'field_ui',
'text',
'locale',
'content_translation',
'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
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$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('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();
}
public function testEncryptFieldNormal() {
$this
->setFieldStorageSettings(TRUE);
$test_node = $this
->createNode([
'title' => 'Test title',
]);
$fields = $test_node
->getFields();
$definition = $fields['title']
->getFieldDefinition();
$this
->assertTrue($definition instanceof FieldDefinitionInterface);
$storage = $definition
->getFieldStorageDefinition();
$this
->assertEquals(TRUE, $storage
->getSetting('field_encrypt.encrypt'));
$this
->assertEquals([
'value',
], array_filter($storage
->getSetting('field_encrypt.properties')));
$this
->drupalGet('node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains("Test title");
$result = \Drupal::database()
->query("SELECT title FROM {node_field_data} WHERE nid = :entity_id", [
':entity_id' => $test_node
->id(),
])
->fetchField();
$this
->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $result);
$this
->config('field_encrypt.settings')
->set('encryption_profile', 'encryption_profile_2')
->save();
$this
->resetAll();
$this
->drupalGet('node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains("Test title");
$this
->drupalGet('node/add/page');
$this
->assertSession()
->fieldExists('title[0][value]')
->setValue('Test title encrypted');
$this
->assertSession()
->buttonExists('Save')
->press();
$this
->assertSession()
->pageTextContains('Test title encrypted has been created.');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_ENTITY_TYPE_insert: Test title encrypted');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_entity_insert: Test title encrypted');
$this
->drupalGet('node/2/edit');
$this
->assertSession()
->fieldExists('title[0][value]')
->setValue('Test new title encrypted');
$this
->assertSession()
->buttonExists('Save')
->press();
$this
->assertSession()
->pageTextContains('Test new title encrypted has been updated.');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_ENTITY_TYPE_update: Test new title encrypted');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_entity_update: Test new title encrypted');
$this
->drupalGet('node/2/delete');
$this
->assertSession()
->buttonExists('Delete')
->press();
$this
->assertSession()
->pageTextContains('Test new title encrypted has been deleted.');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_ENTITY_TYPE_delete: Test new title encrypted');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_entity_delete: Test new title encrypted');
$this
->drupalGet('node/add/page');
$this
->assertSession()
->fieldExists('title[0][value]')
->setValue('Test title encrypted 3');
$this
->assertSession()
->buttonExists('Save')
->press();
$this
->assertSession()
->pageTextContains('Test title encrypted 3 has been created.');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_ENTITY_TYPE_insert: Test title encrypted 3');
$this
->assertSession()
->pageTextContains('Field encrypt test hook_entity_insert: Test title encrypted 3');
$this
->setFieldStorageSettings(FALSE);
$this
->assertSession()
->linkByHrefExists('admin/config/system/field-encrypt/process-queues');
$this
->drupalGet('admin/config/system/field-encrypt/process-queues');
$this
->assertSession()
->pageTextContains('There are 2 entities queued for updating to use the latest field encryption settings.');
$this
->assertTrue(\Drupal::database()
->schema()
->fieldExists('node_field_data', ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__value'));
$this
->drupalGet('node/3/delete');
$this
->assertSession()
->buttonExists('Delete')
->press();
$this
->assertSession()
->pageTextContains('Test title encrypted 3 has been deleted.');
$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.');
$this
->drupalGet('node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains("Test title");
$result = \Drupal::database()
->query("SELECT title FROM {node_field_data} WHERE nid = :entity_id", [
':entity_id' => $test_node
->id(),
])
->fetchField();
$this
->assertEquals('Test title', $result);
$this
->assertTrue(\Drupal::database()
->schema()
->fieldExists('node_field_data', ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__value'));
$this
->cronRun();
$this
->assertFalse(\Drupal::database()
->schema()
->fieldExists('node_field_data', ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__value'));
$this
->drupalGet('node/add/page');
$this
->assertSession()
->fieldExists('title[0][value]')
->setValue('Test title no encryption');
$this
->assertSession()
->buttonExists('Save')
->press();
$this
->assertSession()
->pageTextContains('Test title no encryption has been created.');
}
public function testEncryptFieldRevision() {
$this
->setFieldStorageSettings(TRUE);
$test_node = $this
->createNode([
'title' => 'Test title rev 1',
]);
$old_revision_id = $test_node
->getRevisionId();
$test_node
->setNewRevision(TRUE);
$test_node->title->value = 'Test title rev 2';
$test_node
->save();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache([
$test_node
->id(),
]);
$this
->assertNotSame($test_node
->getRevisionId(), $old_revision_id, 'A new revision has been created.');
$this
->drupalGet('node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains('Test title rev 2');
$this
->drupalGet('node/' . $test_node
->id() . '/revisions/' . $old_revision_id . '/view');
$this
->assertSession()
->pageTextContains("Test title rev 1");
$result = \Drupal::database()
->query("SELECT title FROM {node_field_revision} WHERE nid = :entity_id", [
':entity_id' => $test_node
->id(),
])
->fetchField();
$this
->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $result);
}
public function testEncryptFieldTranslation() {
ConfigurableLanguage::createFromLangcode('fr')
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'page', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
$this
->rebuildContainer();
$this
->setFieldStorageSettings(TRUE);
$test_node = $this
->createNode([
'title' => 'Test title en',
]);
$controller = \Drupal::entityTypeManager()
->getStorage($test_node
->getEntityTypeId());
$controller
->resetCache([
$test_node
->id(),
]);
$test_node = $controller
->load($test_node
->id());
$test_node
->addTranslation('fr', [
'title' => "test title fr",
]);
$test_node
->save();
$this
->drupalGet('node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains("Test title en");
$this
->drupalGet('fr/node/' . $test_node
->id());
$this
->assertSession()
->pageTextContains("Test title fr");
$result = \Drupal::database()
->query("SELECT title FROM {node_field_data} WHERE nid = :entity_id", [
':entity_id' => $test_node
->id(),
])
->fetchAll();
$this
->assertCount(2, $result);
foreach ($result as $record) {
$this
->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $record->title);
}
}
public function testEntityTypeDependencies() {
$this
->setFieldStorageSettings(TRUE);
$config_entity = \Drupal::entityTypeManager()
->getStorage('field_encrypt_entity_type')
->load('node');
$this
->assertNotNull($config_entity);
$old_config = $config_entity
->getBaseFields();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
\Drupal::service('module_installer')
->uninstall([
'node',
]);
$config_entity = \Drupal::entityTypeManager()
->getStorage('field_encrypt_entity_type')
->load('node');
$this
->assertNull($config_entity);
\Drupal::service('module_installer')
->uninstall([
'field_encrypt',
'field_encrypt_test',
]);
$this
->configImporter()
->import();
$config_entity = \Drupal::entityTypeManager()
->getStorage('field_encrypt_entity_type')
->load('node');
$this
->assertSame($old_config, $config_entity
->getBaseFields());
$this
->assertFalse($this
->configImporter()
->reset()
->hasUnprocessedConfigurationChanges());
$this
->createNode([
'title' => 'Test title en',
]);
$this
->assertSame([], \Drupal::entityDefinitionUpdateManager()
->getChangeList());
$this
->assertFalse(\Drupal::entityDefinitionUpdateManager()
->needsUpdates());
\Drupal::state()
->set('field_encrypt.create_base_field', TRUE);
$this
->assertTrue(\Drupal::entityDefinitionUpdateManager()
->needsUpdates());
$this
->applyEntityUpdates();
$config_entity = \Drupal::entityTypeManager()
->getStorage('field_encrypt_entity_type')
->load('node');
$config_entity
->setBaseFields([
'field_encrypt_test_base_field' => [
'value',
],
])
->save();
$queue = \Drupal::service('queue')
->get('field_encrypt_update_entity_encryption');
$this
->assertSame(1, $queue
->numberOfItems());
$this
->cronRun();
\Drupal::state()
->set('field_encrypt.create_base_field', FALSE);
$this
->applyEntityUpdates();
$config_entity = \Drupal::entityTypeManager()
->getStorage('field_encrypt_entity_type')
->load('node');
$this
->assertNull($config_entity);
$queue = \Drupal::service('queue')
->get('field_encrypt_update_entity_encryption');
$this
->assertSame(1, $queue
->numberOfItems());
}
}