You are here

public function AdvancedPluginSelectorBaseTest::testBuildSelectorFormWithMultipleAvailablePlugins in Plugin 8.2

@covers ::buildSelectorForm @covers ::setPluginSelector

File

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

Class

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

Namespace

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

Code

public function testBuildSelectorFormWithMultipleAvailablePlugins() {
  $form = [];
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $cache_contexts = [
    $this
      ->randomMachineName(),
  ];
  $cache_tags = [
    $this
      ->randomMachineName(),
  ];
  $plugin_id_a = $this
    ->randomMachineName();
  $plugin_a = $this
    ->createMock(PluginInspectionInterface::class);
  $plugin_id_b = $this
    ->randomMachineName();
  $plugin_b = $this
    ->createMock(AdvancedPluginSelectorBaseUnitTestPluginFormPluginInterface::class);
  $this
    ->configureMockCacheableDependency($plugin_b, $cache_contexts, mt_rand(), $cache_tags);
  $plugin_definitions = [
    $plugin_id_a => [
      'id' => $plugin_id_a,
    ],
    $plugin_id_b => [
      'id' => $plugin_id_b,
    ],
  ];
  $map = [
    [
      $plugin_id_a,
      [],
      $plugin_a,
    ],
    [
      $plugin_id_b,
      [],
      $plugin_b,
    ],
  ];
  $this->selectablePluginManager
    ->expects($this
    ->atLeastOnce())
    ->method('createInstance')
    ->willReturnMap($map);
  $this->selectablePluginManager
    ->expects($this
    ->any())
    ->method('getDefinitions')
    ->willReturn($plugin_definitions);
  $build = $this->sut
    ->buildSelectorForm($form, $form_state);
  unset($build['container']['#plugin_selector_form_state_key']);
  $expected_build = [
    '#cache' => [
      'contexts' => $cache_contexts,
      'tags' => $cache_tags,
      'max-age' => 0,
    ],
    'container' => [
      '#attributes' => [
        'class' => [
          'plugin-selector-' . Html::getId($this->pluginId),
        ],
      ],
      '#available_plugins' => [
        $plugin_a,
        $plugin_b,
      ],
      '#process' => [
        [
          AdvancedPluginSelectorBase::class,
          'processBuildSelectorForm',
        ],
      ],
      '#tree' => TRUE,
      '#type' => 'container',
    ],
  ];
  $this
    ->assertSame($expected_build, $build);
}