Issue #1839076 by Berdir | chx: Implement the new entity field API for the number field type.
parent
1f5e126d29
commit
076919f0a3
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\number\Tests\NumberItemTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\number\Tests;
|
||||
|
||||
use Drupal\Core\Entity\Field\FieldItemInterface;
|
||||
use Drupal\Core\Entity\Field\FieldInterface;
|
||||
use Drupal\field\Tests\FieldItemUnitTestBase;
|
||||
|
||||
/**
|
||||
* Tests the new entity API for the number field type.
|
||||
*/
|
||||
class NumberItemTest extends FieldItemUnitTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('number');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Number field items',
|
||||
'description' => 'Tests the new entity API for the number field types.',
|
||||
'group' => 'Field types',
|
||||
);
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create number fields and instances for validation.
|
||||
foreach (array('integer', 'float', 'decimal') as $type) {
|
||||
$this->field[$type] = array(
|
||||
'field_name' => 'field_' . $type,
|
||||
'type' => 'number_' . $type,
|
||||
);
|
||||
field_create_field($this->field[$type]);
|
||||
$this->instance[$type] = array(
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_' . $type,
|
||||
'bundle' => 'entity_test',
|
||||
'widget' => array(
|
||||
'type' => 'number',
|
||||
),
|
||||
);
|
||||
field_create_instance($this->instance[$type]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the number field type.
|
||||
*/
|
||||
public function testNumberItem() {
|
||||
// Verify entity creation.
|
||||
$entity = entity_create('entity_test', array());
|
||||
$integer = rand(0, 10);
|
||||
$entity->field_integer = $integer;
|
||||
$float = 3.14;
|
||||
$entity->field_float = $float;
|
||||
$decimal = '31.3';
|
||||
$entity->field_decimal = $decimal;
|
||||
$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_integer instanceof FieldInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->field_integer[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_integer->value, $integer);
|
||||
$this->assertEqual($entity->field_integer[0]->value, $integer);
|
||||
$this->assertTrue($entity->field_float instanceof FieldInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->field_float[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_float->value, $float);
|
||||
$this->assertEqual($entity->field_float[0]->value, $float);
|
||||
$this->assertTrue($entity->field_decimal instanceof FieldInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->field_decimal[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_decimal->value, $decimal);
|
||||
$this->assertEqual($entity->field_decimal[0]->value, $decimal);
|
||||
|
||||
// Verify changing the number value.
|
||||
$new_integer = rand(11, 20);
|
||||
$new_float = rand(1001, 2000) / 100;
|
||||
$new_decimal = '18.2';
|
||||
$entity->field_integer->value = $new_integer;
|
||||
$this->assertEqual($entity->field_integer->value, $new_integer);
|
||||
$entity->field_float->value = $new_float;
|
||||
$this->assertEqual($entity->field_float->value, $new_float);
|
||||
$entity->field_decimal->value = $new_decimal;
|
||||
$this->assertEqual($entity->field_decimal->value, $new_decimal);
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$entity->save();
|
||||
$entity = entity_load('entity_test', $id);
|
||||
$this->assertEqual($entity->field_integer->value, $new_integer);
|
||||
$this->assertEqual($entity->field_float->value, $new_float);
|
||||
$this->assertEqual($entity->field_decimal->value, $new_decimal);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\number\Type\DecimalItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\number\Type;
|
||||
|
||||
use Drupal\Core\Entity\Field\FieldItemBase;
|
||||
|
||||
/**
|
||||
* Defines the 'number_decimal_field' entity field item.
|
||||
*/
|
||||
class DecimalItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* Definitions of the contained properties.
|
||||
*
|
||||
* @see DecimalItem::getPropertyDefinitions()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $propertyDefinitions;
|
||||
|
||||
/**
|
||||
* Implements ComplexDataInterface::getPropertyDefinitions().
|
||||
*/
|
||||
public function getPropertyDefinitions() {
|
||||
|
||||
if (!isset(static::$propertyDefinitions)) {
|
||||
static::$propertyDefinitions['value'] = array(
|
||||
// Decimals are represented as string in PHP.
|
||||
'type' => 'string',
|
||||
'label' => t('Decimal value'),
|
||||
);
|
||||
}
|
||||
return static::$propertyDefinitions;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\number\Type\FloatItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\number\Type;
|
||||
|
||||
use Drupal\Core\Entity\Field\FieldItemBase;
|
||||
|
||||
/**
|
||||
* Defines the 'number_float_field' entity field item.
|
||||
*/
|
||||
class FloatItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* Definitions of the contained properties.
|
||||
*
|
||||
* @see FloatItem::getPropertyDefinitions()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $propertyDefinitions;
|
||||
|
||||
/**
|
||||
* Implements ComplexDataInterface::getPropertyDefinitions().
|
||||
*/
|
||||
public function getPropertyDefinitions() {
|
||||
|
||||
if (!isset(static::$propertyDefinitions)) {
|
||||
static::$propertyDefinitions['value'] = array(
|
||||
'type' => 'float',
|
||||
'label' => t('Float value'),
|
||||
);
|
||||
}
|
||||
return static::$propertyDefinitions;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\number\Type\IntegerItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\number\Type;
|
||||
|
||||
use Drupal\Core\Entity\Field\FieldItemBase;
|
||||
|
||||
/**
|
||||
* Defines the 'number_integer_field' entity field item.
|
||||
*/
|
||||
class IntegerItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* Definitions of the contained properties.
|
||||
*
|
||||
* @see IntegerItem::getPropertyDefinitions()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $propertyDefinitions;
|
||||
|
||||
/**
|
||||
* Implements ComplexDataInterface::getPropertyDefinitions().
|
||||
*/
|
||||
public function getPropertyDefinitions() {
|
||||
|
||||
if (!isset(static::$propertyDefinitions)) {
|
||||
static::$propertyDefinitions['value'] = array(
|
||||
'type' => 'integer',
|
||||
'label' => t('Integer value'),
|
||||
);
|
||||
}
|
||||
return static::$propertyDefinitions;
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ function number_field_info() {
|
|||
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
|
||||
'default_widget' => 'number',
|
||||
'default_formatter' => 'number_integer',
|
||||
'field item class' => '\Drupal\number\Type\IntegerItem',
|
||||
),
|
||||
'number_decimal' => array(
|
||||
'label' => t('Decimal'),
|
||||
|
@ -39,6 +40,7 @@ function number_field_info() {
|
|||
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
|
||||
'default_widget' => 'number',
|
||||
'default_formatter' => 'number_decimal',
|
||||
'field item class' => '\Drupal\number\Type\DecimalItem',
|
||||
),
|
||||
'number_float' => array(
|
||||
'label' => t('Float'),
|
||||
|
@ -46,6 +48,7 @@ function number_field_info() {
|
|||
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
|
||||
'default_widget' => 'number',
|
||||
'default_formatter' => 'number_decimal',
|
||||
'field item class' => '\Drupal\number\Type\FloatItem',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue