class Select2Test in Select 2 8
Same name in this branch
- 8 tests/src/Unit/Element/Select2Test.php \Drupal\Tests\select2\Unit\Element\Select2Test
- 8 tests/src/Kernel/Element/Select2Test.php \Drupal\Tests\select2\Kernel\Element\Select2Test
@coversDefaultClass \Drupal\select2\Element\Select2 @group select2
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\select2\Unit\Element\Select2Test
Expanded class hierarchy of Select2Test
File
- tests/
src/ Unit/ Element/ Select2Test.php, line 15
Namespace
Drupal\Tests\select2\Unit\ElementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
Select2Test:: |
public | function | Data provider for testPlaceholderPropertyRendering(). | |
Select2Test:: |
public | function | Data provider for testPreRenderSelect(). | |
Select2Test:: |
protected | function |
Overrides UnitTestCase:: |
|
Select2Test:: |
public | function | Checks #placeholder property. | |
Select2Test:: |
public | function | @covers ::preRenderSelect | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |