function ContentEntityBaseUnitTest::testGet

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testGet()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testGet()
  3. 11.x 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 553

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) : void {
    // Mock ContentEntityBase.
    $mock_base = $this->getMockBuilder(ContentEntityBaseMockableClass::class)
        ->disableOriginalConstructor()
        ->onlyMethods([
        'getTranslatedField',
    ])
        ->getMock();
    // 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->setValue($mock_base, $active_langcode);
    // Poke in fields.
    $ref_fields = new \ReflectionProperty($mock_base, 'fields');
    $ref_fields->setValue($mock_base, $fields);
    // Exercise get().
    $this->assertEquals($expected, $mock_base->get($field_name));
}

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