You are here

class EntityDisplayTraitTest in CCK Select Other 8

Tests the entity display trait.

Note that prophecy cannot be used to mock because there is a conflict between the base mockForTrait method and prophecy. PHPUnitWTF.

@group cck_select_other

Hierarchy

Expanded class hierarchy of EntityDisplayTraitTest

File

tests/src/Unit/EntityDisplayTraitTest.php, line 16

Namespace

Drupal\Tests\cck_select_other\Unit
View source
class EntityDisplayTraitTest extends UnitTestCase {
  protected $mock;

  /**
   * The entity manager mock.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->mock = $this
      ->getMockForTrait('Drupal\\cck_select_other\\EntityDisplayTrait');

    // Mock the entity manager.
    $this->entityManager = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $this->entityManager);
    \Drupal::setContainer($container);
  }

  /**
   * Assert that select other widget is detected on form displays.
   *
   * @param string $plugin_id
   *   The plugin ID.
   * @param bool $expected
   *   The expected result.
   *
   * @dataProvider hasSelectOtherWidgetProvider
   */
  public function testHasSelectOtherWidget($plugin_id, $expected) {

    // Mock Field plugin settings.
    $fieldSettings = $this
      ->getMockBuilder('\\Drupal\\Core\\Field\\PluginSettingsInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $fieldSettings
      ->expects($this
      ->any())
      ->method('getPluginId')
      ->willReturn($plugin_id);

    // Mock Entity Form Display.
    $entityDisplay = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $entityDisplay
      ->expects($this
      ->any())
      ->method('getRenderer')
      ->with('field_list')
      ->willReturn($fieldSettings);

    // Mock the entity storage interface.
    $entityStorage = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $entityStorage
      ->expects($this
      ->any())
      ->method('loadByProperties')
      ->with([
      'targetEntityType' => 'entity_test',
    ])
      ->willReturn([
      'entity_test.display.mock' => $entityDisplay,
    ]);

    // Mock entity manager methods.
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('entity_form_display')
      ->willReturn($entityStorage);

    // Mock the definition.
    $definition = $this
      ->getMockBuilder('\\Drupal\\Core\\Field\\FieldDefinitionInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $definition
      ->expects($this
      ->any())
      ->method('getTargetEntityTypeId')
      ->willReturn('entity_test');
    $definition
      ->expects($this
      ->any())
      ->method('getName')
      ->willReturn('field_list');
    $this
      ->assertEquals($expected, $this->mock
      ->hasSelectOtherWidget($definition));
  }

  /**
   * Provide parameters for testHasSelectOtherWidget().
   *
   * @return array
   *   An array of test parameters.
   */
  public function hasSelectOtherWidgetProvider() {
    return [
      [
        'cck_select_other',
        TRUE,
      ],
      [
        'textfield',
        FALSE,
      ],
    ];
  }

  /**
   * Asserts that select other widget settings are returned or not.
   *
   * @param string $plugin_id
   *   The plugin ID.
   * @param array $expected
   *   The expected result.
   *
   * @dataProvider getWidgetSettingsProvider().
   */
  public function testGetWidgetSettings($plugin_id, array $expected) {

    // Mock Field plugin settings.
    $fieldSettings = $this
      ->getMockBuilder('\\Drupal\\Core\\Field\\PluginSettingsInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $fieldSettings
      ->expects($this
      ->any())
      ->method('getPluginId')
      ->willReturn($plugin_id);
    $fieldSettings
      ->expects($this
      ->any())
      ->method('getSettings')
      ->willReturn($expected);

    // Mock Entity Form Display.
    $entityDisplay = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $entityDisplay
      ->expects($this
      ->any())
      ->method('getRenderer')
      ->with('field_list')
      ->willReturn($fieldSettings);

    // Mock the entity storage interface.
    $entityStorage = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $entityStorage
      ->expects($this
      ->any())
      ->method('loadByProperties')
      ->with([
      'targetEntityType' => 'entity_test',
    ])
      ->willReturn([
      'entity_test.display.mock' => $entityDisplay,
    ]);

    // Mock entity manager methods.
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('entity_form_display')
      ->willReturn($entityStorage);

    // Mock the definition.
    $definition = $this
      ->getMockBuilder('\\Drupal\\Core\\Field\\FieldDefinitionInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $definition
      ->expects($this
      ->any())
      ->method('getTargetEntityTypeId')
      ->willReturn('entity_test');
    $definition
      ->expects($this
      ->any())
      ->method('getName')
      ->willReturn('field_list');
    $this
      ->assertEquals($expected, $this->mock
      ->getWidgetSettings($definition));
  }

  /**
   * Get test parameters for testGetWidgetSettings().
   *
   * @return array
   *   Test parameters.
   */
  public function getWidgetSettingsProvider() {
    return [
      [
        'cck_select_other',
        [
          'other_label' => 'Other',
        ],
      ],
      [
        'textfield',
        [],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDisplayTraitTest::$entityManager protected property The entity manager mock.
EntityDisplayTraitTest::$mock protected property
EntityDisplayTraitTest::getWidgetSettingsProvider public function Get test parameters for testGetWidgetSettings().
EntityDisplayTraitTest::hasSelectOtherWidgetProvider public function Provide parameters for testHasSelectOtherWidget().
EntityDisplayTraitTest::setUp protected function Overrides UnitTestCase::setUp
EntityDisplayTraitTest::testGetWidgetSettings public function Asserts that select other widget settings are returned or not.
EntityDisplayTraitTest::testHasSelectOtherWidget public function Assert that select other widget is detected on form displays.
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.