You are here

class EntityReferenceSelectionUnitTest in Drupal 8

Provides unit testing for selection handlers.

@coversDefaultClass \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase

@group entity_reference @group legacy

Hierarchy

Expanded class hierarchy of EntityReferenceSelectionUnitTest

File

core/tests/Drupal/Tests/Core/EntityReferenceSelection/EntityReferenceSelectionUnitTest.php, line 16

Namespace

Drupal\Tests\Core\EntityReferenceSelection
View source
class EntityReferenceSelectionUnitTest extends UnitTestCase {

  /**
   * Tests invalid default configuration.
   *
   * @covers ::defaultConfiguration
   * @covers ::resolveBackwardCompatibilityConfiguration
   */
  public function testInvalidDefaultConfiguration() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage("TestSelectionWithInvalidDefaultConfiguration::defaultConfiguration() should not contain a 'handler_settings' key. All settings should be placed in the root level.");
    new TestSelectionWithInvalidDefaultConfiguration([], 'test_selector', [
      'class' => 'TestSelectionWithInvalidDefaultConfiguration',
    ]);
  }

  /**
   * Tests the selection handler with malformed 'handler_settings' value.
   *
   * @covers ::setConfiguration
   * @covers ::resolveBackwardCompatibilityConfiguration
   */
  public function testMalformedHandlerSettingsValue() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage("The setting 'handler_settings' is reserved and cannot be used.");
    new TestSelection([
      'handler_settings' => FALSE,
    ], 'test_selector', [
      'class' => 'TestSelectionWithInvalidDefaultConfiguration',
    ]);
  }

  /**
   * Provides test data for ::testSetConfiguration()
   *
   * @return array
   *
   * @see \Drupal\Tests\Core\EntityReferenceSelection\testSetConfiguration
   */
  public function providerTestSetConfiguration() {
    return [
      [
        [
          'setting1' => 'foo',
          'setting2' => [
            'bar' => 'bar value',
            'baz' => 'baz value',
          ],
        ],
      ],
      [
        [
          'handler_settings' => [
            'setting1' => 'foo',
            'setting2' => [
              'bar' => 'bar value',
              'baz' => 'baz value',
            ],
          ],
        ],
      ],
      [
        [
          'setting1' => 'foo',
          'handler_settings' => [
            'setting2' => [
              'bar' => 'bar value',
              'baz' => 'baz value',
            ],
          ],
        ],
      ],
      [
        [
          'setting1' => 'foo',
          'setting2' => [
            'bar' => 'bar value',
            'baz' => 'baz value',
          ],
          'handler_settings' => [
            // Same setting from root level takes precedence.
            'setting2' => 'this will be overwritten',
          ],
        ],
      ],
    ];
  }

  /**
   * Tests selection handler plugin configuration set.
   *
   * @dataProvider providerTestSetConfiguration
   * @covers ::setConfiguration
   * @covers ::resolveBackwardCompatibilityConfiguration
   * @covers ::ensureBackwardCompatibilityConfiguration
   *
   * @param array $options
   *   The configuration passed to the plugin.
   */
  public function testSetConfiguration($options) {
    $selection = new TestSelection($options, 'test_selector', []);
    $expected = [
      'target_type' => NULL,
      'handler' => 'test_selector',
      'entity' => NULL,
      'setting1' => 'foo',
      'setting2' => [
        'qux' => 'qux value',
        'bar' => 'bar value',
        'baz' => 'baz value',
      ],
      'setting3' => 'foobar',
      'handler_settings' => [
        'setting1' => 'foo',
        'setting2' => [
          'qux' => 'qux value',
          'bar' => 'bar value',
          'baz' => 'baz value',
        ],
        'setting3' => 'foobar',
      ],
    ];
    $this
      ->assertArrayEquals($expected, $selection
      ->getConfiguration());
  }

  /**
   * Tests the selection handler plugin BC structure.
   *
   * @covers ::setConfiguration
   * @covers ::resolveBackwardCompatibilityConfiguration
   * @covers ::ensureBackwardCompatibilityConfiguration
   */
  public function testSetConfigurationBcLevel() {
    $config = [
      'target_type' => 'some_entity_type_id',
      'handler' => 'test_selector',
      'setting1' => 'foo',
    ];
    $selection = new TestSelection($config, 'test_selector', []);
    $expected = [
      'target_type' => 'some_entity_type_id',
      'handler' => 'test_selector',
      'entity' => NULL,
      'setting1' => 'foo',
      'setting2' => [
        'qux' => 'qux value',
      ],
      'setting3' => 'foobar',
      'handler_settings' => [
        'setting1' => 'foo',
        'setting2' => [
          'qux' => 'qux value',
        ],
        'setting3' => 'foobar',
      ],
    ];
    $this
      ->assertArrayEquals($expected, $selection
      ->getConfiguration());

    // Read the stored values and override a setting.
    $config = $selection
      ->getConfiguration();
    $config['setting1'] = 'bar';
    $selection
      ->setConfiguration($config);
    $expected['setting1'] = 'bar';
    $expected['handler_settings']['setting1'] = 'bar';
    $this
      ->assertArrayEquals($expected, $selection
      ->getConfiguration());
  }

  /**
   * Tests deprecation error triggering.
   *
   * @covers ::setConfiguration
   * @covers ::resolveBackwardCompatibilityConfiguration
   * @expectedDeprecation Providing settings under 'handler_settings' is deprecated in drupal:8.4.0 support for 'handler_settings' is removed from drupal:9.0.0. Move the settings in the root of the configuration array. See https://www.drupal.org/node/2870971
   */
  public function testDeprecationErrorTriggering() {

    // Configuration with BC level.
    $config = [
      'handler_settings' => [
        'setting1' => TRUE,
      ],
    ];
    new TestSelection($config, 'test_selector', []);

    // Ensure at least one assertion.
    $this
      ->assertTrue(TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceSelectionUnitTest::providerTestSetConfiguration public function Provides test data for ::testSetConfiguration()
EntityReferenceSelectionUnitTest::testDeprecationErrorTriggering public function Tests deprecation error triggering.
EntityReferenceSelectionUnitTest::testInvalidDefaultConfiguration public function Tests invalid default configuration.
EntityReferenceSelectionUnitTest::testMalformedHandlerSettingsValue public function Tests the selection handler with malformed 'handler_settings' value.
EntityReferenceSelectionUnitTest::testSetConfiguration public function Tests selection handler plugin configuration set.
EntityReferenceSelectionUnitTest::testSetConfigurationBcLevel public function Tests the selection handler plugin BC structure.
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.
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.
UnitTestCase::setUp protected function 340