View source
<?php
namespace Drupal\Tests\key_value_field\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\filter\Entity\FilterFormat;
class KeyValueLongItemTest extends KernelTestBase {
public function testFieldCreate() {
$this
->createTestField('key_value_long');
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => '',
],
]);
$entity
->save();
$this
->assertTrue($entity->test_key_value_field
->isEmpty());
$this
->assertEquals(NULL, $entity->test_key_value_field->value);
}
public function testFieldCreateWithDefaultValue() {
$field_settings = [
'default_value' => [
0 => [
'value' => 'orange',
'key' => 'apple',
],
],
];
$this
->createTestField('key_value_long', [], $field_settings);
$entity = EntityTest::create([]);
$entity
->save();
$this
->assertFalse($entity->test_key_value_field
->isEmpty());
$this
->assertEquals('apple', $entity->test_key_value_field->key);
$this
->assertEquals('orange', $entity->test_key_value_field->value);
$this
->assertEquals(NULL, $entity->test_key_value_field->description);
}
public function testWithData() {
$this
->createTestField('key_value_long');
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => 'orange',
'key' => 'apple',
],
]);
$entity
->save();
$this
->assertFalse($entity->test_key_value_field
->isEmpty());
$this
->assertEquals('apple', $entity->test_key_value_field->key);
$this
->assertEquals('orange', $entity->test_key_value_field->value);
$this
->assertEquals(NULL, $entity->test_key_value_field->description);
$this
->assertEquals(NULL, $entity->test_key_value_field->format);
}
public function testWithDifferentDefaultFormat() {
$format = FilterFormat::load('plain_text')
->createDuplicate()
->set('format', 'muh');
$format
->save();
$this
->createTestField('key_value_long', [], [
'default_format' => 'muh',
]);
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => 'orange',
'key' => 'apple',
],
]);
$entity
->save();
$this
->markTestSkipped('You cannot yet configure a different default format???');
$this
->assertEquals('muh', $entity->test_key_value_field->format);
}
public function testWithDataAndDescription() {
$this
->createTestField('key_value_long');
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => 'orange',
'key' => 'apple',
'description' => 'some description text',
],
]);
$entity
->save();
$this
->assertFalse($entity->test_key_value_field
->isEmpty());
$this
->assertEquals('apple', $entity->test_key_value_field->key);
$this
->assertEquals('orange', $entity->test_key_value_field->value);
$this
->assertEquals('some description text', $entity->test_key_value_field->description);
}
public function testMaximumKeyLength() {
$this
->createTestField('key_value_long', [
'settings' => [
'key_max_length' => 10,
],
]);
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => 'orange',
'key' => 'this-is-really-a-long-key',
],
]);
$entity
->save();
}
}