You are here

public function FieldItemListTest::testEqualsEmptyItems in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php \Drupal\Tests\Core\Field\FieldItemListTest::testEqualsEmptyItems()
  2. 10 core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php \Drupal\Tests\Core\Field\FieldItemListTest::testEqualsEmptyItems()

@covers ::equals

File

core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php, line 228

Class

FieldItemListTest
@coversDefaultClass \Drupal\Core\Field\FieldItemList @group Field

Namespace

Drupal\Tests\Core\Field

Code

public function testEqualsEmptyItems() {

  /** @var \Drupal\Core\Field\FieldItemBase  $fv */
  $first_field_item = $this
    ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldItemBase', [], '', FALSE);
  $first_field_item
    ->setValue([
    '0' => 1,
    '1' => 2,
  ]);
  $second_field_item = $this
    ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldItemBase', [], '', FALSE);
  $second_field_item
    ->setValue([
    '1' => 2,
    '0' => 1,
  ]);
  $empty_field_item = $this
    ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldItemBase', [], '', FALSE);

  // Mock the field type manager and place it in the container.
  $field_type_manager = $this
    ->createMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
  $container = new ContainerBuilder();
  $container
    ->set('plugin.manager.field.field_type', $field_type_manager);
  \Drupal::setContainer($container);

  // Set up the properties of the field item.
  $property_definitions['0'] = $this
    ->createMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
  $property_definitions['0']
    ->expects($this
    ->any())
    ->method('isComputed')
    ->willReturn(FALSE);
  $property_definitions['1'] = $this
    ->createMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
  $property_definitions['1']
    ->expects($this
    ->any())
    ->method('isComputed')
    ->willReturn(FALSE);
  $field_storage_definition = $this
    ->createMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface');
  $field_storage_definition
    ->expects($this
    ->any())
    ->method('getPropertyDefinitions')
    ->will($this
    ->returnValue($property_definitions));
  $field_definition = $this
    ->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
  $field_definition
    ->expects($this
    ->any())
    ->method('getFieldStorageDefinition')
    ->willReturn($field_storage_definition);
  $field_list_a = new FieldItemList($field_definition);
  $field_list_b = new FieldItemList($field_definition);

  // Set up the mocking necessary for creating field items.
  $field_type_manager
    ->expects($this
    ->any())
    ->method('createFieldItem')
    ->willReturnOnConsecutiveCalls($first_field_item, $second_field_item, $empty_field_item, $empty_field_item);

  // Set the field item values.
  $field_list_a
    ->setValue($first_field_item);
  $field_list_b
    ->setValue($second_field_item);
  $field_list_a
    ->appendItem($empty_field_item);

  // Field list A has an empty item.
  $this
    ->assertEquals(FALSE, $field_list_a
    ->equals($field_list_b));

  // Field lists A and B have empty items.
  $field_list_b
    ->appendItem($empty_field_item);
  $this
    ->assertEquals(TRUE, $field_list_a
    ->equals($field_list_b));

  // Field list B has an empty item.
  $field_list_a
    ->filterEmptyItems();
  $this
    ->assertEquals(FALSE, $field_list_a
    ->equals($field_list_b));

  // Neither field lists A and B have empty items.
  $field_list_b
    ->filterEmptyItems();
  $this
    ->assertEquals(TRUE, $field_list_a
    ->equals($field_list_b));
}