You are here

public function NodeFieldOverridesTest::testFieldOverrides in Drupal 9

Same name and namespace in other branches
  1. 8 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());
}