annotate core/modules/taxonomy/tests/src/Functional/TermEntityReferenceTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 6 use Drupal\field\Entity\FieldConfig;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the settings of restricting term selection to a single vocabulary.
Chris@0 10 *
Chris@0 11 * @group taxonomy
Chris@0 12 */
Chris@0 13 class TermEntityReferenceTest extends TaxonomyTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to enable.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['entity_reference_test', 'entity_test'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Tests an entity reference field restricted to a single vocabulary.
Chris@0 24 *
Chris@0 25 * Creates two vocabularies with a term, then set up the entity reference
Chris@0 26 * field to limit the target vocabulary to one of them, ensuring that
Chris@0 27 * the restriction applies.
Chris@0 28 */
Chris@0 29 public function testSelectionTestVocabularyRestriction() {
Chris@0 30
Chris@0 31 // Create two vocabularies.
Chris@0 32 $vocabulary = $this->createVocabulary();
Chris@0 33 $vocabulary2 = $this->createVocabulary();
Chris@0 34
Chris@0 35 $term = $this->createTerm($vocabulary);
Chris@0 36 $term2 = $this->createTerm($vocabulary2);
Chris@0 37
Chris@0 38 // Create an entity reference field.
Chris@0 39 $field_name = 'taxonomy_' . $vocabulary->id();
Chris@0 40 $field_storage = FieldStorageConfig::create([
Chris@0 41 'field_name' => $field_name,
Chris@0 42 'entity_type' => 'entity_test',
Chris@0 43 'translatable' => FALSE,
Chris@0 44 'settings' => [
Chris@0 45 'target_type' => 'taxonomy_term',
Chris@0 46 ],
Chris@0 47 'type' => 'entity_reference',
Chris@0 48 'cardinality' => 1,
Chris@0 49 ]);
Chris@0 50 $field_storage->save();
Chris@0 51 $field = FieldConfig::create([
Chris@0 52 'field_storage' => $field_storage,
Chris@0 53 'entity_type' => 'entity_test',
Chris@0 54 'bundle' => 'test_bundle',
Chris@0 55 'settings' => [
Chris@0 56 'handler' => 'default',
Chris@0 57 'handler_settings' => [
Chris@0 58 // Restrict selection of terms to a single vocabulary.
Chris@0 59 'target_bundles' => [
Chris@0 60 $vocabulary->id() => $vocabulary->id(),
Chris@0 61 ],
Chris@0 62 ],
Chris@0 63 ],
Chris@0 64 ]);
Chris@0 65 $field->save();
Chris@0 66
Chris@0 67 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
Chris@0 68 $result = $handler->getReferenceableEntities();
Chris@0 69
Chris@0 70 $expected_result = [
Chris@0 71 $vocabulary->id() => [
Chris@0 72 $term->id() => $term->getName(),
Chris@0 73 ],
Chris@0 74 ];
Chris@0 75
Chris@0 76 $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');
Chris@0 77 }
Chris@0 78
Chris@0 79 }