KeyValueFormatterTest.php in Key value field 8
File
tests/src/Kernel/KeyValueFormatterTest.php
View source
<?php
namespace Drupal\Tests\key_value_field\Kernel;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\entity_test\Entity\EntityTest;
class KeyValueFormatterTest extends KernelTestBase {
public function testFormatter() {
$this
->createTestField('key_value');
$entity_view_display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
]);
$entity_view_display
->setComponent('test_key_value_field', [
'type' => 'key_value',
]);
$entity_view_display
->save();
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => "orange",
'key' => 'apple',
],
]);
$entity
->save();
$build = $entity_view_display
->build($entity);
$output = $this
->render($build);
$this
->setRawContent($output);
$this
->assertRaw("apple : <p>orange</p>\n");
}
public function testValueOnlyFormatter() {
$this
->createTestField('key_value');
$entity_view_display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
]);
$entity_view_display
->setComponent('test_key_value_field', [
'type' => 'key_value',
'settings' => [
'value_only' => TRUE,
],
]);
$entity_view_display
->save();
$entity = EntityTest::create([
'test_key_value_field' => [
'value' => "orange",
'key' => 'apple',
],
]);
$entity
->save();
$build = $entity_view_display
->build($entity);
$output = $this
->render($build);
$this
->setRawContent($output);
$this
->assertRaw("<div><p>orange</p>\n", 'Found', 'KeyV');
}
}