function FilterTest::testQueryCondition

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php \Drupal\Tests\jsonapi\Kernel\Query\FilterTest::testQueryCondition()
  2. 8.9.x core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php \Drupal\Tests\jsonapi\Kernel\Query\FilterTest::testQueryCondition()
  3. 10 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 152

Class

FilterTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21Query%21Filter.php/class/Filter/11.x" title="Gathers information about the filter parameter." class="local">\Drupal\jsonapi\Query\Filter</a> @group jsonapi @group jsonapi_query @group #slow

Namespace

Drupal\Tests\jsonapi\Kernel\Query

Code

public function testQueryCondition() : void {
    // 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');
        $m2 = $o->getMethod('compile');
        // The private property computed by the two previous private calls, whose
        // value we need to inspect.
        $p = $o->getProperty('sqlQuery');
        $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());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.