EntityFieldDefaultValueTest.php 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Entity\EntityFieldDefaultValueTest.
 */

namespace Drupal\system\Tests\Entity;

10 11
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Utility\String;
12
use Drupal\Core\Language\LanguageInterface;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * Tests Entity API default field value functionality.
 */
class EntityFieldDefaultValueTest extends EntityUnitTestBase  {

  /**
   * The UUID object to be used for generating UUIDs.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuid;

  public static function getInfo() {
    return array(
      'name' => 'Entity Field Default Value',
      'description' => 'Tests default values for entity fields.',
      'group' => 'Entity API',
    );
  }

  public function setUp() {
    parent::setUp();
    // Initiate the generator object.
37
    $this->uuid = $this->container->get('uuid');
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  }

  /**
   * Tests default values on entities and fields.
   */
  public function testDefaultValues() {
    // All entity variations have to have the same results.
    foreach (entity_test_entity_types() as $entity_type) {
      $this->assertDefaultValues($entity_type);
    }
  }

  /**
   * Executes a test set for a defined entity type.
   *
   * @param string $entity_type
   *   The entity type to run the tests with.
   */
  protected function assertDefaultValues($entity_type) {
57
    $entity = entity_create($entity_type);
58
    $this->assertEqual($entity->langcode->value, LanguageInterface::LANGCODE_NOT_SPECIFIED, String::format('%entity_type: Default language', array('%entity_type' => $entity_type)));
59
    $this->assertTrue(Uuid::isValid($entity->uuid->value), String::format('%entity_type: Default UUID', array('%entity_type' => $entity_type)));
60 61
    $this->assertEqual($entity->name->getValue(), array(0 => array('value' => NULL)), 'Field has one empty value by default.');
  }
62 63 64 65 66 67 68 69 70 71 72

  /**
   * Tests custom default value callbacks.
   */
  public function testDefaultValueCallback() {
    $entity = $this->entityManager->getStorage('entity_test_default_value')->create();
    // The description field has a default value callback for testing, see
    // entity_test_field_default_value().
    $this->assertEqual($entity->description->value, 'description_' . $entity->language()->id);
  }

73
}