EntityFieldDefaultValueTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php
View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Utility\SafeMarkup;
class EntityFieldDefaultValueTest extends EntityUnitTestBase {
protected $uuid;
protected function setUp() {
parent::setUp();
$this->uuid = $this->container
->get('uuid');
}
public function testDefaultValues() {
foreach (entity_test_entity_types() as $entity_type) {
$this
->assertDefaultValues($entity_type);
}
}
protected function assertDefaultValues($entity_type_id) {
$entity = entity_create($entity_type_id);
$definition = $this->entityManager
->getDefinition($entity_type_id);
$langcode_key = $definition
->getKey('langcode');
$this
->assertEqual($entity->{$langcode_key}->value, 'en', SafeMarkup::format('%entity_type: Default language', array(
'%entity_type' => $entity_type_id,
)));
$this
->assertTrue(Uuid::isValid($entity->uuid->value), SafeMarkup::format('%entity_type: Default UUID', array(
'%entity_type' => $entity_type_id,
)));
$this
->assertEqual($entity->name
->getValue(), array(), 'Field has one empty value by default.');
}
public function testDefaultValueCallback() {
$entity = $this->entityManager
->getStorage('entity_test_default_value')
->create();
$string = 'description_' . $entity
->language()
->getId();
$expected = array(
array(
'shape' => "shape:0:{$string}",
'color' => "color:0:{$string}",
),
array(
'shape' => "shape:1:{$string}",
'color' => "color:1:{$string}",
),
);
$this
->assertEqual($entity->description
->getValue(), $expected);
}
}