You are here

class Select2Test in Select 2 8

Same name in this branch
  1. 8 tests/src/Unit/Element/Select2Test.php \Drupal\Tests\select2\Unit\Element\Select2Test
  2. 8 tests/src/Kernel/Element/Select2Test.php \Drupal\Tests\select2\Kernel\Element\Select2Test

@coversDefaultClass \Drupal\select2\Element\Select2 @group select2

Hierarchy

Expanded class hierarchy of Select2Test

File

tests/src/Unit/Element/Select2Test.php, line 15

Namespace

Drupal\Tests\select2\Unit\Element
View source
class Select2Test extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $language = $this
      ->createMock('Drupal\\Core\\Language\\LanguageInterface');
    $language
      ->expects($this
      ->any())
      ->method('getDirection')
      ->will($this
      ->returnValue('rtl'));
    $language
      ->method('getId')
      ->will($this
      ->returnValue('en'));
    $language_manager = $this
      ->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
    $language_manager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->will($this
      ->returnValue($language));
    $theme = $this
      ->createMock('Drupal\\Core\\Theme\\ActiveTheme');
    $theme
      ->expects($this
      ->any())
      ->method('getName')
      ->will($this
      ->returnValue('seven'));
    $theme_manager = $this
      ->createMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
    $theme_manager
      ->expects($this
      ->any())
      ->method('getActiveTheme')
      ->will($this
      ->returnValue($theme));
    $library_discovery = $this
      ->createMock('Drupal\\Core\\Asset\\LibraryDiscoveryInterface');
    $library_discovery
      ->expects($this
      ->any())
      ->method('getLibraryByName')
      ->will($this
      ->returnValue(TRUE));
    $string_translation = $this
      ->createMock('Drupal\\Core\\StringTranslation\\TranslationManager');
    $container = new ContainerBuilder();
    $container
      ->set('language_manager', $language_manager);
    $container
      ->set('theme.manager', $theme_manager);
    $container
      ->set('library.discovery', $library_discovery);
    $container
      ->set('string_translation', $string_translation);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::preRenderSelect
   *
   * @dataProvider providerTestPreRenderSelect
   */
  public function testPreRenderSelect($multiple, $required, $settings, $expected) {
    $element = [
      '#name' => 'field_foo',
      '#options' => [],
      '#multiple' => $multiple,
      '#required' => $required,
      '#attributes' => [
        'data-drupal-selector' => 'field-foo',
      ],
      '#autocreate' => [],
      '#autocomplete' => FALSE,
      '#cardinality' => 0,
      '#empty_value' => '',
      '#select2' => $settings,
    ];
    $element = Select2::preRenderSelect($element);
    $element = Select2::preRenderAutocomplete($element);
    $element = Select2::preRenderOverwrites($element);
    $this
      ->assertEquals($expected, array_intersect_key($element['#attributes'], $expected));
  }

  /**
   * Data provider for testPreRenderSelect().
   */
  public function providerTestPreRenderSelect() {
    $data = [];
    $data[] = [
      TRUE,
      TRUE,
      [],
      [
        'multiple' => 'multiple',
        'name' => 'field_foo[]',
        'data-select2-config' => Json::encode([
          'multiple' => TRUE,
          'placeholder' => [
            'id' => '',
            'text' => '',
          ],
          'allowClear' => FALSE,
          'dir' => 'rtl',
          'language' => 'en',
          'tags' => FALSE,
          'theme' => 'seven',
          'maximumSelectionLength' => 0,
          'tokenSeparators' => [],
          'selectOnClose' => FALSE,
          'width' => '100%',
        ]),
      ],
    ];
    $data[] = [
      FALSE,
      TRUE,
      [],
      [
        'name' => 'field_foo',
        'data-select2-config' => Json::encode([
          'multiple' => FALSE,
          'placeholder' => [
            'id' => '',
            'text' => '',
          ],
          'allowClear' => FALSE,
          'dir' => 'rtl',
          'language' => 'en',
          'tags' => FALSE,
          'theme' => 'seven',
          'maximumSelectionLength' => 0,
          'tokenSeparators' => [],
          'selectOnClose' => FALSE,
          'width' => '100%',
        ]),
      ],
    ];
    $data[] = [
      TRUE,
      FALSE,
      [],
      [
        'multiple' => 'multiple',
        'name' => 'field_foo[]',
        'data-select2-config' => Json::encode([
          'multiple' => TRUE,
          'placeholder' => [
            'id' => '',
            'text' => '',
          ],
          'allowClear' => FALSE,
          'dir' => 'rtl',
          'language' => 'en',
          'tags' => FALSE,
          'theme' => 'seven',
          'maximumSelectionLength' => 0,
          'tokenSeparators' => [],
          'selectOnClose' => FALSE,
          'width' => '100%',
        ]),
      ],
    ];
    $data[] = [
      FALSE,
      FALSE,
      [],
      [
        'name' => 'field_foo',
        'data-select2-config' => Json::encode([
          'multiple' => FALSE,
          'placeholder' => [
            'id' => '',
            'text' => '',
          ],
          'allowClear' => TRUE,
          'dir' => 'rtl',
          'language' => 'en',
          'tags' => FALSE,
          'theme' => 'seven',
          'maximumSelectionLength' => 0,
          'tokenSeparators' => [],
          'selectOnClose' => FALSE,
          'width' => '100%',
        ]),
      ],
    ];

    // Test overwriting of the default setting.
    $data[] = [
      FALSE,
      FALSE,
      [
        'allowClear' => FALSE,
        'multiple' => TRUE,
      ],
      [
        'name' => 'field_foo',
        'data-select2-config' => Json::encode([
          'multiple' => TRUE,
          'placeholder' => [
            'id' => '',
            'text' => '',
          ],
          'allowClear' => FALSE,
          'dir' => 'rtl',
          'language' => 'en',
          'tags' => FALSE,
          'theme' => 'seven',
          'maximumSelectionLength' => 0,
          'tokenSeparators' => [],
          'selectOnClose' => FALSE,
          'width' => '100%',
        ]),
      ],
    ];
    return $data;
  }

  /**
   * Checks #placeholder property.
   *
   * @dataProvider providerTestPlaceholderPropertyRendering
   */
  public function testPlaceholderPropertyRendering($required, $empty_option, $empty_value, $placeholder, $expected) {
    $element = [
      '#name' => 'field_foo',
      '#options' => [],
      '#autocreate' => [],
      '#multiple' => FALSE,
      '#required' => $required,
      '#autocomplete' => FALSE,
      '#empty_value' => $empty_value,
      '#empty_option' => $empty_option,
      '#attributes' => [
        'data-drupal-selector' => 'field-foo',
      ],
      '#placeholder' => $placeholder,
      '#select2' => [],
    ];
    $element = Select2::preRenderSelect($element);
    $element = Select2::preRenderAutocomplete($element);
    $placeholder = $element['#attributes']['data-select2-config']['placeholder'];
    $this
      ->assertSame($expected['id'], $placeholder['id']);
    $this
      ->assertEquals($expected['text'], $placeholder['text']
      ->getUntranslatedString());
  }

  /**
   * Data provider for testPlaceholderPropertyRendering().
   */
  public function providerTestPlaceholderPropertyRendering() {
    $data = [];
    $data[] = [
      TRUE,
      '',
      '',
      '',
      [
        'id' => '',
        'text' => '- Select -',
      ],
    ];
    $data[] = [
      FALSE,
      '',
      '',
      '',
      [
        'id' => '',
        'text' => '- None -',
      ],
    ];
    $data[] = [
      FALSE,
      NULL,
      NULL,
      NULL,
      [
        'id' => '',
        'text' => '- None -',
      ],
    ];
    $data[] = [
      FALSE,
      new TranslatableMarkup('empty_option'),
      NULL,
      NULL,
      [
        'id' => '',
        'text' => 'empty_option',
      ],
    ];
    $data[] = [
      FALSE,
      new TranslatableMarkup('empty_option'),
      NULL,
      new TranslatableMarkup('placeholder'),
      [
        'id' => '',
        'text' => 'placeholder',
      ],
    ];
    $data[] = [
      FALSE,
      NULL,
      NULL,
      new TranslatableMarkup('placeholder'),
      [
        'id' => '',
        'text' => 'placeholder',
      ],
    ];
    $data[] = [
      FALSE,
      NULL,
      'foo',
      new TranslatableMarkup('placeholder'),
      [
        'id' => 'foo',
        'text' => 'placeholder',
      ],
    ];
    $data[] = [
      TRUE,
      NULL,
      'foo',
      new TranslatableMarkup('placeholder'),
      [
        'id' => 'foo',
        'text' => 'placeholder',
      ],
    ];
    return $data;
  }

}

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.
Select2Test::providerTestPlaceholderPropertyRendering public function Data provider for testPlaceholderPropertyRendering().
Select2Test::providerTestPreRenderSelect public function Data provider for testPreRenderSelect().
Select2Test::setUp protected function Overrides UnitTestCase::setUp
Select2Test::testPlaceholderPropertyRendering public function Checks #placeholder property.
Select2Test::testPreRenderSelect public function @covers ::preRenderSelect
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.