public function EntityReferenceTest::testGenericEntityFormatting in Reference Table Formatter 2.0.x
Same name and namespace in other branches
- 8 tests/src/Kernel/EntityReferenceTest.php \Drupal\Tests\reference_table_formatter\Kernel\EntityReferenceTest::testGenericEntityFormatting()
Tests with generic entity referencing.
File
- tests/
src/ Kernel/ EntityReferenceTest.php, line 51
Class
- EntityReferenceTest
- @coversDefaultClass \Drupal\reference_table_formatter\Plugin\Field\FieldFormatter\EntityReferenceTableFormatter @group reference_table_formatter
Namespace
Drupal\Tests\reference_table_formatter\KernelCode
public function testGenericEntityFormatting() {
$this
->installEntitySchema('entity_test_with_bundle');
EntityTestBundle::create([
'id' => 'test_bundle',
])
->save();
$this
->setUpChildFields('entity_test_with_bundle', 'test_bundle');
$child_0 = EntityTestWithBundle::create([
'type' => 'test_bundle',
'name' => $this
->randomMachineName(),
'field_string' => 'Foo',
]);
$child_0
->save();
$child_1 = EntityTestWithBundle::create([
'type' => 'test_bundle',
'name' => $this
->randomMachineName(),
]);
$child_1
->save();
// Setup reference field to be used on the parent entity.
$field_storage = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_reference',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => [
'target_type' => 'entity_test_with_bundle',
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => 'References',
'settings' => [
'handler' => 'default:entity_test_with_bundle',
'handler_settings' => [
'target_bundles' => [
'test_bundle' => 'test_bundle',
],
],
],
]);
$field
->save();
$parent = EntityTest::create([
'field_reference' => [
[
'target_id' => $child_0
->id(),
],
[
'target_id' => $child_1
->id(),
],
],
]);
$build = $this->renderer
->executeInRenderContext(new RenderContext(), function () use ($parent) {
return $parent->field_reference
->view([
'type' => 'entity_reference_table',
'settings' => [
'show_entity_label' => TRUE,
],
]);
});
$this
->setRawContent($this->renderer
->renderPlain($build));
$this
->assertText($child_0
->label());
$this
->assertText('Foo');
$this
->assertText($child_1
->label());
// Test no error raised with deleted referenced entity.
$child_1
->delete();
$this->renderer
->executeInRenderContext(new RenderContext(), function () use ($parent) {
return $parent->field_reference
->view([
'type' => 'entity_reference_table',
'settings' => [
'show_entity_label' => TRUE,
],
]);
});
$this
->installModule('language');
$lang = ConfigurableLanguage::createFromLangcode('ja');
$lang
->save();
$negotiator = $this
->createMock(LanguageNegotiatorInterface::class);
$negotiator
->method('initializeType')
->willReturn([
$lang,
]);
$this->container
->get('language_manager')
->setNegotiator($negotiator);
$child_0 = EntityTestWithBundle::load($child_0
->id());
$child_0
->addTranslation('ja', [
'field_string' => '大丈夫',
]);
$child_0
->save();
$parent = EntityTest::create([
'field_reference' => [
'target_id' => $child_0
->id(),
],
]);
$build = $this->renderer
->executeInRenderContext(new RenderContext(), function () use ($parent) {
return $parent->field_reference
->view([
'type' => 'entity_reference_table',
]);
});
$this
->setRawContent($this->renderer
->renderPlain($build));
$this
->assertText('大丈夫', 'Referenced entities rendered in correct language.');
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('Using non-default reference handler with reference_table_formatter has not yet been implemented.');
// Install views so we can test using an entity reference selection plugin
// other than default.
$this
->installModule('views');
$field
->setSettings([
'handler' => 'views',
'handler_settings' => [],
])
->save();
// Re-create parent entity to reload latest FieldConfig instance.
$parent = EntityTest::create([
'field_reference' => [
[
'target_id' => $child_0
->id(),
],
],
]);
$build = $this->renderer
->executeInRenderContext(new RenderContext(), function () use ($parent) {
return $parent->field_reference
->view([
'type' => 'entity_reference_table',
]);
});
}