Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\field\Tests\TestItemTest.
*/
namespace Drupal\field\Tests;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
Alex Pott
committed
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
Angie Byron
committed
/**
* Tests the new entity API for the test field type.
*
* @group field
Angie Byron
committed
*/
Angie Byron
committed
class TestItemTest extends FieldUnitTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
Dries Buytaert
committed
/**
* The name of the field to use in this test.
*
* @var string
*/
Alex Pott
committed
protected $fieldName = 'field_test';
Dries Buytaert
committed
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
// Create a 'test_field' field and storage for validation.
entity_create('field_storage_config', array(
Alex Pott
committed
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
Angie Byron
committed
'type' => 'test_field',
))->save();
entity_create('field_config', array(
Angie Byron
committed
'entity_type' => 'entity_test',
Alex Pott
committed
'field_name' => $this->fieldName,
Angie Byron
committed
'bundle' => 'entity_test',
))->save();
Angie Byron
committed
}
/**
* Tests using entity fields of the field field type.
*/
public function testTestItem() {
// Verify entity creation.
$entity = entity_create('entity_test');
Dries Buytaert
committed
$value = rand(1, 10);
Angie Byron
committed
$entity->field_test = $value;
Alex Pott
committed
$entity->name->value = $this->randomMachineName();
Angie Byron
committed
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
Alex Pott
committed
$this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->{$this->fieldName}->value, $value);
$this->assertEqual($entity->{$this->fieldName}[0]->value, $value);
Angie Byron
committed
// Verify changing the field value.
Dries Buytaert
committed
$new_value = rand(1, 10);
Angie Byron
committed
$entity->field_test->value = $new_value;
Alex Pott
committed
$this->assertEqual($entity->{$this->fieldName}->value, $new_value);
Angie Byron
committed
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
Alex Pott
committed
$this->assertEqual($entity->{$this->fieldName}->value, $new_value);
Alex Pott
committed
// Test the schema for this field type.
$expected_schema = array(
'columns' => array(
'value' => array(
'type' => 'int',
'size' => 'medium',
),
),
Alex Pott
committed
'unique keys' => array(),
Alex Pott
committed
'indexes' => array(
'value' => array('value'),
),
'foreign keys' => array(),
);
Alex Pott
committed
$field_schema = BaseFieldDefinition::create('test_field')->getSchema();
Alex Pott
committed
$this->assertEqual($field_schema, $expected_schema);