You are here

public function CreditCardTest::testSecuritySettings in Ubercart 8.4

Tests security settings configuration.

File

payment/uc_credit/tests/src/Functional/CreditCardTest.php, line 90

Class

CreditCardTest
Tests credit card payments with the test gateway.

Namespace

Drupal\Tests\uc_credit\Functional

Code

public function testSecuritySettings() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // @todo Still need tests with existing key file
  // where key file is not readable or doesn't contain a valid key.
  // Create key directory, make it readable and writeable.
  \Drupal::service('file_system')
    ->mkdir('sites/default/files/testkey', 0755);

  // Try to submit settings form without a key file path.
  // Save current variable, reset to its value when first installed.
  $config = \Drupal::configFactory()
    ->getEditable('uc_credit.settings');
  $temp_variable = $config
    ->get('encryption_path');
  $config
    ->set('encryption_path', '')
    ->save();
  $this
    ->drupalGet('admin/store');
  $assert
    ->pageTextContains('You must review your credit card security settings and enable encryption before you can accept credit card payments.');
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([], 'Save configuration');

  // Check that key file has not yet been configured.
  $assert
    ->fieldValueEquals('uc_credit_encryption_path', 'Not configured.');

  // Restore variable setting.
  $config
    ->set('encryption_path', $temp_variable)
    ->save();

  // Try to submit settings form with an empty key file path.
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([
    'uc_credit_encryption_path' => '',
  ], 'Save configuration');
  $assert
    ->pageTextContains('Key path must be specified in security settings tab.');

  // Specify non-existent directory.
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([
    'uc_credit_encryption_path' => 'sites/default/ljkh/asdfasfaaaaa',
  ], 'Save configuration');
  $assert
    ->pageTextContains('You have specified a non-existent directory.');

  // Next, specify existing directory that's write protected.
  // Use /dev, as that should never be accessible.
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([
    'uc_credit_encryption_path' => '/dev',
  ], 'Save configuration');
  $assert
    ->pageTextContains('Cannot write to directory, please verify the directory permissions.');

  // Next, specify writeable directory, but with trailing '/' and
  // excess whitespace.
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([
    'uc_credit_encryption_path' => '  sites/default/files/testkey/ ',
  ], 'Save configuration');

  // See that the directory has been properly re-written to remove
  // trailing '/' and whitespace.
  $assert
    ->fieldValueEquals('uc_credit_encryption_path', 'sites/default/files/testkey');
  $assert
    ->pageTextContains('Credit card encryption key file generated.');

  // Check that warning about needing key file goes away.
  $assert
    ->pageTextNotContains('Credit card security settings must be configured in the security settings tab.');

  // Remove key file.
  \Drupal::service('file_system')
    ->unlink('sites/default/files/testkey/' . UC_CREDIT_KEYFILE_NAME);

  // Finally, specify good directory.
  $this
    ->drupalGet('admin/store/config/payment/credit');
  $this
    ->submitForm([
    'uc_credit_encryption_path' => 'sites/default/files/testkey',
  ], 'Save configuration');
  $assert
    ->pageTextContains('Credit card encryption key file generated.');

  // Test contents - must contain 32-character hexadecimal string.
  $this
    ->assertTrue(file_exists('sites/default/files/simpletest.keys/' . UC_CREDIT_KEYFILE_NAME), 'Key has been generated and stored.');
  $this
    ->assertTrue(preg_match("([0-9a-fA-F]{32})", uc_credit_encryption_key()), 'Valid key detected in key file.');

  // Cleanup keys directory after test.
  \Drupal::service('file_system')
    ->unlink('sites/default/files/testkey/' . UC_CREDIT_KEYFILE_NAME);
  \Drupal::service('file_system')
    ->rmdir('sites/default/files/testkey');
}