View source
<?php
namespace Drupal\Tests\office_hours\Kernel;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
class OfficeHoursItemTest extends FieldKernelTestBase {
protected $fieldStorage;
protected $field;
protected static $modules = [
'office_hours',
];
protected function setUp() : void {
parent::setUp();
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_office_hours',
'type' => 'office_hours',
'entity_type' => 'entity_test',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'element_type' => 'office_hours_datelist',
],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'cardinality_per_day' => 2,
],
'default_value' => [
[
'day' => 0,
'starthours' => 900,
'endhours' => 1730,
'comment' => 'Test comment',
],
[
'day' => 1,
'starthours' => 700,
'endhours' => 1800,
'comment' => 'Test comment',
],
],
]);
$this->field
->save();
$entity_display = EntityViewDisplay::create([
'targetEntityType' => $this->field
->getTargetEntityTypeId(),
'bundle' => $this->field
->getTargetBundle(),
'mode' => 'default',
]);
$entity_display
->setComponent('field_office_hours', [
'type' => 'office_hours_table',
]);
$entity_display
->save();
$entity_display
->setComponent('field_office_hours', [
'type' => 'office_hours',
]);
$entity_display
->save();
}
public function testOfficeHoursField() {
$this->fieldStorage
->setSetting('element_type', 'office_hours_datelist');
$this->fieldStorage
->save();
$entity = EntityTest::create();
$field_name = 'field_office_hours';
$value = [
[
'day' => '2',
'starthours' => '1330',
'endhours' => '2000',
'comment' => '',
],
[
'day' => '3',
'starthours' => '900',
'endhours' => '2000',
'comment' => '',
],
];
$entity
->set($field_name, $value);
$entity
->setName($this
->randomMachineName());
$this
->entityValidateAndSave($entity);
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity
->get($field_name));
$this
->assertInstanceOf(FieldItemInterface::class, $entity
->get($field_name)
->first());
$new_value = [
[
'day' => '2',
'starthours' => '1430',
'endhours' => '2000',
'comment' => '',
],
[
'day' => '3',
'starthours' => '1900',
'endhours' => '2000',
'comment' => '',
],
];
$entity->{$field_name}
->setValue($new_value);
$test_value = $entity->{$field_name}
->first()
->getValue();
$this
->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$test_value = $entity->{$field_name}
->first()
->getValue();
$this
->assertEquals(implode('/', $new_value[0]), implode('/', $test_value));
$entity = EntityTest::create();
$entity->{$field_name}
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}