function EntityFieldDefaultValueTest::testDefaultValueCallback

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php \Drupal\KernelTests\Core\Entity\EntityFieldDefaultValueTest::testDefaultValueCallback()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php \Drupal\KernelTests\Core\Entity\EntityFieldDefaultValueTest::testDefaultValueCallback()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php \Drupal\KernelTests\Core\Entity\EntityFieldDefaultValueTest::testDefaultValueCallback()

Tests custom default value callbacks.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php, line 64

Class

EntityFieldDefaultValueTest
Tests default values for entity fields.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testDefaultValueCallback() : void {
    $entity = $this->entityTypeManager
        ->getStorage('entity_test_default_value')
        ->create();
    // The description field has a default value callback for testing, see
    // entity_test_field_default_value().
    $string = 'description_' . $entity->language()
        ->getId();
    $expected = [
        [
            'shape' => "shape:0:{$string}",
            'color' => "color:0:{$string}",
        ],
        [
            'shape' => "shape:1:{$string}",
            'color' => "color:1:{$string}",
        ],
    ];
    $this->assertEquals($expected, $entity->description
        ->getValue());
}

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