You are here

EncryptionTest.php in Ubercart 8.4

File

uc_store/tests/src/Functional/EncryptionTest.php
View source
<?php

namespace Drupal\Tests\uc_store\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests the encryption and decryption of strings.
 *
 * @group ubercart
 */
class EncryptionTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   *
   * We need access to uc_store.encryption service, so enable uc_store.
   */
  protected static $modules = [
    'uc_store',
  ];

  /**
   * Encryption object.
   *
   * @var \Drupal\uc_store\EncryptionInterface
   */
  protected $crypt;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->crypt = \Drupal::service('uc_store.encryption');
  }

  /**
   * Tests operation of uc_store.encryption service.
   */
  public function testEncryptionService() {

    // 16-byte random key.
    $key = $this
      ->randomMachineName(16);
    $this->crypt
      ->setCypher('ubbi-dubbi');
    $errors = $this->crypt
      ->getErrors();
    if (!empty($errors)) {
      $this
        ->assertTrue(TRUE, 'Tried to use invalid cypher.');
      $this
        ->assertEquals('ubbi-dubbi is not a valid cypher', $errors[0], 'Invalid cypher error message found.');
    }
    $this->crypt
      ->setCypher('aes-128-cbc');
    $errors = $this->crypt
      ->getErrors();
    if (empty($errors)) {
      $this
        ->assertTrue(TRUE, 'AES-128-CBC cypher found.');
    }
    $plaintext = 'The quick brown fox jumps over the lazy dog.';
    $cyphertext = $this->crypt
      ->encrypt($key, $plaintext);
    $errors = $this->crypt
      ->getErrors();
    $this
      ->assertEmpty($errors, 'Encryption successful.');
    $decrypted = $this->crypt
      ->decrypt($key, $cyphertext);
    $errors = $this->crypt
      ->getErrors();
    $this
      ->assertEmpty($errors, 'Decryption successful.');
    $this
      ->assertEquals($plaintext, $decrypted, 'Decrypted text is the same as initial plaintext.');
  }

}

Classes

Namesort descending Description
EncryptionTest Tests the encryption and decryption of strings.