outline-8.x-1.x-dev/tests/src/Kernel/EntryEntityReferenceTest.php
tests/src/Kernel/EntryEntityReferenceTest.php
<?php
namespace Drupal\Tests\outline\Kernel;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\outline\Entity\Entry;
use Drupal\outline\Entity\Outline;
/**
* Tests the settings of restricting entry selection to a single outline.
*
* @group outline
*/
class EntryEntityReferenceTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'entity_test',
'field',
'system',
'outline',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('entity_test');
$this->installEntitySchema('outline_entry');
$this->installEntitySchema('user');
}
/**
* Tests an entity reference field restricted to a single outline.
*
* Creates two outlines with a entry, then set up the entity reference
* field to limit the target outline to one of them, ensuring that
* the restriction applies.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function testSelectionTestOutlineRestriction() {
// Create two outlines.
$outline = Outline::create([
'name' => 'test1',
'oid' => 'test1',
]);
$outline->save();
$outline2 = Outline::create([
'name' => 'test2',
'oid' => 'test2',
]);
$outline2->save();
$entry = Entry::create([
'name' => 'entry1',
'oid' => $outline->id(),
]);
$entry->save();
$entry2 = Entry::create([
'name' => 'entry2',
'oid' => $outline2->id(),
]);
$entry2->save();
// Create an entity reference field.
$field_name = 'outline_' . $outline->id();
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'translatable' => FALSE,
'settings' => [
'target_type' => 'outline_entry',
],
'type' => 'entity_reference',
'cardinality' => 1,
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'entity_test',
'bundle' => 'test_bundle',
'settings' => [
'handler' => 'default',
'handler_settings' => [
// Restrict selection of entries to a single outline.
'target_bundles' => [
$outline->id() => $outline->id(),
],
],
],
]);
$field->save();
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
$result = $handler->getReferenceableEntities();
$expected_result = [
$outline->id() => [
$entry->id() => $entry->getName(),
],
];
$this->assertIdentical($result, $expected_result, 'Entrys selection restricted to a single outline.');
}
}
