You are here

class EncryptedFieldValueManagerTest in Field Encryption 8.2

Unit Tests for the EncryptedFieldValueManager service.

@group field_encrypt

@coversDefaultClass \Drupal\field_encrypt\EncryptedFieldValueManager

Hierarchy

Expanded class hierarchy of EncryptedFieldValueManagerTest

File

tests/src/Unit/EncryptedFieldValueManagerTest.php, line 22
Contains \Drupal\Tests\field_encrypt\Unit\EncryptedFieldValueManagerTest.

Namespace

Drupal\Tests\field_encrypt\Unit
View source
class EncryptedFieldValueManagerTest extends UnitTestCase {

  /**
   * A mock entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;

  /**
   * A mock entity.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface
   */
  protected $entity;

  /**
   * A mock EncryptedFieldValue entity.
   *
   * @var \Drupal\field_encrypt\Entity\EncryptedFieldValueInterface
   */
  protected $encryptedFieldValue;

  /**
   * A mock EntityStorage instance.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setup();

    // Set up a mock entity type manager service.
    $this->entityManager = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
      ->disableOriginalConstructor()
      ->getMock();

    // Set up a mock EntityStorage.
    $this->storage = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
      ->disableOriginalConstructor()
      ->getMock();

    // Set up expectations for the entity type manager.
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->will($this
      ->returnValue($this->storage));

    // Set up a mock entity.
    $this->entity = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\ContentEntityInterface')
      ->disableOriginalConstructor()
      ->getMock();

    // Set up language object.
    $language = $this
      ->getMockBuilder('\\Drupal\\Core\\Language\\Language')
      ->setMethods([
      'getId',
    ])
      ->disableOriginalConstructor()
      ->getMock();

    // Set up expectations for language.
    $language
      ->expects($this
      ->any())
      ->method('getId')
      ->will($this
      ->returnValue('en'));

    // Set up expectations for entity.
    $this->entity
      ->expects($this
      ->any())
      ->method('language')
      ->will($this
      ->returnValue($language));
    $this->entity
      ->expects($this
      ->any())
      ->method('getEntityTypeId')
      ->will($this
      ->returnValue('node'));
    $this->entity
      ->expects($this
      ->any())
      ->method('getRevisionId')
      ->will($this
      ->returnValue(2));

    // Set up EncryptedFieldValue.
    $this->encryptedFieldValue = $this
      ->getMockBuilder('\\Drupal\\field_encrypt\\Entity\\EncryptedFieldValue')
      ->setMethods([
      'getEncryptedValue',
      'hasTranslation',
      'getTranslation',
      'setEncryptedValue',
      'save',
    ])
      ->disableOriginalConstructor()
      ->getMock();

    // Set up expectations for EncryptedFieldValue.
    $this->encryptedFieldValue
      ->expects($this
      ->any())
      ->method('hasTranslation')
      ->will($this
      ->returnValue(TRUE));
    $this->encryptedFieldValue
      ->expects($this
      ->any())
      ->method('getTranslation')
      ->will($this
      ->returnSelf());
  }

  /**
   * Test the saveEncryptedFieldValue method.
   *
   * @covers ::__construct
   * @covers ::saveEncryptedFieldValues
   *
   * @dataProvider saveEncryptedFieldValueDataProvider
   */
  public function testSaveEncryptedFieldValue($existing) {

    // Set up a mock for the EncryptedFieldValueManager class to mock
    // some methods.

    /** @var \Drupal\field_encrypt\EncryptedFieldValueManager $service */
    $service = $this
      ->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
      ->setMethods([
      'getExistingEntity',
      'getEntityRevisionId',
    ])
      ->setConstructorArgs(array(
      $this->entityManager,
    ))
      ->getMock();

    // Set up expectations depending on whether an existing entity exists.
    if ($existing) {
      $this->encryptedFieldValue
        ->expects($this
        ->once())
        ->method('setEncryptedValue');
      $this->encryptedFieldValue
        ->expects($this
        ->once())
        ->method('save');
      $service
        ->expects($this
        ->once())
        ->method('getExistingEntity')
        ->will($this
        ->returnValue($this->encryptedFieldValue));
    }
    else {
      $service
        ->expects($this
        ->once())
        ->method('getExistingEntity')
        ->will($this
        ->returnValue(FALSE));
      $this->encryptedFieldValue
        ->expects($this
        ->never())
        ->method('setEncryptedValue');
    }
    $service
      ->createEncryptedFieldValue($this->entity, 'field_test', 0, 'value', 'encrypted text');
    $service
      ->saveEncryptedFieldValues($this->entity, 'field_test', 0, 'value', 'encrypted text');
  }

  /**
   * Data provider for testSaveEncryptedFieldValue method.
   *
   * @return array
   *   An array with data for the test method.
   */
  public function saveEncryptedFieldValueDataProvider() {
    return [
      'existing' => [
        TRUE,
      ],
      'not_existing' => [
        FALSE,
      ],
    ];
  }

  /**
   * Test the getEncryptedFieldValue method.
   *
   * @covers ::__construct
   * @covers ::getEncryptedFieldValue
   *
   * @dataProvider getEncryptedFieldValueDataProvider
   */
  public function testGetEncryptedFieldValue($existing, $expected_value) {

    // Set up a mock for the EncryptedFieldValueManager class to mock some
    // methods.
    $service = $this
      ->getMockBuilder('\\Drupal\\field_encrypt\\EncryptedFieldValueManager')
      ->setMethods([
      'getExistingEntity',
      'getEncryptedValue',
    ])
      ->setConstructorArgs(array(
      $this->entityManager,
    ))
      ->getMock();

    // Set up expectations depending on whether an existing entity exists.
    if ($existing) {
      $this->encryptedFieldValue
        ->expects($this
        ->once())
        ->method('getEncryptedValue')
        ->will($this
        ->returnValue("encrypted text"));
      $service
        ->expects($this
        ->once())
        ->method('getExistingEntity')
        ->will($this
        ->returnValue($this->encryptedFieldValue));
    }
    else {
      $service
        ->expects($this
        ->once())
        ->method('getExistingEntity')
        ->will($this
        ->returnValue(FALSE));
      $service
        ->expects($this
        ->never())
        ->method('getEncryptedValue');
    }
    $value = $service
      ->getEncryptedFieldValue($this->entity, 'field_test', 0, 'value');
    $this
      ->assertEquals($expected_value, $value);
  }

  /**
   * Data provider for testGetEncryptedFieldValue method.
   *
   * @return array
   *   An array with data for the test method.
   */
  public function getEncryptedFieldValueDataProvider() {
    return [
      'existing' => [
        TRUE,
        "encrypted text",
      ],
      'not_existing' => [
        FALSE,
        FALSE,
      ],
    ];
  }

  /**
   * Test the deleteEntityEncryptedFieldValues method.
   *
   * @covers ::__construct
   * @covers ::deleteEntityEncryptedFieldValues
   */
  public function testDeleteEntityEncryptedFieldValues() {

    // Set up expectations for storage.
    $this->storage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->will($this
      ->returnValue([
      $this->encryptedFieldValue,
    ]));
    $this->storage
      ->expects($this
      ->once())
      ->method('delete');
    $service = new EncryptedFieldValueManager($this->entityManager);
    $service
      ->deleteEntityEncryptedFieldValues($this->entity);
  }

  /**
   * Test the deleteEntityEncryptedFieldValuesForField method.
   *
   * @covers ::__construct
   * @covers ::deleteEntityEncryptedFieldValuesForField
   * @covers ::getEntityRevisionId
   */
  public function testDeleteEntityEncryptedFieldValuesForField() {

    // Set up expectations for storage.
    $this->storage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->will($this
      ->returnValue([
      $this->encryptedFieldValue,
    ]));
    $this->storage
      ->expects($this
      ->once())
      ->method('delete');

    // Set up entity type object.
    $entity_type = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
      ->disableOriginalConstructor()
      ->getMock();

    // Set up expectations for entity type object.
    $entity_type
      ->expects($this
      ->once())
      ->method('hasKey')
      ->will($this
      ->returnValue(TRUE));
    $this->entity
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->will($this
      ->returnValue($entity_type));
    $this->entity
      ->expects($this
      ->once())
      ->method('getRevisionId')
      ->will($this
      ->returnValue(1));
    $this->entity
      ->expects($this
      ->never())
      ->method('id')
      ->will($this
      ->returnValue(1));
    $service = new EncryptedFieldValueManager($this->entityManager);
    $service
      ->deleteEntityEncryptedFieldValuesForField($this->entity, 'field_test');
  }

  /**
   * Test the deleteEncryptedFieldValuesForField method.
   *
   * @covers ::__construct
   * @covers ::deleteEncryptedFieldValuesForField
   */
  public function testDeleteEncryptedFieldValuesForField() {

    // Set up expectations for storage.
    $this->storage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->will($this
      ->returnValue([
      $this->encryptedFieldValue,
    ]));
    $this->storage
      ->expects($this
      ->once())
      ->method('delete');
    $service = new EncryptedFieldValueManager($this->entityManager);
    $service
      ->deleteEncryptedFieldValuesForField('node', 'field_test');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EncryptedFieldValueManagerTest::$encryptedFieldValue protected property A mock EncryptedFieldValue entity.
EncryptedFieldValueManagerTest::$entity protected property A mock entity.
EncryptedFieldValueManagerTest::$entityManager protected property A mock entity type manager service.
EncryptedFieldValueManagerTest::$storage protected property A mock EntityStorage instance.
EncryptedFieldValueManagerTest::getEncryptedFieldValueDataProvider public function Data provider for testGetEncryptedFieldValue method.
EncryptedFieldValueManagerTest::saveEncryptedFieldValueDataProvider public function Data provider for testSaveEncryptedFieldValue method.
EncryptedFieldValueManagerTest::setUp public function Overrides UnitTestCase::setUp
EncryptedFieldValueManagerTest::testDeleteEncryptedFieldValuesForField public function Test the deleteEncryptedFieldValuesForField method.
EncryptedFieldValueManagerTest::testDeleteEntityEncryptedFieldValues public function Test the deleteEntityEncryptedFieldValues method.
EncryptedFieldValueManagerTest::testDeleteEntityEncryptedFieldValuesForField public function Test the deleteEntityEncryptedFieldValuesForField method.
EncryptedFieldValueManagerTest::testGetEncryptedFieldValue public function Test the getEncryptedFieldValue method.
EncryptedFieldValueManagerTest::testSaveEncryptedFieldValue public function Test the saveEncryptedFieldValue method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.