You are here

public function ParentCandidateTest::testGetBundles in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ParentCandidateTest.php \Drupal\Tests\entity_hierarchy\Kernel\ParentCandidateTest::testGetBundles()

Tests bundles.

File

tests/src/Kernel/ParentCandidateTest.php, line 46

Class

ParentCandidateTest
Defines a class for testing ParentCandidate.

Namespace

Drupal\Tests\entity_hierarchy\Kernel

Code

public function testGetBundles() {
  $parentCandidate = $this->container
    ->get('entity_hierarchy.information.parent_candidate');
  entity_test_create_bundle('fooey', 'Fooey');
  entity_test_create_bundle('bar');
  $this
    ->setupEntityHierarchyField(self::ENTITY_TYPE, 'fooey', self::FIELD_NAME);
  $this
    ->setupEntityHierarchyField(self::ENTITY_TYPE, 'bar', self::FIELD_NAME);
  $bundles = $parentCandidate
    ->getCandidateBundles($this->parent);
  $this
    ->assertEquals([
    'entity_test',
    'fooey',
    'bar',
  ], array_keys($bundles[self::FIELD_NAME]));
  $this
    ->assertEquals([
    'label' => 'Fooey',
  ], $bundles[self::FIELD_NAME]['fooey']);

  // Add a bundle limit - prevent fooey bundle from referencing the
  // entity_test bundle.
  $field = FieldConfig::load('entity_test.fooey.parents');
  $settings = $field
    ->getSetting('handler_settings');
  $settings['target_bundles'] = [
    'bar',
  ];
  $field
    ->setSetting('handler_settings', $settings);
  $field
    ->save();
  $bundles = $parentCandidate
    ->getCandidateBundles($this->parent);
  $this
    ->assertEquals([
    'entity_test',
    'bar',
  ], array_keys($bundles[self::FIELD_NAME]));
}