You are here

public function BaseFieldDefinitionTest::testFieldDefaultValue in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php \Drupal\Tests\Core\Entity\BaseFieldDefinitionTest::testFieldDefaultValue()

Tests field default value.

@covers ::getDefaultValue @covers ::setDefaultValue

File

core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php, line 162
Contains \Drupal\Tests\Core\Entity\BaseFieldDefinitionTest.

Class

BaseFieldDefinitionTest
Unit test for BaseFieldDefinition.

Namespace

Drupal\Tests\Core\Entity

Code

public function testFieldDefaultValue() {
  $definition = BaseFieldDefinition::create($this->fieldType);
  $default_value = array(
    'value' => $this
      ->randomMachineName(),
  );
  $expected_default_value = array(
    $default_value,
  );
  $definition
    ->setDefaultValue($default_value);
  $entity = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
    ->disableOriginalConstructor()
    ->getMock();

  // Set the field item list class to be used to avoid requiring the typed
  // data manager to retrieve it.
  $definition
    ->setClass('Drupal\\Core\\Field\\FieldItemList');
  $this
    ->assertEquals($expected_default_value, $definition
    ->getDefaultValue($entity));
  $data_definition = $this
    ->getMockBuilder('Drupal\\Core\\TypedData\\DataDefinition')
    ->disableOriginalConstructor()
    ->getMock();
  $data_definition
    ->expects($this
    ->any())
    ->method('getClass')
    ->will($this
    ->returnValue('Drupal\\Core\\Field\\FieldItemBase'));
  $definition
    ->setItemDefinition($data_definition);

  // Set default value only with a literal.
  $definition
    ->setDefaultValue($default_value['value']);
  $this
    ->assertEquals($expected_default_value, $definition
    ->getDefaultValue($entity));

  // Set default value with an indexed array.
  $definition
    ->setDefaultValue($expected_default_value);
  $this
    ->assertEquals($expected_default_value, $definition
    ->getDefaultValue($entity));

  // Set default value with an empty array.
  $definition
    ->setDefaultValue(array());
  $this
    ->assertEquals(array(), $definition
    ->getDefaultValue($entity));

  // Set default value with NULL.
  $definition
    ->setDefaultValue(NULL);
  $this
    ->assertEquals([], $definition
    ->getDefaultValue($entity));
}