You are here

public function ContentEntityBaseUnitTest::testGet in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testGet()
  2. 9 core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testGet()

@covers ::get @dataProvider providerGet

File

core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php, line 545

Class

ContentEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Entity\ContentEntityBase @group Entity @group Access

Namespace

Drupal\Tests\Core\Entity

Code

public function testGet($expected, $field_name, $active_langcode, $fields) {

  // Mock ContentEntityBase.
  $mock_base = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
    ->disableOriginalConstructor()
    ->onlyMethods([
    'getTranslatedField',
  ])
    ->getMockForAbstractClass();

  // Set up expectations for getTranslatedField() method. In get(),
  // getTranslatedField() is only called if the field name and language code
  // are not present as keys in the fields array.
  if (isset($fields[$field_name][$active_langcode])) {
    $mock_base
      ->expects($this
      ->never())
      ->method('getTranslatedField');
  }
  else {
    $mock_base
      ->expects($this
      ->once())
      ->method('getTranslatedField')
      ->with($this
      ->equalTo($field_name), $this
      ->equalTo($active_langcode))
      ->willReturn($expected);
  }

  // Poke in activeLangcode.
  $ref_langcode = new \ReflectionProperty($mock_base, 'activeLangcode');
  $ref_langcode
    ->setAccessible(TRUE);
  $ref_langcode
    ->setValue($mock_base, $active_langcode);

  // Poke in fields.
  $ref_fields = new \ReflectionProperty($mock_base, 'fields');
  $ref_fields
    ->setAccessible(TRUE);
  $ref_fields
    ->setValue($mock_base, $fields);

  // Exercise get().
  $this
    ->assertEquals($expected, $mock_base
    ->get($field_name));
}