function ContentEntityBaseUnitTest::testGet

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

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));
}

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