findContent() public method

Finds content objects for the given query.
public findContent ( eZ\Publish\API\Repository\Values\Content\Query $query, array $languageFilter = [], boolean $filterOnUserPermissions = true ) : eZ\Publish\API\Repository\Values\Content\Search\SearchResult
$query eZ\Publish\API\Repository\Values\Content\Query
$languageFilter array Configuration for specifying prioritized languages query will be performed on. Currently supports: array("languages" => array(,..), "useAlwaysAvailable" => bool) useAlwaysAvailable defaults to true to avoid exceptions on missing translations.
$filterOnUserPermissions boolean if true only the objects which the user is allowed to read are returned.
return eZ\Publish\API\Repository\Values\Content\Search\SearchResult
 /**
  * Prepares content for ContentDataValue class.
  *
  * @param string $contentIdList
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return \EzSystems\RecommendationBundle\Rest\Values\ContentData
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content, version with the given id and languages or content type does not exist
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
  */
 public function getContent($contentIdList, Request $request)
 {
     $contentIds = explode(',', $contentIdList);
     $lang = $request->get('lang');
     $criteria = array(new Criterion\ContentId($contentIds));
     if (!$request->get('hidden')) {
         $criteria[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
     }
     if ($lang) {
         $criteria[] = new Criterion\LanguageCode($lang);
     }
     $query = new Query();
     $query->query = new Criterion\LogicalAnd($criteria);
     $contentItems = $this->searchService->findContent($query)->searchHits;
     $data = $this->prepareContent($contentItems, $request);
     return new ContentDataValue($data);
 }
 /**
  * Prepares content for ContentDataValue class.
  *
  * @param string $contentIdList
  * @param string $responseType
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return \EzSystems\RecommendationBundle\Rest\Values\ContentData
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content, version with the given id and languages or content type does not exist
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
  * @throws \EzSystems\RecommendationBundle\Rest\Exception\UnsupportedResponseTypeException
  */
 public function getContent($contentIdList, $responseType, Request $request)
 {
     if ($responseType != 'http') {
         throw new UnsupportedResponseTypeException('Only http response is available for single content');
     }
     $contentIds = explode(',', $contentIdList);
     $lang = $request->get('lang');
     $criteria = array(new Criterion\ContentId($contentIds));
     if (!$request->get('hidden')) {
         $criteria[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
     }
     if ($lang) {
         $criteria[] = new Criterion\LanguageCode($lang);
     }
     $query = new Query();
     $query->query = new Criterion\LogicalAnd($criteria);
     $contentItems = $this->searchService->findContent($query)->searchHits;
     $content = $this->prepareContent($contentItems, $request);
     return new ContentDataValue($content, ['responseType' => 'http', 'documentRoot' => $request->server->get('DOCUMENT_ROOT'), 'host' => $request->getSchemeAndHttpHost(), 'customerId' => $this->customerId]);
 }
    /**
     * Test for the findContent() method.
     */
    public function testFindContentWithDefaultQueryValues()
    {
        $repositoryMock = $this->getRepositoryMock();
        /** @var \eZ\Publish\SPI\Search\Content\Handler $searchHandlerMock */
        $searchHandlerMock = $this->getSPIMockHandler( 'Search\\Content\\Handler' );
        /** @var \eZ\Publish\SPI\Search\Content\Location\Handler $locationSearchHandlerMock */
        $locationSearchHandlerMock = $this->getSPIMockHandler( 'Search\\Content\\Location\\Handler' );
        $domainMapperMock = $this->getDomainMapperMock();
        $service = new SearchService(
            $repositoryMock,
            $searchHandlerMock,
            $locationSearchHandlerMock,
            $domainMapperMock,
            $this->getPermissionsCriterionHandlerMock(),
            array()
        );

        $repositoryMock
            ->expects( $this->once() )
            ->method( "getContentService" )
            ->will(
                $this->returnValue(
                    $contentServiceMock = $this
                        ->getMockBuilder( "eZ\\Publish\\Core\\Repository\\ContentService" )
                        ->disableOriginalConstructor()
                        ->getMock()
                )
            );

        $fieldFilters = array();
        $spiContentInfo = new SPIContentInfo;
        $contentMock = $this->getMockForAbstractClass( "eZ\\Publish\\API\\Repository\\Values\\Content\\Content" );
        $domainMapperMock->expects( $this->never() )
            ->method( $this->anything() );

        $contentServiceMock
            ->expects( $this->once() )
            ->method( "internalLoadContent" )
            ->will( $this->returnValue( $contentMock ) );

        /** @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
        $searchHandlerMock
            ->expects( $this->once() )
            ->method( "findContent" )
            ->with(
                new Query(
                    array(
                        "filter" => new Criterion\MatchAll(),
                        "limit" => 1073741824
                    )
                ),
                array()
            )
            ->will(
                $this->returnValue(
                    new SearchResult(
                        array(
                            "searchHits" => array( new SearchHit( array( "valueObject" => $spiContentInfo ) ) ),
                            "totalCount" => 1
                        )
                    )
                )
            );

        $result = $service->findContent( new Query(), $fieldFilters, false );

        $this->assertEquals(
            new SearchResult(
                array(
                    "searchHits" => array( new SearchHit( array( "valueObject" => $contentMock ) ) ),
                    "totalCount" => 1
                )
            ),
            $result
        );
    }
Beispiel #4
0
 /**
  * Test for the findContent() method.
  */
 public function testFindContentWithDefaultQueryValues()
 {
     $repositoryMock = $this->getRepositoryMock();
     /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
     $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
     $domainMapperMock = $this->getDomainMapperMock();
     $service = new SearchService($repositoryMock, $searchHandlerMock, $domainMapperMock, $this->getPermissionsCriterionHandlerMock(), array());
     $repositoryMock->expects($this->once())->method('getContentService')->will($this->returnValue($contentServiceMock = $this->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')->disableOriginalConstructor()->getMock()));
     $languageFilter = array();
     $spiContentInfo = new SPIContentInfo();
     $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
     $domainMapperMock->expects($this->never())->method($this->anything());
     $contentServiceMock->expects($this->once())->method('internalLoadContent')->will($this->returnValue($contentMock));
     /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
     $searchHandlerMock->expects($this->once())->method('findContent')->with(new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25)), array())->will($this->returnValue(new SearchResult(array('searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))), 'totalCount' => 1))));
     $result = $service->findContent(new Query(), $languageFilter, false);
     $this->assertEquals(new SearchResult(array('searchHits' => array(new SearchHit(array('valueObject' => $contentMock))), 'totalCount' => 1)), $result);
 }