You are here

class RadiosTest in Plugin 8.2

Same name in this branch
  1. 8.2 tests/src/Functional/Plugin/PluginSelector/RadiosTest.php \Drupal\Tests\plugin\Functional\Plugin\PluginSelector\RadiosTest
  2. 8.2 tests/src/Unit/Plugin/Plugin/PluginSelector/RadiosTest.php \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\RadiosTest

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

@group Plugin

Hierarchy

Expanded class hierarchy of RadiosTest

File

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

Namespace

Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector
View source
class RadiosTest extends PluginSelectorBaseTestBase {

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\Radios
   */
  protected $sut;

  /**
   * The response policy.
   *
   * @var \Drupal\Core\PageCache\ResponsePolicyInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $responsePolicy;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->responsePolicy = $this
      ->getMockBuilder(KillSwitch::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new Radios([], $this->pluginId, $this->pluginDefinition, $this->defaultPluginResolver, $this->stringTranslation, $this->responsePolicy);
    $this->sut
      ->setSelectablePluginType($this->selectablePluginType);
  }

  /**
   * @covers ::buildSelectorForm
   */
  public function testBuildSelectorFormWithoutAvailablePlugins() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $this->selectablePluginManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->willReturn([]);
    $build = $this->sut
      ->buildSelectorForm($form, $form_state);
    $this
      ->assertArrayHasKey('clear', $build);
  }

  /**
   * @covers ::buildSelector
   */
  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']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
PluginSelectorBaseTestBase::$defaultPluginResolver protected property The default plugin resolver.
PluginSelectorBaseTestBase::$pluginDefinition protected property The plugin definition of the class under test.
PluginSelectorBaseTestBase::$pluginId protected property The plugin ID of the class plugin under test.
PluginSelectorBaseTestBase::$selectablePluginManager protected property The plugin manager through which to select plugins.
PluginSelectorBaseTestBase::$selectablePluginType protected property The plugin type of which to select plugins.
PluginSelectorBaseTestBase::$selectedPlugin protected property The selected plugin.
RadiosTest::$responsePolicy protected property The response policy.
RadiosTest::$stringTranslation protected property The string translator.
RadiosTest::$sut protected property The class under test. Overrides PluginSelectorBaseTestBase::$sut
RadiosTest::setUp protected function Overrides PluginSelectorBaseTestBase::setUp
RadiosTest::testBuildSelector public function @covers ::buildSelector
RadiosTest::testBuildSelectorFormWithoutAvailablePlugins public function @covers ::buildSelectorForm
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.