function FieldItemListTest::testEquals

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php \Drupal\Tests\Core\Field\FieldItemListTest::testEquals()
  2. 10 core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php \Drupal\Tests\Core\Field\FieldItemListTest::testEquals()
  3. 11.x core/tests/Drupal/Tests/Core/Field/FieldItemListTest.php \Drupal\Tests\Core\Field\FieldItemListTest::testEquals()

@covers ::equals

@dataProvider providerTestEquals

File

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

Class

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

Namespace

Drupal\Tests\Core\Field

Code

public function testEquals($expected, FieldItemInterface $first_field_item = NULL, FieldItemInterface $second_field_item = NULL) {
    // 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 three properties, one of them being computed.
    $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);
    $property_definitions['2'] = $this->createMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
    $property_definitions['2']->expects($this->any())
        ->method('isComputed')
        ->willReturn(TRUE);
    $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);
    // Set the field item values.
    if ($first_field_item instanceof FieldItemInterface) {
        $field_list_a->setValue($first_field_item);
    }
    if ($second_field_item instanceof FieldItemInterface) {
        $field_list_b->setValue($second_field_item);
    }
    $this->assertEquals($expected, $field_list_a->equals($field_list_b));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.