You are here

public function RadiosTest::testBuildSelector in Plugin 8.2

@covers ::buildSelector

File

tests/src/Unit/Plugin/Plugin/PluginSelector/RadiosTest.php, line 75

Class

RadiosTest
@coversDefaultClass \Drupal\plugin\Plugin\Plugin\PluginSelector\Radios

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_definition = $this
    ->createMock(PluginLabelDefinitionInterface::class);
  $plugin_definition
    ->expects($this
    ->atLeastOnce())
    ->method('getLabel')
    ->willReturn($plugin_label);
  $plugin = $this
    ->createMock(PluginInspectionInterface::class);
  $plugin
    ->expects($this
    ->atLeastOnce())
    ->method('getPluginDefinition')
    ->willReturn($plugin_definition);
  $plugin
    ->expects($this
    ->atLeastOnce())
    ->method('getPluginId')
    ->willReturn($plugin_id);
  $this->selectablePluginType
    ->expects($this
    ->atLeastOnce())
    ->method('ensureTypedPluginDefinition')
    ->willReturnArgument(0);
  $this->sut
    ->setSelectedPlugin($plugin);
  $selector_title = $this
    ->randomMachineName();
  $this->sut
    ->setLabel($selector_title);
  $selector_description = $this
    ->randomMachineName();
  $this->sut
    ->setDescription($selector_description);
  $element = array(
    '#parents' => array(
      'foo',
      'bar',
    ),
    '#title' => $selector_title,
  );
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $available_plugins = array(
    $plugin,
  );
  $expected_build_plugin_id = array(
    '#ajax' => array(
      'callback' => array(
        Radios::class,
        'ajaxRebuildForm',
      ),
      'effect' => 'fade',
      'event' => 'change',
      'progress' => 'none',
      'trigger_as' => array(
        'name' => 'foo__bar__select__container__change',
      ),
    ),
    '#attached' => [
      'library' => [
        'plugin/plugin_selector.plugin_radios',
      ],
    ],
    '#default_value' => $plugin_id,
    '#empty_value' => 'select',
    '#options' => array(
      $plugin_id => $plugin_label,
    ),
    '#required' => FALSE,
    '#title' => $selector_title,
    '#description' => $selector_description,
    '#type' => 'radios',
  );
  $expected_build_change = array(
    '#ajax' => array(
      'callback' => array(
        AdvancedPluginSelectorBase::class,
        'ajaxRebuildForm',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'js-hide',
      ),
    ),
    '#limit_validation_errors' => array(
      array(
        'foo',
        'bar',
        'select',
        'plugin_id',
      ),
    ),
    '#name' => 'foo__bar__select__container__change',
    '#submit' => [
      [
        AdvancedPluginSelectorBase::class,
        'rebuildForm',
      ],
    ],
    '#type' => 'submit',
    '#value' => 'Choose',
  );
  $build = $method
    ->invokeArgs($this->sut, array(
    $element,
    $form_state,
    $available_plugins,
  ));
  $this
    ->assertEquals($expected_build_plugin_id, $build['container']['plugin_id']);
  $this
    ->assertEquals($expected_build_change, $build['container']['change']);
  $this
    ->assertSame('container', $build['container']['#type']);
}