annotate core/modules/taxonomy/tests/src/Kernel/TermEntityReferenceTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\Tests\taxonomy\Kernel;
Chris@5 4
Chris@5 5 use Drupal\field\Entity\FieldStorageConfig;
Chris@5 6 use Drupal\field\Entity\FieldConfig;
Chris@5 7 use Drupal\KernelTests\KernelTestBase;
Chris@5 8 use Drupal\taxonomy\Entity\Term;
Chris@5 9 use Drupal\taxonomy\Entity\Vocabulary;
Chris@5 10
Chris@5 11 /**
Chris@5 12 * Tests the settings of restricting term selection to a single vocabulary.
Chris@5 13 *
Chris@5 14 * @group taxonomy
Chris@5 15 */
Chris@5 16 class TermEntityReferenceTest extends KernelTestBase {
Chris@5 17
Chris@5 18 /**
Chris@5 19 * {@inheritdoc}
Chris@5 20 */
Chris@5 21 public static $modules = [
Chris@5 22 'entity_test',
Chris@5 23 'field',
Chris@5 24 'system',
Chris@5 25 'taxonomy',
Chris@5 26 'text',
Chris@5 27 'user',
Chris@5 28 ];
Chris@5 29
Chris@5 30 /**
Chris@5 31 * {@inheritdoc}
Chris@5 32 */
Chris@5 33 protected function setUp() {
Chris@5 34 parent::setUp();
Chris@5 35
Chris@5 36 $this->installEntitySchema('entity_test');
Chris@5 37 $this->installEntitySchema('taxonomy_term');
Chris@5 38 $this->installEntitySchema('user');
Chris@5 39 }
Chris@5 40
Chris@5 41 /**
Chris@5 42 * Tests an entity reference field restricted to a single vocabulary.
Chris@5 43 *
Chris@5 44 * Creates two vocabularies with a term, then set up the entity reference
Chris@5 45 * field to limit the target vocabulary to one of them, ensuring that
Chris@5 46 * the restriction applies.
Chris@5 47 */
Chris@5 48 public function testSelectionTestVocabularyRestriction() {
Chris@5 49 // Create two vocabularies.
Chris@5 50 $vocabulary = Vocabulary::create([
Chris@5 51 'name' => 'test1',
Chris@5 52 'vid' => 'test1',
Chris@5 53 ]);
Chris@5 54 $vocabulary->save();
Chris@5 55 $vocabulary2 = Vocabulary::create([
Chris@5 56 'name' => 'test2',
Chris@5 57 'vid' => 'test2',
Chris@5 58 ]);
Chris@5 59 $vocabulary2->save();
Chris@5 60
Chris@5 61 $term = Term::create([
Chris@5 62 'name' => 'term1',
Chris@5 63 'vid' => $vocabulary->id(),
Chris@5 64 ]);
Chris@5 65 $term->save();
Chris@5 66 $term2 = Term::create([
Chris@5 67 'name' => 'term2',
Chris@5 68 'vid' => $vocabulary2->id(),
Chris@5 69 ]);
Chris@5 70 $term2->save();
Chris@5 71
Chris@5 72 // Create an entity reference field.
Chris@5 73 $field_name = 'taxonomy_' . $vocabulary->id();
Chris@5 74 $field_storage = FieldStorageConfig::create([
Chris@5 75 'field_name' => $field_name,
Chris@5 76 'entity_type' => 'entity_test',
Chris@5 77 'translatable' => FALSE,
Chris@5 78 'settings' => [
Chris@5 79 'target_type' => 'taxonomy_term',
Chris@5 80 ],
Chris@5 81 'type' => 'entity_reference',
Chris@5 82 'cardinality' => 1,
Chris@5 83 ]);
Chris@5 84 $field_storage->save();
Chris@5 85 $field = FieldConfig::create([
Chris@5 86 'field_storage' => $field_storage,
Chris@5 87 'entity_type' => 'entity_test',
Chris@5 88 'bundle' => 'test_bundle',
Chris@5 89 'settings' => [
Chris@5 90 'handler' => 'default',
Chris@5 91 'handler_settings' => [
Chris@5 92 // Restrict selection of terms to a single vocabulary.
Chris@5 93 'target_bundles' => [
Chris@5 94 $vocabulary->id() => $vocabulary->id(),
Chris@5 95 ],
Chris@5 96 ],
Chris@5 97 ],
Chris@5 98 ]);
Chris@5 99 $field->save();
Chris@5 100
Chris@5 101 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
Chris@5 102 $result = $handler->getReferenceableEntities();
Chris@5 103
Chris@5 104 $expected_result = [
Chris@5 105 $vocabulary->id() => [
Chris@5 106 $term->id() => $term->getName(),
Chris@5 107 ],
Chris@5 108 ];
Chris@5 109
Chris@5 110 $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');
Chris@5 111 }
Chris@5 112
Chris@5 113 }