TelephoneItemTest.php in Drupal 10
File
core/modules/telephone/tests/src/Kernel/TelephoneItemTest.php
View source
<?php
namespace Drupal\Tests\telephone\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
class TelephoneItemTest extends FieldKernelTestBase {
protected static $modules = [
'telephone',
];
protected function setUp() : void {
parent::setUp();
FieldStorageConfig::create([
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'telephone',
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
'default_value' => [
0 => [
'value' => '+012345678',
],
],
])
->save();
}
public function testTestItem() {
$entity = EntityTest::create();
$value = '+0123456789';
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_test);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_test[0]);
$this
->assertEquals($value, $entity->field_test->value);
$this
->assertEquals($value, $entity->field_test[0]->value);
$new_value = '+41' . rand(1000000, 9999999);
$entity->field_test->value = $new_value;
$this
->assertEquals($new_value, $entity->field_test->value);
$entity
->save();
$entity = EntityTest::load($id);
$this
->assertEquals($new_value, $entity->field_test->value);
$entity = EntityTest::create();
$entity->field_test
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}