You are here

public function TermSelectionTest::testGetReferenceableEntities in Taxonomy container 8

Tests that terms are returned in the correct hierarchical format.

@covers ::getReferenceableEntities

@dataProvider getReferenceableEntitiesProvider

File

tests/src/Unit/TermSelectionTest.php, line 109

Class

TermSelectionTest
Tests selection of taxonomy terms by the Taxonomy Container module.

Namespace

Drupal\Tests\taxonomy_container\Unit

Code

public function testGetReferenceableEntities(array $configuration, array $vocabularies, array $expected_result) {
  $plugin = $this
    ->instantiatePlugin($configuration);

  // It is expected that the plugin will request information about the
  // available taxonomy term bundles, so that it can use this as defaults in
  // case no specific target bundles have been configured in the settings for
  // the entity reference field.
  $vocabulary_bundles = array_keys($vocabularies);
  $bundle_info = array_combine($vocabulary_bundles, array_map(function ($bundle) {
    return [
      'label' => $bundle,
    ];
  }, $vocabulary_bundles));
  $this->entityTypeBundleInfo
    ->getBundleInfo('taxonomy_term')
    ->willReturn($bundle_info);

  // It is expected that the plugin will load the available taxonomy term
  // trees for every vocabulary.
  foreach ($vocabularies as $bundle => $parent_terms) {

    // Each tree will contain the terms from our test case. Populate it with
    // the parent terms as well as the child terms.
    $tree = [];
    foreach ($parent_terms as $parent_id => $parent_term) {

      // Created a mocked Term entity for the parent term.
      $tree[] = $this
        ->getMockTerm($parent_id, $parent_term['label']);

      // Created mocked Term entities for the child terms.
      foreach ($parent_term['children'] as $child_id => $child_label) {
        $tree[] = $this
          ->getMockTerm($child_id, $child_label, $parent_id);
      }
    }
    $this->termStorage
      ->loadTree($bundle, 0, NULL, TRUE)
      ->willReturn($tree);
  }
  $result = $plugin
    ->getReferenceableEntities();
  $this
    ->assertEquals($expected_result, $result);
}