You are here

public function AdvancedPluginSelectorBaseTest::testBuildSelector in Plugin 8.2

@covers ::buildSelector

File

tests/src/Unit/Plugin/Plugin/PluginSelector/AdvancedPluginSelectorBaseTest.php, line 618

Class

AdvancedPluginSelectorBaseTest
@coversDefaultClass \Drupal\plugin\Plugin\Plugin\PluginSelector\AdvancedPluginSelectorBase

Namespace

Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector

Code

public function testBuildSelector() {
  $this->stringTranslation
    ->expects($this
    ->any())
    ->method('translate')
    ->willReturnArgument(0);
  $method = new \ReflectionMethod($this->sut, 'buildSelector');
  $method
    ->setAccessible(TRUE);
  $plugin_id = $this
    ->randomMachineName();
  $plugin_label = $this
    ->randomMachineName();
  $plugin = $this
    ->createMock(PluginInspectionInterface::class);
  $plugin
    ->expects($this
    ->any())
    ->method('getPluginId')
    ->willReturn($plugin_id);
  $this->sut
    ->setSelectedPlugin($plugin);
  $element = [
    '#parents' => [
      'foo',
      'bar',
    ],
  ];
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $available_plugins = [
    $plugin,
  ];
  $expected_build_change = [
    '#ajax' => [
      'callback' => [
        AdvancedPluginSelectorBase::class,
        'ajaxRebuildForm',
      ],
    ],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'foo',
        'bar',
        'select',
        'plugin_id',
      ],
    ],
    '#name' => 'foo__bar__select__container__change',
    '#submit' => [
      [
        AdvancedPluginSelectorBase::class,
        'rebuildForm',
      ],
    ],
    '#type' => 'submit',
    '#value' => 'Choose',
  ];
  $build = $method
    ->invokeArgs($this->sut, [
    $element,
    $form_state,
    $available_plugins,
  ]);
  $this
    ->assertArrayHasKey('plugin_id', $build['container']);
  $this
    ->assertEquals($expected_build_change, $build['container']['change']);
  $this
    ->assertSame('container', $build['container']['#type']);
}