/**
  * @throws NotImplementedException If Content is missing location as this is not supported in current version
  */
 public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null)
 {
     $this->previewHelper->setPreviewActive(true);
     try {
         $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
         $location = $this->locationProvider->loadMainLocation($contentId);
         if (!$location instanceof Location) {
             throw new NotImplementedException("Preview for content without locations");
         }
         $this->previewHelper->setPreviewedContent($content);
         $this->previewHelper->setPreviewedLocation($location);
     } catch (UnauthorizedException $e) {
         throw new AccessDeniedException();
     }
     if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
         throw new AccessDeniedException();
     }
     $siteAccess = $this->previewHelper->getOriginalSiteAccess();
     // Only switch if $siteAccessName is set and different from original
     if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) {
         $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
     }
     $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request), HttpKernelInterface::SUB_REQUEST);
     $response->headers->remove('cache-control');
     $response->headers->remove('expires');
     $this->previewHelper->restoreConfigScope();
     $this->previewHelper->setPreviewActive(false);
     return $response;
 }
 public function testGetPreviewLocationNoLocation()
 {
     $contentId = 123;
     $contentInfo = $this->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo')->setConstructorArgs(array(array('id' => $contentId)))->getMockForAbstractClass();
     $this->contentService->expects($this->once())->method('loadContentInfo')->with($contentId)->will($this->returnValue($contentInfo));
     $this->locationHandler->expects($this->once())->method('loadParentLocationsForDraftContent')->with($contentId)->will($this->returnValue(array()));
     $this->locationHandler->expects($this->never())->method('loadLocation');
     $this->assertNull($this->provider->loadMainLocation($contentId));
 }
 public function testPreviewDefaultSiteaccess()
 {
     $contentId = 123;
     $lang = 'eng-GB';
     $versionNo = 3;
     $locationId = 456;
     $content = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
     $location = $this->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Location')->setConstructorArgs(array(array('id' => $locationId)))->getMockForAbstractClass();
     // Repository expectations
     $this->locationProvider->expects($this->once())->method('loadMainLocation')->with($contentId)->will($this->returnValue($location));
     $this->contentService->expects($this->once())->method('loadContent')->with($contentId, array($lang), $versionNo)->will($this->returnValue($content));
     $this->authorizationChecker->expects($this->once())->method('isGranted')->with($this->equalTo(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content))))->will($this->returnValue(true));
     $previousSiteAccessName = 'foo';
     $previousSiteAccess = new SiteAccess($previousSiteAccessName);
     $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request', array('duplicate'));
     $this->previewHelper->expects($this->once())->method('getOriginalSiteAccess')->will($this->returnValue($previousSiteAccess));
     $this->previewHelper->expects($this->once())->method('restoreConfigScope');
     // Request expectations
     $duplicatedRequest = $this->getDuplicatedRequest($location, $content, $previousSiteAccess);
     $request->expects($this->once())->method('duplicate')->will($this->returnValue($duplicatedRequest));
     // Kernel expectations
     $expectedResponse = new Response();
     $this->httpKernel->expects($this->once())->method('handle')->with($duplicatedRequest, HttpKernelInterface::SUB_REQUEST)->will($this->returnValue($expectedResponse));
     $controller = $this->getPreviewController();
     $this->assertSame($expectedResponse, $controller->previewContentAction($request, $contentId, $versionNo, $lang));
 }