TelephoneItemTest.php in Zircon Profile 8
File
core/modules/telephone/src/Tests/TelephoneItemTest.php
View source
<?php
namespace Drupal\telephone\Tests;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
class TelephoneItemTest extends FieldUnitTestBase {
public static $modules = array(
'telephone',
);
protected function setUp() {
parent::setUp();
entity_create('field_storage_config', array(
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'telephone',
))
->save();
entity_create('field_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
))
->save();
}
public function testTestItem() {
$entity = entity_create('entity_test');
$value = '+0123456789';
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = entity_load('entity_test', $id);
$this
->assertTrue($entity->field_test instanceof FieldItemListInterface, '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);
$new_value = '+41' . rand(1000000, 9999999);
$entity->field_test->value = $new_value;
$this
->assertEqual($entity->field_test->value, $new_value);
$entity
->save();
$entity = entity_load('entity_test', $id);
$this
->assertEqual($entity->field_test->value, $new_value);
$entity = entity_create('entity_test');
$entity->field_test
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}