You are here

public function FilterTest::testCreateFromQueryParameterNested in JSON:API 8.2

@covers ::createFromQueryParameter

File

tests/src/Kernel/Query/FilterTest.php, line 330

Class

FilterTest
@coversDefaultClass \Drupal\jsonapi\Query\Filter @group jsonapi @group jsonapi_query @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Query

Code

public function testCreateFromQueryParameterNested() {
  $parameter = [
    'or-group' => [
      'group' => [
        'conjunction' => 'OR',
      ],
    ],
    'nested-or-group' => [
      'group' => [
        'conjunction' => 'OR',
        'memberOf' => 'or-group',
      ],
    ],
    'nested-and-group' => [
      'group' => [
        'conjunction' => 'AND',
        'memberOf' => 'or-group',
      ],
    ],
    'condition-0' => [
      'condition' => [
        'path' => 'field0',
        'value' => 'value0',
        'memberOf' => 'nested-or-group',
      ],
    ],
    'condition-1' => [
      'condition' => [
        'path' => 'field1',
        'value' => 'value1',
        'memberOf' => 'nested-or-group',
      ],
    ],
    'condition-2' => [
      'condition' => [
        'path' => 'field2',
        'value' => 'value2',
        'memberOf' => 'nested-and-group',
      ],
    ],
    'condition-3' => [
      'condition' => [
        'path' => 'field3',
        'value' => 'value3',
        'memberOf' => 'nested-and-group',
      ],
    ],
  ];
  $resource_type = new ResourceType('foo', 'bar', NULL);
  $filter = Filter::createFromQueryParameter($parameter, $resource_type, $this
    ->getFieldResolverMock($resource_type));
  $root = $filter
    ->root();

  // Make sure the implicit root group was added.
  $this
    ->assertEquals($root
    ->conjunction(), 'AND');

  // Ensure the or-group and the and-group were added correctly.
  $members = $root
    ->members();

  // Ensure the OR group was added.
  $or_group = $members[0];
  $this
    ->assertEquals($or_group
    ->conjunction(), 'OR');
  $or_group_members = $or_group
    ->members();

  // Make sure the nested OR group was added with the right conditions.
  $nested_or_group = $or_group_members[0];
  $this
    ->assertEquals($nested_or_group
    ->conjunction(), 'OR');
  $nested_or_group_members = $nested_or_group
    ->members();
  $this
    ->assertEquals($nested_or_group_members[0]
    ->field(), 'field0');
  $this
    ->assertEquals($nested_or_group_members[1]
    ->field(), 'field1');

  // Make sure the nested AND group was added with the right conditions.
  $nested_and_group = $or_group_members[1];
  $this
    ->assertEquals($nested_and_group
    ->conjunction(), 'AND');
  $nested_and_group_members = $nested_and_group
    ->members();
  $this
    ->assertEquals($nested_and_group_members[0]
    ->field(), 'field2');
  $this
    ->assertEquals($nested_and_group_members[1]
    ->field(), 'field3');
}