You are here

public function FilterTest::testQueryCondition in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php \Drupal\Tests\jsonapi\Kernel\Query\FilterTest::testQueryCondition()

@covers ::queryCondition

File

core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php, line 144

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Query

Code

public function testQueryCondition() {

  // Can't use a data provider because we need access to the container.
  $data = $this
    ->queryConditionData();
  $get_sql_query_for_entity_query = function ($entity_query) {

    // Expose parts of \Drupal\Core\Entity\Query\Sql\Query::execute().
    $o = new \ReflectionObject($entity_query);
    $m1 = $o
      ->getMethod('prepare');
    $m1
      ->setAccessible(TRUE);
    $m2 = $o
      ->getMethod('compile');
    $m2
      ->setAccessible(TRUE);

    // The private property computed by the two previous private calls, whose
    // value we need to inspect.
    $p = $o
      ->getProperty('sqlQuery');
    $p
      ->setAccessible(TRUE);
    $m1
      ->invoke($entity_query);
    $m2
      ->invoke($entity_query);
    return (string) $p
      ->getValue($entity_query);
  };
  $resource_type = $this->resourceTypeRepository
    ->get('node', 'painting');
  foreach ($data as $case) {
    $parameter = $case[0];
    $expected_query = $case[1];
    $filter = Filter::createFromQueryParameter($parameter, $resource_type, $this->fieldResolver);
    $query = $this->nodeStorage
      ->getQuery()
      ->accessCheck(FALSE);

    // Get the query condition parsed from the input.
    $condition = $filter
      ->queryCondition($query);

    // Apply it to the query.
    $query
      ->condition($condition);

    // Verify the SQL query is exactly the same.
    $expected_sql_query = $get_sql_query_for_entity_query($expected_query);
    $actual_sql_query = $get_sql_query_for_entity_query($query);
    $this
      ->assertSame($expected_sql_query, $actual_sql_query);

    // Compare the results.
    $this
      ->assertEquals($expected_query
      ->execute(), $query
      ->execute());
  }
}