You are here

protected function ProductVariationResourceTest::getNestedIncludePaths in Commerce Core 8.2

@todo remove after https://www.drupal.org/project/drupal/issues/3163590

Overrides ResourceTestBase::getNestedIncludePaths

File

modules/product/tests/src/Functional/Jsonapi/ProductVariationResourceTest.php, line 329

Class

ProductVariationResourceTest
JSON:API resource test for variations.

Namespace

Drupal\Tests\commerce_product\Functional\Jsonapi

Code

protected function getNestedIncludePaths($depth = 3) {
  $resource_type_repository = $this->container
    ->get('jsonapi.resource_type.repository');
  $get_nested_relationship_field_names = function (EntityInterface $entity, $depth, $path = "") use (&$get_nested_relationship_field_names, $resource_type_repository) {
    $resource_type = $resource_type_repository
      ->get($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    $relationship_field_names = $this
      ->getRelationshipFieldNames($entity, $resource_type);
    if ($depth > 0) {
      $paths = [];
      foreach ($relationship_field_names as $field_name) {
        $next = $path ? "{$path}.{$field_name}" : $field_name;

        // @note this is where it gets weird.
        // variation -> type (bundle ref)
        // product -> type (bundle ref)
        // store -> type (bundle ref)
        // jsonapi auto aliases `type` to `{entity_type_id}_type`
        $internal_field_name = $resource_type
          ->getInternalName($field_name);
        if (!is_object($entity->{$internal_field_name})) {
          throw new \RuntimeException("{$entity->getEntityTypeId()}: {$field_name} ({$internal_field_name})");
        }
        if ($target_entity = $entity->{$internal_field_name}->entity) {
          $deep = $get_nested_relationship_field_names($target_entity, $depth - 1, $next);
          $paths = array_merge($paths, $deep);
        }
        else {
          $paths[] = $next;
        }
      }
      return $paths;
    }
    return array_map(function ($target_name) use ($path) {
      return "{$path}.{$target_name}";
    }, $relationship_field_names);
  };
  return $get_nested_relationship_field_names($this->entity, $depth);
}