You are here

protected function ResourceTestBase::getNestedIncludePaths in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getNestedIncludePaths()

Gets an array of of all nested include paths to be tested.

Parameters

int $depth: (optional) The maximum depth to which included paths should be nested.

Return value

array An array of nested include paths.

1 call to ResourceTestBase::getNestedIncludePaths()
ResourceTestBase::doTestIncluded in tests/src/Functional/ResourceTestBase.php
Tests included resources.

File

tests/src/Functional/ResourceTestBase.php, line 2808

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getNestedIncludePaths($depth = 3) {
  $get_nested_relationship_field_names = function (EntityInterface $entity, $depth, $path = "") use (&$get_nested_relationship_field_names) {
    $relationship_field_names = $this
      ->getRelationshipFieldNames($entity);
    if ($depth > 0) {

      // @todo remove the line below and uncomment the following line in https://www.drupal.org/project/jsonapi/issues/2946537
      $paths = $path ? [
        $path,
      ] : [];

      /* $paths = []; */
      foreach ($relationship_field_names as $field_name) {
        $next = $path ? "{$path}.{$field_name}" : $field_name;
        if ($target_entity = $entity->{$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);
}