function NodeFieldOverridesTest::testFieldOverrides

Same name in other branches
  1. 8.9.x core/modules/node/tests/src/Kernel/NodeFieldOverridesTest.php \Drupal\Tests\node\Kernel\NodeFieldOverridesTest::testFieldOverrides()
  2. 10 core/modules/node/tests/src/Kernel/NodeFieldOverridesTest.php \Drupal\Tests\node\Kernel\NodeFieldOverridesTest::testFieldOverrides()
  3. 11.x core/modules/node/tests/src/Kernel/NodeFieldOverridesTest.php \Drupal\Tests\node\Kernel\NodeFieldOverridesTest::testFieldOverrides()

Tests that field overrides work as expected.

File

core/modules/node/tests/src/Kernel/NodeFieldOverridesTest.php, line 45

Class

NodeFieldOverridesTest
Tests node field overrides.

Namespace

Drupal\Tests\node\Kernel

Code

public function testFieldOverrides() {
    if (!NodeType::load('ponies')) {
        NodeType::create([
            'name' => 'Ponies',
            'type' => 'ponies',
        ])->save();
    }
    $override = BaseFieldOverride::loadByName('node', 'ponies', 'uid');
    if ($override) {
        $override->delete();
    }
    $uid_field = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node')['uid'];
    $config = $uid_field->getConfig('ponies');
    $config->save();
    $this->assertEquals('Drupal\\node\\Entity\\Node::getDefaultEntityOwner', $config->get('default_value_callback'));
    
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
        'type' => 'ponies',
    ]);
    $owner = $node->getOwner();
    $this->assertInstanceOf(UserInterface::class, $owner);
    $this->assertEquals($this->user
        ->id(), $owner->id());
}

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