You are here

public function PerformanceScriptTest::testScript in Field Encryption 3.0.x

Runs the performance test script to check it works.

File

tests/src/Functional/PerformanceScriptTest.php, line 55

Class

PerformanceScriptTest
Tests the performance script provided by the module.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testScript() {
  $this
    ->assertCount(0, \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple());

  // Create 15 nodes in the script.
  putenv('FIELD_ENCRYPT_QUANTITY=15');
  ob_start();
  include __DIR__ . '/../../scripts/performance_test.php';
  $output = ob_get_clean();
  $this
    ->assertStringContainsString('Created 15 encrypted nodes in', $output);
  $this
    ->assertStringContainsString('Decrypted 15 encrypted nodes in', $output);
  $this
    ->assertCount(15, \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple());

  // Ensure the fields are actually encrypted.
  for ($nid = 1; $nid < 16; $nid++) {
    $result = \Drupal::database()
      ->query("SELECT field_test_single_value FROM {node__field_test_single} WHERE entity_id = :entity_id", [
      ':entity_id' => $nid,
    ])
      ->fetchField();
    $this
      ->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $result);
    $result = \Drupal::database()
      ->query("SELECT field_test_multi_value FROM {node__field_test_multi} WHERE entity_id = :entity_id", [
      ':entity_id' => $nid,
    ])
      ->fetchAll();
    foreach ($result as $record) {
      $this
        ->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $record->field_test_multi_value);
    }
  }
}