You are here

class ReferenceWidgetTest in Select (or other) 8

Same name and namespace in other branches
  1. 8.3 Tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest
  2. 4.x tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest

Tests the form element implementation.

@group select_or_other

@covers Drupal\select_or_other\Plugin\Field\FieldWidget\ReferenceWidget

Hierarchy

Expanded class hierarchy of ReferenceWidgetTest

File

tests/src/Unit/ReferenceWidgetTest.php, line 18

Namespace

Drupal\Tests\select_or_other\Unit
View source
class ReferenceWidgetTest extends UnitTestBase {

  /**
   * {@inheritdoc}
   */
  protected function getTestedClassName() {
    return 'Drupal\\select_or_other\\Plugin\\Field\\FieldWidget\\ReferenceWidget';
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareFormElementMock($target_type = 'entity', $tested_class_name = FALSE) {
    $methods = [
      'getColumn',
      'getOptions',
      'getSelectedOptions',
      'getFieldSetting',
      'getAutoCreateBundle',
    ];

    // Get the mockBuilder.
    if ($tested_class_name) {
      $builder = $this
        ->getMockBuilder($tested_class_name);
    }
    else {
      $builder = $this->mockBuilder;
    }

    // Configure the mockBuilder.
    $field_definition = $this
      ->getMockForAbstractClass('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
    $field_definition
      ->expects($this
      ->any())
      ->method('getFieldStorageDefinition')
      ->willReturn($this
      ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'));
    $constructor_arguments = [
      '',
      '',
      $field_definition,
      [],
      [],
    ];
    $builder
      ->setConstructorArgs($constructor_arguments)
      ->setMethods($methods);
    if ($tested_class_name) {
      $class = new \ReflectionClass($tested_class_name);
      $mock = $class
        ->isAbstract() ? $builder
        ->getMockForAbstractClass() : $builder
        ->getMock();
    }
    else {
      $mock = $builder
        ->getMock();
    }

    // Configure the mock.
    $mock
      ->method('getColumn')
      ->willReturn('column');
    $mock
      ->method('getOptions')
      ->willReturn([]);
    $mock
      ->method('getSelectedOptions')
      ->willReturn([]);
    $mock
      ->method('getFieldSetting')
      ->willReturnOnConsecutiveCalls($target_type, 'some_handler', [], $target_type);
    $mock
      ->method('getAutoCreateBundle')
      ->willReturn('autoCreateBundle');
    return $mock;
  }

  /**
   * Test if defaultSettings() returns the correct keys.
   */
  public function testGetOptions() {
    $entity_id = 1;
    $entity_label = 'Label';
    $entity_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')
      ->disableOriginalConstructor()
      ->getMock();
    $entity_mock
      ->expects($this
      ->exactly(1))
      ->method('id')
      ->willReturn($entity_id);
    $entity_mock
      ->expects($this
      ->exactly(2))
      ->method('label')
      ->willReturn($entity_label);
    $entity_storage_mock = $this
      ->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_storage_mock
      ->expects($this
      ->exactly(2))
      ->method('loadByProperties')
      ->willReturnOnConsecutiveCalls([], [
      $entity_mock,
    ]);
    $mock = $this->mockBuilder
      ->disableOriginalConstructor()
      ->setMethods([
      'getEntityStorage',
      'getBundleKey',
      'getSelectionHandlerSetting',
    ])
      ->getMock();
    $mock
      ->expects($this
      ->exactly(2))
      ->method('getEntityStorage')
      ->willReturn($entity_storage_mock);
    $mock
      ->expects($this
      ->exactly(2))
      ->method('getBundleKey')
      ->willReturn('bundle');
    $mock
      ->expects($this
      ->exactly(2))
      ->method('getSelectionHandlerSetting')
      ->willReturn('target_bundle');
    $get_options = new ReflectionMethod($mock, 'getOptions');
    $get_options
      ->setAccessible(TRUE);

    // First invocation returns an empty array because there are no entities.
    $options = $get_options
      ->invoke($mock, $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\FieldableEntityInterface'));
    $expected = [];
    $this
      ->assertArrayEquals($options, $expected);

    // Second invocation returns a key=>value array because there is one entity.
    $options = $get_options
      ->invoke($mock, $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\FieldableEntityInterface'));
    $expected = [
      "{$entity_label} ({$entity_id})" => $entity_label,
    ];
    $this
      ->assertArrayEquals($options, $expected);
  }

  /**
   * Test if formElement() adds the expected information.
   */
  public function testFormElement() {
    foreach ([
      'node',
      'taxonomy_term',
    ] as $target_type) {

      /** @var ReferenceWidget $mock */
      $mock = $this
        ->prepareFormElementMock($target_type);

      /** @var WidgetBase $parent */
      $parent = $this
        ->prepareFormElementMock($target_type, 'Drupal\\select_or_other\\Plugin\\Field\\FieldWidget\\WidgetBase');
      $entity = $this
        ->getMockForAbstractClass('Drupal\\Core\\Entity\\FieldableEntityInterface');
      $items = $this
        ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldItemListInterface');
      $items
        ->method('getEntity')
        ->willReturn($entity);

      /** @var FieldItemListInterface $items */
      $delta = 1;
      $element = [];
      $form = [];
      $form_state = new FormState();
      $parent_result = $parent
        ->formElement($items, $delta, $element, $form, $form_state);
      $result = $mock
        ->formElement($items, $delta, $element, $form, $form_state);
      $added = array_diff_key($result, $parent_result);
      $expected = [
        '#target_type' => $target_type,
        '#selection_handler' => 'some_handler',
        '#selection_settings' => [],
        '#autocreate' => [
          'bundle' => 'autoCreateBundle',
          'uid' => 1,
        ],
        '#validate_reference' => TRUE,
        '#tags' => $target_type === 'taxonomy_term',
        '#merged_values' => TRUE,
        '#element_validate' => [
          [
            get_class($mock),
            'validateReferenceWidget',
          ],
        ],
      ];
      $this
        ->assertArrayEquals($expected, $added);
    }
  }

  /**
   * Tests preparation for EntityAutocomplete::validateEntityAutocomplete.
   */
  public function testPrepareElementValuesForValidation() {
    $method = new ReflectionMethod($this
      ->getTestedClassName(), 'prepareElementValuesForValidation');
    $method
      ->setAccessible(TRUE);
    foreach ([
      FALSE,
      TRUE,
    ] as $tags) {
      $element = $original_element = [
        '#tags' => $tags,
        '#value' => [
          'Some value',
          'Another value',
        ],
      ];
      $method
        ->invokeArgs(NULL, [
        &$element,
      ]);
      if ($tags) {
        $this
          ->assertTrue(is_string($element['#value']));
      }
      else {
        $this
          ->assertArrayEquals($original_element, $element);
      }
    }
  }

  /**
   * Tests if the widget correctly determines if it is applicable.
   */
  public function testIsApplicable() {
    $entity_reference_selection = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $entity_reference_selection
      ->expects($this
      ->exactly(4))
      ->method('getInstance')
      ->willReturnOnConsecutiveCalls($this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionInterface'), $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionWithAutocreateInterface'), $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionInterface'), $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionWithAutocreateInterface'));
    $this
      ->registerServiceWithContainerMock('plugin.manager.entity_reference_selection', $entity_reference_selection);
    $definition = $this
      ->getMockBuilder('Drupal\\Core\\Field\\FieldDefinitionInterface')
      ->getMockForAbstractClass();
    $definition
      ->expects($this
      ->exactly(4))
      ->method('getSettings')
      ->willReturnOnConsecutiveCalls([], [], [
      'handler_settings' => [
        'auto_create' => TRUE,
      ],
    ], [
      'handler_settings' => [
        'auto_create' => TRUE,
      ],
    ]);

    /** @var \Drupal\Core\Field\FieldDefinitionInterface $definition */
    $this
      ->assertFalse(ReferenceWidget::isApplicable($definition));
    $this
      ->assertFalse(ReferenceWidget::isApplicable($definition));
    $this
      ->assertFalse(ReferenceWidget::isApplicable($definition));
    $this
      ->assertTrue(ReferenceWidget::isApplicable($definition));
  }

  /**
   * Tests if the selected options are propery prepared.
   */
  public function testPrepareSelectedOptions() {
    $entity_id = 1;
    $entity_label = 'Label';
    $entity_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')
      ->disableOriginalConstructor()
      ->getMock();
    $entity_mock
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($entity_id);
    $entity_mock
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn($entity_label);
    $entity_storage_mock = $this
      ->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_storage_mock
      ->expects($this
      ->exactly(2))
      ->method('loadMultiple')
      ->willReturnOnConsecutiveCalls([], [
      $entity_mock,
    ]);
    $mock = $this->mockBuilder
      ->disableOriginalConstructor()
      ->setMethods([
      'getEntityStorage',
    ])
      ->getMock();
    $mock
      ->expects($this
      ->exactly(2))
      ->method('getEntityStorage')
      ->willReturn($entity_storage_mock);
    $get_options = new ReflectionMethod($mock, 'prepareSelectedOptions');
    $get_options
      ->setAccessible(TRUE);

    // First invocation returns an empty array because there are no entities.
    $options = $get_options
      ->invokeArgs($mock, [
      [],
    ]);
    $expected = [];
    $this
      ->assertArrayEquals($options, $expected);

    // Second invocation returns a value array..
    $options = $get_options
      ->invokeArgs($mock, [
      [],
    ]);
    $expected = [
      "{$entity_label} ({$entity_id})",
    ];
    $this
      ->assertArrayEquals($options, $expected);
  }

}

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.
ReferenceWidgetTest::getTestedClassName protected function Overrides UnitTestBase::getTestedClassName
ReferenceWidgetTest::prepareFormElementMock protected function
ReferenceWidgetTest::testFormElement public function Test if formElement() adds the expected information.
ReferenceWidgetTest::testGetOptions public function Test if defaultSettings() returns the correct keys.
ReferenceWidgetTest::testIsApplicable public function Tests if the widget correctly determines if it is applicable.
ReferenceWidgetTest::testPrepareElementValuesForValidation public function Tests preparation for EntityAutocomplete::validateEntityAutocomplete.
ReferenceWidgetTest::testPrepareSelectedOptions public function Tests if the selected options are propery prepared.
UnitTestBase::$containerMock private property
UnitTestBase::$mockBuilder protected property
UnitTestBase::$services private property @var array $services
UnitTestBase::addMockServicesToContainer private function Adds mocked services to the container.
UnitTestBase::containerMockGetServiceCallback public function Callback for the get method on the mocked service container.
UnitTestBase::getBasicMocks protected function Creates and returns two basic mocks.
UnitTestBase::getMockForAbstractClassWithMethods protected function Creates a mock for an abstract class with some mapped methods.
UnitTestBase::getNewEntityTypeManagerMock private function
UnitTestBase::getNewUserMock private function Creates and returns a mocked user.
UnitTestBase::prepareContainer private function Prepares a mocked service container.
UnitTestBase::registerServiceWithContainerMock protected function Registers a (mocked) service with the mocked service container.
UnitTestBase::setUp protected function Overrides UnitTestCase::setUp
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.