Commit a63c6cbc authored by webchick's avatar webchick

Issue #1839082 by Berdir: Implement the new entity field API for the field test field type.

parent a06421e8
......@@ -7,22 +7,21 @@
namespace Drupal\email\Tests;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\email\Type\EmailItem;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldItemUnitTestBase;
/**
* Tests the new entity API for the email field type.
*/
class EmailItemTest extends DrupalUnitTestBase {
class EmailItemTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('email', 'entity_test');
public static $modules = array('email');
public static function getInfo() {
return array(
......@@ -34,7 +33,6 @@ public static function getInfo() {
public function setUp() {
parent::setUp();
$this->enableModules(array('system', 'entity', 'field', 'text', 'options', 'field_sql_storage', 'entity_test', 'email'));
// Create an email field and instance for validation.
$this->field = array(
......
<?php
/**
* @file
* Contains \Drupal\field\Tests\FieldItemUnitTestBase.
*/
namespace Drupal\field\Tests;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Base test class for field type item tests.
*/
class FieldItemUnitTestBase extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'system', 'field', 'text', 'field_sql_storage', 'field_test', 'entity_test');
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installSchema('field', 'field_config');
$this->installSchema('field', 'field_config_instance');
$this->installSchema('entity_test', 'entity_test');
}
}
<?php
/**
* @file
* Contains \Drupal\field\Tests\ShapeItemTest.
*/
namespace Drupal\field\Tests;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Entity\Field\FieldInterface;
/**
* Tests the new entity API for the shape field type.
*/
class ShapeItemTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
public static function getInfo() {
return array(
'name' => 'Shape field item',
'description' => 'Tests the new entity API for the shape field type.',
'group' => 'Field types',
);
}
public function setUp() {
parent::setUp();
// Create an field field and instance for validation.
$this->field = array(
'field_name' => 'field_shape',
'type' => 'shape',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_shape',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'test_field_widget',
),
);
field_create_instance($this->instance);
}
/**
* Tests using entity fields of the field field type.
*/
public function testShapeItem() {
// Verify entity creation.
$entity = entity_create('entity_test', array());
$shape = 'cube';
$color = 'blue';
$entity->field_shape->shape = $shape;
$entity->field_shape->color = $color;
$entity->name->value = $this->randomName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_shape instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_shape[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_shape->shape, $shape);
$this->assertEqual($entity->field_shape->color, $color);
$this->assertEqual($entity->field_shape[0]->shape, $shape);
$this->assertEqual($entity->field_shape[0]->color, $color);
// Verify changing the field value.
$new_shape = 'circle';
$new_color = 'red';
$entity->field_shape->shape = $new_shape;
$entity->field_shape->color = $new_color;
$this->assertEqual($entity->field_shape->shape, $new_shape);
$this->assertEqual($entity->field_shape->color, $new_color);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_shape->shape, $new_shape);
$this->assertEqual($entity->field_shape->color, $new_color);
}
}
<?php
/**
* @file
* Contains \Drupal\field\Tests\TestItemTest.
*/
namespace Drupal\field\Tests;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Entity\Field\FieldInterface;
/**
* Tests the new entity API for the test field type.
*/
class TestItemTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
public static function getInfo() {
return array(
'name' => 'Test field item',
'description' => 'Tests the new entity API for the test field type.',
'group' => 'Field types',
);
}
public function setUp() {
parent::setUp();
// Create an field field and instance for validation.
$this->field = array(
'field_name' => 'field_test',
'type' => 'test_field',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'test_field_widget',
),
);
field_create_instance($this->instance);
}
/**
* Tests using entity fields of the field field type.
*/
public function testTestItem() {
// Verify entity creation.
$entity = entity_create('entity_test', array());
$value = $this->randomName();
$entity->field_test = $value;
$entity->name->value = $this->randomName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test->value, $value);
$this->assertEqual($entity->field_test[0]->value, $value);
// Verify changing the field value.
$new_value = $this->randomName();
$entity->field_test->value = $new_value;
$this->assertEqual($entity->field_test->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_test->value, $new_value);
}
}
......@@ -172,6 +172,10 @@ function field_test_create_entity($id = 1, $vid = 1, $bundle = 'test_bundle', $l
* The loaded entity.
*/
function field_test_entity_test_load($ftid, $ftvid = NULL) {
// Prevent this from being called as hook_entity_test_load().
if (is_array($ftid)) {
return;
}
$ids = (isset($ftid) ? array($ftid) : array());
$conditions = (isset($ftvid) ? array('ftvid' => $ftvid) : array());
$test_entity = entity_load_multiple('test_entity', $ids, $conditions);
......
......@@ -27,6 +27,7 @@ function field_test_field_info() {
),
'default_widget' => 'test_field_widget',
'default_formatter' => 'field_test_default',
'field item class' => 'Drupal\field_test\Type\TestItem',
),
'shape' => array(
'label' => t('Shape'),
......@@ -35,6 +36,7 @@ function field_test_field_info() {
'instance_settings' => array(),
'default_widget' => 'test_field_widget',
'default_formatter' => 'field_test_default',
'field item class' => 'Drupal\field_test\Type\ShapeItem',
),
'hidden_test_field' => array(
'no_ui' => TRUE,
......@@ -44,6 +46,7 @@ function field_test_field_info() {
'instance_settings' => array(),
'default_widget' => 'test_field_widget',
'default_formatter' => 'field_test_default',
'field item class' => 'Drupal\field_test\Type\TestItem',
),
);
}
......
<?php
/**
* @file
* Contains \Drupal\field_test\Type\ShapeItem.
*/
namespace Drupal\field_test\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'shape_field' entity field item.
*/
class ShapeItem extends FieldItemBase {
/**
* Property definitions of the contained properties.
*
* @see ShapeItem::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['shape'] = array(
'type' => 'string',
'label' => t('Shape'),
);
static::$propertyDefinitions['color'] = array(
'type' => 'string',
'label' => t('Color'),
);
}
return static::$propertyDefinitions;
}
}
<?php
/**
* @file
* Contains \Drupal\field_test\Type\TestItem.
*/
namespace Drupal\field_test\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'test_field' entity field item.
*/
class TestItem extends FieldItemBase {
/**
* Property definitions of the contained properties.
*
* @see TestItem::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['value'] = array(
'type' => 'integer',
'label' => t('Test integer value'),
);
}
return static::$propertyDefinitions;
}
}
......@@ -7,26 +7,25 @@
namespace Drupal\taxonomy\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Type\TaxonomyTermReferenceItem;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldItemUnitTestBase;
/**
* Tests the new entity API for the taxonomy term reference field type.
*/
class TaxonomyTermReferenceItemTest extends WebTestBase {
class TaxonomyTermReferenceItemTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field', 'field_sql_storage', 'taxonomy', 'entity_test', 'options');
public static $modules = array('taxonomy', 'options');
public static function getInfo() {
return array(
'name' => 'Taxonomy reference API',
'name' => 'Taxonomy reference field item',
'description' => 'Tests using entity fields of the taxonomy term reference field type.',
'group' => 'Taxonomy',
);
......@@ -34,6 +33,9 @@ public static function getInfo() {
public function setUp() {
parent::setUp();
$this->installSchema('taxonomy', 'taxonomy_term_data');
$this->installSchema('taxonomy', 'taxonomy_term_hierarchy');
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomName(),
'vid' => drupal_strtolower($this->randomName()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment