You are here

class ContextAwarePluginTraitTest in Drupal 10

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php \Drupal\KernelTests\Core\Plugin\Context\ContextAwarePluginTraitTest

@coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginTrait

@group Plugin

Hierarchy

Expanded class hierarchy of ContextAwarePluginTraitTest

File

core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php, line 26

Namespace

Drupal\KernelTests\Core\Plugin\Context
View source
class ContextAwarePluginTraitTest extends KernelTestBase {

  /**
   * The plugin instance under test.
   *
   * @var \Drupal\Core\Plugin\ContextAwarePluginTrait
   */
  private $plugin;

  /**
   * The configurable plugin instance under test.
   *
   * @var \Drupal\Core\Plugin\ContextAwarePluginTrait
   */
  private $configurablePlugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $plugin_definition = new TestContextAwarePluginDefinition();
    $plugin_definition
      ->addContextDefinition('nato_letter', ContextDefinition::create('string'));
    $this->plugin = new TestContextAwarePlugin([], 'the_sisko', $plugin_definition);
    $this->configurablePlugin = new TestConfigurableContextAwarePlugin([], 'the_sisko', $plugin_definition);
  }

  /**
   * @covers ::getContextDefinitions
   */
  public function testGetContextDefinitions() {
    $this
      ->assertIsArray($this->plugin
      ->getContextDefinitions());
  }

  /**
   * @covers ::getContextDefinition
   */
  public function testGetContextDefinition() {

    // The context is not defined, so an exception will be thrown.
    $this
      ->expectException(ContextException::class);
    $this
      ->expectExceptionMessage('The person context is not a valid context.');
    $this->plugin
      ->getContextDefinition('person');
  }

  /**
   * @covers ::getContextValue
   */
  public function testGetContextValue() {
    $this->plugin
      ->setContextValue('nato_letter', 'Alpha');
    $this
      ->assertSame('Alpha', $this->plugin
      ->getContextValue('nato_letter'));
  }

  /**
   * @covers ::setContextValue
   */
  public function testSetContextValue() {
    $typed_data_manager = $this
      ->prophesize(TypedDataManagerInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('typed_data_manager', $typed_data_manager
      ->reveal());
    \Drupal::setContainer($container);
    $this->plugin
      ->getPluginDefinition()
      ->addContextDefinition('foo', new ContextDefinition('string'));
    $this
      ->assertFalse($this->plugin->setContextCalled);
    $this->plugin
      ->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
    $this
      ->assertTrue($this->plugin->setContextCalled);
  }

}

Members