You are here

class KeyEntityTest in Key 8

@coversDefaultClass \Drupal\key\Entity\Key @group key

Hierarchy

Expanded class hierarchy of KeyEntityTest

File

tests/src/Unit/Entity/KeyEntityTest.php, line 15

Namespace

Drupal\Tests\key\Unit\Entity
View source
class KeyEntityTest extends KeyTestBase {

  /**
   * Key type manager.
   *
   * @var \Drupal\key\Plugin\KeyPluginManager
   */
  protected $keyTypeManager;

  /**
   * Key provider manager.
   *
   * @var \Drupal\key\Plugin\KeyPluginManager
   */
  protected $keyProviderManager;

  /**
   * Key plugin manager.
   *
   * @var \Drupal\key\Plugin\KeyPluginManager
   */
  protected $keyInputManager;

  /**
   * Key type settings.
   *
   * @var array
   *   Key type settings to use for Authentication key type.
   */
  protected $key_type_settings;

  /**
   * Key provider settings.
   *
   * @var array
   *   Key provider settings to use for Configuration key provider.
   */
  protected $key_provider_settings;

  /**
   * Key input settings.
   *
   * @var array
   *   Key input settings to use for None key input.
   */
  protected $key_input_settings;

  /**
   * Assert that key entity getters work.
   */
  public function testGetters() {

    // Create a key entity using Configuration key provider.
    $values = [
      'key_id' => $this
        ->getRandomGenerator()
        ->word(15),
      'key_provider' => 'config',
      'key_provider_settings' => $this->key_provider_settings,
    ];
    $key = new Key($values, 'key');
    $this
      ->assertEquals($values['key_provider'], $key
      ->getKeyProvider()
      ->getPluginId());
    $this
      ->assertEquals($values['key_provider_settings'], $key
      ->getKeyProvider()
      ->getConfiguration());
    $this
      ->assertEquals($values['key_provider_settings']['key_value'], $key
      ->getKeyProvider()
      ->getConfiguration()['key_value']);
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $definition = [
      'id' => 'authentication',
      'label' => 'Authentication',
    ];
    $this->key_type_settings = [];
    $plugin = new AuthenticationKeyType($this->key_type_settings, 'authentication', $definition);

    // Mock the KeyTypeManager service.
    $this->keyTypeManager = $this
      ->getMockBuilder('\\Drupal\\key\\Plugin\\KeyPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->keyTypeManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->willReturn([
      [
        'id' => 'authentication',
        'label' => 'Authentication',
      ],
    ]);
    $this->keyTypeManager
      ->expects($this
      ->any())
      ->method('createInstance')
      ->with('authentication', $this->key_type_settings)
      ->willReturn($plugin);
    $this->container
      ->set('plugin.manager.key.key_type', $this->keyTypeManager);
    $definition = [
      'id' => 'config',
      'label' => 'Configuration',
      'storage_method' => 'config',
    ];
    $this->key_provider_settings = [
      'key_value' => $this
        ->createToken(),
      'base64_encoded' => FALSE,
    ];
    $plugin = new ConfigKeyProvider($this->key_provider_settings, 'config', $definition);

    // Mock the KeyProviderManager service.
    $this->keyProviderManager = $this
      ->getMockBuilder('\\Drupal\\key\\Plugin\\KeyPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->keyProviderManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->willReturn([
      [
        'id' => 'file',
        'label' => 'File',
        'storage_method' => 'file',
      ],
      [
        'id' => 'config',
        'label' => 'Configuration',
        'storage_method' => 'config',
      ],
    ]);
    $this->keyProviderManager
      ->expects($this
      ->any())
      ->method('createInstance')
      ->with('config', $this->key_provider_settings)
      ->willReturn($plugin);
    $this->container
      ->set('plugin.manager.key.key_provider', $this->keyProviderManager);
    $definition = [
      'id' => 'none',
      'label' => 'None',
    ];
    $this->key_input_settings = [];
    $plugin = new NoneKeyInput($this->key_input_settings, 'none', $definition);

    // Mock the KeyInputManager service.
    $this->keyInputManager = $this
      ->getMockBuilder('\\Drupal\\key\\Plugin\\KeyPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->keyInputManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->willReturn([
      [
        'id' => 'none',
        'label' => 'None',
      ],
    ]);
    $this->keyInputManager
      ->expects($this
      ->any())
      ->method('createInstance')
      ->with('none', $this->key_input_settings)
      ->willReturn($plugin);
    $this->container
      ->set('plugin.manager.key.key_input', $this->keyInputManager);
    \Drupal::setContainer($this->container);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyEntityTest::$keyInputManager protected property Key plugin manager.
KeyEntityTest::$keyProviderManager protected property Key provider manager.
KeyEntityTest::$keyTypeManager protected property Key type manager.
KeyEntityTest::$key_input_settings protected property Key input settings.
KeyEntityTest::$key_provider_settings protected property Key provider settings.
KeyEntityTest::$key_type_settings protected property Key type settings.
KeyEntityTest::setUp protected function Overrides KeyTestBase::setUp
KeyEntityTest::testGetters public function Assert that key entity getters work.
KeyTestBase::$config protected property Config.
KeyTestBase::$configStorage protected property Configuration storage.
KeyTestBase::$container protected property Container builder.
KeyTestBase::$entityTypeManager protected property Entity type manager instance.
KeyTestBase::createToken protected function Return a token that could be a key.
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.