View source
<?php
namespace Drupal\Tests\field\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class DisplayApiTest extends FieldKernelTestBase {
protected $fieldName;
protected $label;
protected $cardinality;
protected $displayOptions;
protected $entity;
protected $values;
protected static $modules = [
'system',
];
protected function setUp() : void {
parent::setUp();
$this->fieldName = 'test_field';
$this->label = $this
->randomMachineName();
$this->cardinality = 4;
$field_storage = [
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'test_field',
'cardinality' => $this->cardinality,
];
$field = [
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => $this->label,
];
$this->displayOptions = [
'default' => [
'type' => 'field_test_default',
'settings' => [
'test_formatter_setting' => $this
->randomMachineName(),
],
],
'teaser' => [
'type' => 'field_test_default',
'settings' => [
'test_formatter_setting' => $this
->randomMachineName(),
],
],
];
$display_repository = \Drupal::service('entity_display.repository');
FieldStorageConfig::create($field_storage)
->save();
FieldConfig::create($field)
->save();
$display_repository
->getViewDisplay($field['entity_type'], $field['bundle'])
->setComponent($this->fieldName, $this->displayOptions['default'])
->save();
EntityViewMode::create([
'id' => 'entity_test.teaser',
'targetEntityType' => 'entity_test',
])
->save();
$display_repository
->getViewDisplay($field['entity_type'], $field['bundle'], 'teaser')
->setComponent($this->fieldName, $this->displayOptions['teaser'])
->save();
$this->values = $this
->_generateTestFieldValues($this->cardinality);
$this->entity = EntityTest::create();
$this->entity->{$this->fieldName}
->setValue($this->values);
$this->entity
->save();
}
public function testFieldItemListView() {
$items = $this->entity
->get($this->fieldName);
\Drupal::service('theme_installer')
->install([
'classy',
]);
$this
->config('system.theme')
->set('default', 'classy')
->save();
$build = $items
->view();
$this
->render($build);
$settings = \Drupal::service('plugin.manager.field.formatter')
->getDefaultSettings('field_test_default');
$setting = $settings['test_formatter_setting'];
$this
->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$display = [
'label' => 'hidden',
'type' => 'field_test_multiple',
'settings' => [
'test_formatter_setting_multiple' => $this
->randomMachineName(),
'alter' => TRUE,
],
];
$build = $items
->view($display);
$this
->render($build);
$setting = $display['settings']['test_formatter_setting_multiple'];
$this
->assertNoText($this->label, 'Label was not displayed.');
$this
->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
$this
->assertText('entity language is en', 'Language is placed onto the context.');
$array = [];
foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value'];
}
$this
->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
$display = [
'label' => 'visually_hidden',
'type' => 'field_test_multiple',
'settings' => [
'test_formatter_setting_multiple' => $this
->randomMachineName(),
'alter' => TRUE,
],
];
$build = $items
->view($display);
$this
->render($build);
$setting = $display['settings']['test_formatter_setting_multiple'];
$this
->assertRaw('visually-hidden', 'Label was visually hidden.');
$this
->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
$this
->assertText('entity language is en', 'Language is placed onto the context.');
$array = [];
foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value'];
}
$this
->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
$display = [
'label' => 'hidden',
'type' => 'field_test_with_prepare_view',
'settings' => [
'test_formatter_setting_additional' => $this
->randomMachineName(),
],
];
$build = $items
->view($display);
$this
->render($build);
$setting = $display['settings']['test_formatter_setting_additional'];
$this
->assertNoText($this->label, 'Label was not displayed.');
$this
->assertNoText('field_test_entity_display_build_alter', 'Alter not fired.');
foreach ($this->values as $delta => $value) {
$this
->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$build = $items
->view('teaser');
$this
->render($build);
$setting = $this->displayOptions['teaser']['settings']['test_formatter_setting'];
$this
->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$build = $items
->view('unknown_view_mode');
$this
->render($build);
$setting = $this->displayOptions['default']['settings']['test_formatter_setting'];
$this
->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
}
public function testFieldItemView() {
$settings = \Drupal::service('plugin.manager.field.formatter')
->getDefaultSettings('field_test_default');
$setting = $settings['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item
->view();
$this
->render($build);
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$display = [
'type' => 'field_test_multiple',
'settings' => [
'test_formatter_setting_multiple' => $this
->randomMachineName(),
],
];
$setting = $display['settings']['test_formatter_setting_multiple'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item
->view($display);
$this
->render($build);
$this
->assertText($setting . '|0:' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$display = [
'type' => 'field_test_with_prepare_view',
'settings' => [
'test_formatter_setting_additional' => $this
->randomMachineName(),
],
];
$setting = $display['settings']['test_formatter_setting_additional'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item
->view($display);
$this
->render($build);
$this
->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$setting = $this->displayOptions['teaser']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item
->view('teaser');
$this
->render($build);
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
$setting = $this->displayOptions['default']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item
->view('unknown_view_mode');
$this
->render($build);
$this
->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
}
public function testFieldEmpty() {
$display = [
'label' => 'hidden',
'type' => 'field_empty_test',
'settings' => [
'test_empty_string' => '**EMPTY FIELD**' . $this
->randomMachineName(),
],
];
$build = $this->entity
->get($this->fieldName)
->view($display);
$this
->render($build);
$this
->assertNoText($display['settings']['test_empty_string']);
$this->entity->{$this->fieldName} = [];
$this->entity
->save();
$build = $this->entity
->get($this->fieldName)
->view($display);
$this
->render($build);
$this
->assertText($display['settings']['test_empty_string']);
}
}