/**
     * Test for the findContent() method.
     *
     * @see \eZ\Publish\API\Repository\SearchService::findContent()
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetSearchService
     * @group maplocation
     */
    public function testMapLocationDistanceWithCustomFieldSort()
    {
        $setupFactory = $this->getSetupFactory();
        if ( $setupFactory instanceof LegacyElasticsearch )
        {
            $this->markTestIncomplete( "TODO: Some issues with 'copy_to' and 'geo_point'" );
        }

        $contentType = $this->createTestPlaceContentType();

        // Create a draft to account for behaviour with ContentType in different states
        $repository = $this->getRepository();
        $contentTypeService = $repository->getContentTypeService();
        $contentService = $repository->getContentService();
        $contentTypeService->createContentTypeDraft( $contentType );

        $createStruct = $contentService->newContentCreateStruct( $contentType, "eng-GB" );
        $createStruct->alwaysAvailable = false;
        $createStruct->mainLanguageCode = "eng-GB";
        $createStruct->setField(
            "maplocation",
            array(
                "latitude" => 45.894877,
                "longitude" => 15.972699,
                "address" => "Here be wild boars",
            ),
            "eng-GB"
        );

        $draft = $contentService->createContent( $createStruct );
        $wildBoars = $contentService->publishVersion( $draft->getVersionInfo() );

        $createStruct = $contentService->newContentCreateStruct( $contentType, "eng-GB" );
        $createStruct->alwaysAvailable = false;
        $createStruct->mainLanguageCode = "eng-GB";
        $createStruct->setField(
            "maplocation",
            array(
                "latitude" => 45.927334,
                "longitude" => 15.934847,
                "address" => "A lone tree",
            ),
            "eng-GB"
        );

        $draft = $contentService->createContent( $createStruct );
        $tree = $contentService->publishVersion( $draft->getVersionInfo() );

        $createStruct = $contentService->newContentCreateStruct( $contentType, "eng-GB" );
        $createStruct->alwaysAvailable = false;
        $createStruct->mainLanguageCode = "eng-GB";
        $createStruct->setField(
            "maplocation",
            array(
                "latitude" => 45.903777,
                "longitude" => 15.958788,
                "address" => "Meadow with mushrooms",
            ),
            "eng-GB"
        );

        $draft = $contentService->createContent( $createStruct );
        $mushrooms = $contentService->publishVersion( $draft->getVersionInfo() );

        $well = array(
            "latitude" => 43.756825,
            "longitude" => 15.775074
        );

        $sortClause = new SortClause\MapLocationDistance(
            "testtype",
            "maplocation",
            $well["latitude"],
            $well["longitude"],
            Query::SORT_DESC
        );
        $sortClause->setCustomField( 'testtype', 'maplocation', 'custom_geolocation_field' );

        $query = new Query(
            array(
                'filter' => new Criterion\LogicalAnd(
                    array(
                        new Criterion\ContentTypeId( $contentType->id ),
                        new Criterion\MapLocationDistance(
                            "maplocation",
                            Criterion\Operator::GTE,
                            235,
                            $well["latitude"],
                            $well["longitude"]
                        )
                    )
                ),
                'offset' => 0,
                'limit' => 10,
                'sortClauses' => array(
                    $sortClause
                )
            )
        );

        $searchService = $repository->getSearchService();
        $result = $searchService->findContent( $query );

        $this->assertEquals( 3, $result->totalCount );
        $this->assertEquals(
            $wildBoars->id,
            $result->searchHits[2]->valueObject->id
        );
        $this->assertEquals(
            $mushrooms->id,
            $result->searchHits[1]->valueObject->id
        );
        $this->assertEquals(
            $tree->id,
            $result->searchHits[0]->valueObject->id
        );
    }
 /**
  * Test for the findLocations() method.
  *
  * @see \eZ\Publish\API\Repository\SearchService::findLocations()
  * @group maplocation
  */
 public function testMapLocationDistanceWithCustomFieldSort()
 {
     $contentType = $this->createTestPlaceContentType();
     // Create a draft to account for behaviour with ContentType in different states
     $repository = $this->getRepository();
     $contentTypeService = $repository->getContentTypeService();
     $contentService = $repository->getContentService();
     $contentTypeService->createContentTypeDraft($contentType);
     $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
     $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
     $createStruct->alwaysAvailable = false;
     $createStruct->mainLanguageCode = 'eng-GB';
     $createStruct->setField('maplocation', array('latitude' => 45.894877, 'longitude' => 15.972699, 'address' => 'Here be wild boars'), 'eng-GB');
     $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
     $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
     $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
     $createStruct->alwaysAvailable = false;
     $createStruct->mainLanguageCode = 'eng-GB';
     $createStruct->setField('maplocation', array('latitude' => 45.927334, 'longitude' => 15.934847, 'address' => 'A lone tree'), 'eng-GB');
     $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
     $tree = $contentService->publishVersion($draft->getVersionInfo());
     $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
     $createStruct->alwaysAvailable = false;
     $createStruct->mainLanguageCode = 'eng-GB';
     $createStruct->setField('maplocation', array('latitude' => 45.903777, 'longitude' => 15.958788, 'address' => 'Meadow with mushrooms'), 'eng-GB');
     $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
     $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
     $this->refreshSearch($repository);
     $well = array('latitude' => 43.756825, 'longitude' => 15.775074);
     $sortClause = new SortClause\MapLocationDistance('testtype', 'maplocation', $well['latitude'], $well['longitude'], LocationQuery::SORT_DESC);
     $sortClause->setCustomField('testtype', 'maplocation', 'custom_geolocation_field');
     $query = new LocationQuery(array('filter' => new Criterion\LogicalAnd(array(new Criterion\ContentTypeId($contentType->id), new Criterion\MapLocationDistance('maplocation', Criterion\Operator::GTE, 235, $well['latitude'], $well['longitude']))), 'offset' => 0, 'limit' => 10, 'sortClauses' => array($sortClause)));
     $searchService = $repository->getSearchService();
     $result = $searchService->findLocations($query);
     $this->assertEquals(3, $result->totalCount);
     $this->assertEquals($wildBoars->contentInfo->mainLocationId, $result->searchHits[2]->valueObject->id);
     $this->assertEquals($mushrooms->contentInfo->mainLocationId, $result->searchHits[1]->valueObject->id);
     $this->assertEquals($tree->contentInfo->mainLocationId, $result->searchHits[0]->valueObject->id);
 }