public function testOnPermissionUpdate() { $document = new \stdClass(); $event = new PermissionUpdateEvent(SecurityBehavior::class, '1', null); $this->documentManager->find('1')->willReturn($document); $this->searchManager->deindex($document)->shouldBeCalled(); $this->permissionListener->onPermissionUpdate($event); }
public function setUp() { $this->initPhpcr(); $this->session = $this->getContainer()->get('doctrine_phpcr')->getConnection(); $this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager'); $this->getSearchManager()->purge('page'); $this->homeDocument = $this->documentManager->find('/cmf/sulu_io/contents'); }
public function setUp() { $this->initPhpcr(); $this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager'); $this->contentDocument = $this->documentManager->find('/cmf/sulu_io/contents', 'en'); $this->serializer = $this->getContainer()->get('jms_serializer'); $this->contentMapper = $this->getContainer()->get('sulu.content.mapper'); }
public function onPermissionUpdate(PermissionUpdateEvent $permissionUpdateEvent) { if ($permissionUpdateEvent->getType() !== SecurityBehavior::class) { return; } $document = $this->documentManager->find($permissionUpdateEvent->getIdentifier()); $this->searchManager->deindex($document); }
public function setUp() { $this->manager = $this->getContainer()->get('sulu_document_manager.document_manager'); $this->initPhpcr(); $this->parent = $this->manager->find('/cmf/sulu_io/contents', 'de'); $this->serializer = $this->getContainer()->get('jms_serializer'); $this->registry = $this->getContainer()->get('sulu_document_manager.document_registry'); }
/** * {@inheritdoc} */ public function handle($workload) { /** @var PageDocument $document */ $document = $this->documentManager->find($workload['uuid'], $workload['locale']); $urls = $this->webspaceManager->findUrlsByResourceLocator($document->getResourceSegment(), $this->environment, $workload['locale'], $workload['webspaceKey']); foreach ($urls as $url) { $this->client->get($url)->send(); } }
/** * {@inheritdoc} */ public function translateObject($object, $locale) { $document = $this->documentManager->find($this->inspector->getUuid($object), $locale); if ($document instanceof WorkflowStageBehavior && $this->context === SuluKernel::CONTEXT_ADMIN) { // set the workflowstage to test, so that the document will be indexed in the index for drafting // this change must not be persisted // is required because of the expression for the index name uses the workflowstage $document->setWorkflowStage(WorkflowStage::TEST); } return $document; }
/** * {@inheritdoc} */ public function getPermissions($type, $identifier) { $permissions = []; if (!$identifier) { return $permissions; } try { $document = $this->documentManager->find($identifier, null, ['rehydrate' => false]); } catch (DocumentNotFoundException $e) { return []; } return $document->getPermissions(); }
public function setUp() { $this->initPhpcr(); $this->documentManager = $this->container->get('sulu_document_manager.document_manager'); $this->nodeManager = $this->container->get('sulu_document_manager.node_manager'); $this->webspaceDocument = $this->documentManager->find('/cmf/sulu_io/contents'); $mediaEntity = new Media(); $tagManager = $this->getMock('Sulu\\Bundle\\TagBundle\\Tag\\TagManagerInterface'); $this->media = new ApiMedia($mediaEntity, 'de', null, $tagManager); $this->mediaSelectionContainer = $this->prophesize(MediaSelectionContainer::class); $this->mediaSelectionContainer->getData('de')->willReturn([$this->media]); $this->mediaSelectionContainer->toArray()->willReturn(null); }
/** * Set the document parent to be the webspace content path * when the document has no parent. * * @param FormEvent $event */ public function postSubmitDocumentParent(FormEvent $event) { $document = $event->getData(); if ($document->getParent()) { return; } $form = $event->getForm(); $webspaceKey = $form->getConfig()->getAttribute('webspace_key'); $parent = $this->documentManager->find($this->sessionManager->getContentPath($webspaceKey)); if (null === $parent) { throw new \InvalidArgumentException(sprintf('Could not determine parent for document with title "%s" in webspace "%s"', $document->getTitle(), $webspaceKey)); } $document->setParent($parent); }
/** * {@inheritdoc} */ public function load($webspaceKey, $type, $locale) { $snippetNode = $this->settingsManager->load($webspaceKey, 'snippets-' . $type); if (null === $snippetNode) { return; } $uuid = $snippetNode->getIdentifier(); /** @var SnippetDocument $document */ $document = $this->documentManager->find($uuid, $locale); if (null !== $document && $document->getStructureType() !== $type) { throw new WrongSnippetTypeException($document->getStructureType(), $type, $document); } return $document; }
public function testGetInternalLink() { $client = $this->createAuthenticatedClient(); $targetPage = $this->documentManager->create('page'); $targetPage->setTitle('target'); $targetPage->setResourceSegment('/target'); $targetPage->setStructureType('default'); $this->documentManager->persist($targetPage, 'en', ['parent_path' => '/cmf/sulu_io/contents']); $this->documentManager->flush(); $internalLinkPage = $this->documentManager->create('page'); $internalLinkPage->setTitle('page'); $internalLinkPage->setStructureType('default'); $internalLinkPage->setResourceSegment('/test'); $this->documentManager->persist($internalLinkPage, 'en', ['parent_path' => '/cmf/sulu_io/contents']); $this->documentManager->flush(); $internalLinkPage = $this->documentManager->find($internalLinkPage->getUuid()); $internalLinkPage->setRedirectType(RedirectType::INTERNAL); $internalLinkPage->setRedirectTarget($targetPage); $this->documentManager->persist($internalLinkPage, 'en', ['parent_path' => '/cmf/sulu_io/contents']); $this->documentManager->flush(); $client->request('GET', '/api/nodes/' . $internalLinkPage->getUuid() . '?webspace=sulu_io&language=en'); $response = json_decode($client->getResponse()->getContent(), true); $this->assertEquals('internal', $response['linked']); $this->assertEquals($targetPage->getUuid(), $response['internal_link']); }
/** * Updates the route for the given document after a move or copy. * * @param object $document * @param bool $generateRoutes If set to true a route in the routing tree will also be created */ private function updateRoute($document, $generateRoutes) { $locales = $this->documentInspector->getLocales($document); $webspaceKey = $this->documentInspector->getWebspace($document); $uuid = $this->documentInspector->getUuid($document); $path = $this->documentInspector->getPath($document); $parentUuid = $this->documentInspector->getUuid($this->documentInspector->getParent($document)); $defaultNode = $this->defaultSession->getNode($path); $liveNode = $this->liveSession->getNode($path); $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($webspaceKey); foreach ($locales as $locale) { $localizedDocument = $this->documentManager->find($uuid, $locale); if ($localizedDocument->getRedirectType() !== RedirectType::NONE) { continue; } $resourceSegmentPropertyName = $this->encoder->localizedSystemName($this->getResourceSegmentProperty($localizedDocument)->getName(), $locale); $this->updateResourceSegmentProperty($defaultNode, $resourceSegmentPropertyName, $parentUuid, $webspaceKey, $locale); if ($liveNode->hasProperty($resourceSegmentPropertyName)) { $this->updateResourceSegmentProperty($liveNode, $resourceSegmentPropertyName, $parentUuid, $webspaceKey, $locale); // if the method is called with the generateRoutes flag it will create a new route // this happens on a move, but not on copy, because copy results in a draft page without url if ($generateRoutes) { $localizedDocument->setResourceSegment($liveNode->getPropertyValue($resourceSegmentPropertyName)); $resourceLocatorStrategy->save($localizedDocument, null); $localizedDocument->setResourceSegment($defaultNode->getPropertyValue($resourceSegmentPropertyName)); } } } }
/** * @dataProvider provideRemoveSnippetWithReferencesDereference */ public function testRemoveSnippetWithReferencesDereference($multiple = false) { $document = $this->documentManager->create('page'); $document->setTitle('test'); $document->setResourceSegment('/url/foo'); if ($multiple) { $document->getStructure()->bind(['animals' => [$this->snippet1->getUuid(), $this->snippet2->getUuid()]], false); } else { $document->getStructure()->bind(['animals' => $this->snippet1->getUuid()], false); } $document->setParent($this->parent); $document->setStructureType('test_page'); $this->documentManager->persist($document, 'de'); $this->documentManager->flush(); $this->contentMapper->delete($this->snippet1->getUuid(), 'sulu_io', true); try { $this->session->getNode($this->snippet1OriginalPath); $this->assertTrue(false, 'Snippet was found FAIL'); } catch (\PHPCR\PathNotFoundException $e) { $this->assertTrue(true, 'Snippet was removed'); } $referrer = $this->documentManager->find('/cmf/sulu_io/contents/test', 'de'); if ($multiple) { $contents = $referrer->getStructure()->getProperty('animals')->getValue(); $this->assertCount(1, $contents); $content = reset($contents); $this->assertEquals($this->snippet2->getUuid(), $content); } else { $contents = $referrer->getStructure()->getProperty('animals')->getValue(); $this->assertCount(0, $contents); } }
/** * {@inheritdoc} */ public function deleteByPath($path, $webspaceKey, $languageCode, $segmentKey = null) { if (!is_string($path) || trim($path, '/') == '') { throw new \InvalidArgumentException(sprintf('The path to delete must be a non-empty string, "%s" given.', $path)); } $routeDocument = $this->documentManager->find($this->getPath($path, $webspaceKey, $languageCode, $segmentKey), $languageCode); $this->documentManager->remove($routeDocument); }
/** * Returns Proxy Document for uuid. * * @param string $uuid * @param string $locale * * @return object */ private function getResource($uuid, $locale) { return $this->proxyFactory->createProxy(PageDocument::class, function (&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) use($uuid, $locale) { $initializer = null; $wrappedObject = $this->documentManager->find($uuid, $locale); return true; }); }
private function initializeWebspace(OutputInterface $output, Webspace $webspace) { $homePath = $this->pathBuilder->build(['%base%', $webspace->getKey(), '%content%']); $routesPath = $this->pathBuilder->build(['%base%', $webspace->getKey(), '%route%']); $webspaceLocales = []; foreach ($webspace->getAllLocalizations() as $localization) { $webspaceLocales[] = $localization->getLocale(); } $homeType = $webspace->getDefaultTemplate('home'); $existingLocales = []; $homeDocument = null; if ($this->nodeManager->has($homePath)) { $homeDocument = $this->documentManager->find($homePath, null, ['load_ghost_content' => false, 'auto_create' => true, 'path' => $homePath]); $existingLocales = $this->inspector->getLocales($homeDocument); } foreach ($webspaceLocales as $webspaceLocale) { if (in_array($webspaceLocale, $existingLocales)) { $output->writeln(sprintf(' [ ] <info>homepage</info>: %s (%s)', $homePath, $webspaceLocale)); continue; } $output->writeln(sprintf(' [+] <info>homepage</info>: [%s] %s (%s)', $homeType, $homePath, $webspaceLocale)); $persistOptions = ['ignore_required' => true]; if (!$homeDocument) { $homeDocument = new HomeDocument(); $persistOptions['path'] = $homePath; $persistOptions['auto_create'] = true; } else { $homeDocument = $this->documentManager->find($homePath, $webspaceLocale, ['load_ghost_content' => false]); } $homeDocument->setTitle('Homepage'); $homeDocument->setStructureType($homeType); $this->documentManager->persist($homeDocument, $webspaceLocale, $persistOptions); $this->documentManager->publish($homeDocument, $webspaceLocale); $routePath = $routesPath . '/' . $webspaceLocale; try { $routeDocument = $this->documentManager->find($routePath); } catch (DocumentNotFoundException $e) { $routeDocument = $this->documentManager->create('route'); } $routeDocument->setTargetDocument($homeDocument); $this->documentManager->persist($routeDocument, $webspaceLocale, ['path' => $routePath, 'auto_create' => true]); $this->documentManager->publish($routeDocument, $webspaceLocale); } $this->documentManager->flush(); }
/** * {@inheritdoc} */ public function getPermissions($type, $identifier) { $permissions = []; try { $document = $this->documentManager->find($identifier, null, ['rehydrate' => false]); } catch (DocumentNotFoundException $e) { return $permissions; } $allowedPermissions = $document->getPermissions(); if (is_array($allowedPermissions)) { foreach ($allowedPermissions as $roleId => $rolePermissions) { $permissions[$roleId] = []; foreach ($this->permissions as $permission => $value) { $permissions[$roleId][$permission] = in_array($permission, $rolePermissions); } } } return $permissions; }
/** * @param string $title * @param string $locale * @param string $shadowedLocale * * @return PageDocument */ private function createShadowPage($title, $locale, $shadowedLocale) { $document1 = $this->createPage($title, $locale); $document = $this->documentManager->find($document1->getUuid(), $shadowedLocale, ['load_ghost_content' => false]); $document->setShadowLocaleEnabled(true); $document->setTitle(strrev($title)); $document->setShadowLocale($locale); $document->setLocale($shadowedLocale); $document->setResourceSegment($document1->getResourceSegment()); $this->documentManager->persist($document, $shadowedLocale); $this->documentManager->flush(); return $document; }
/** * Upgrades the node to new URL representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain URL fields * @param bool $addScheme Adds the scheme to URLs if true, removes the scheme otherwise */ private function upgradeNode(NodeInterface $node, $locale, $properties, $addScheme) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { $this->upgradeProperty($document->getStructure()->getProperty($property), $addScheme); } $this->documentManager->persist($document, $locale); }
/** * Find or create route-document for given path. * * @param string $path * @param string $locale * @param CustomUrlBehavior $document * @param string $route * * @return RouteDocument * * @throws ResourceLocatorAlreadyExistsException */ protected function findOrCreateRoute($path, $locale, CustomUrlBehavior $document, $route) { try { /** @var RouteDocument $routeDocument */ $routeDocument = $this->documentManager->find($path, $locale); } catch (DocumentNotFoundException $ex) { return $this->documentManager->create('custom_url_route'); } if (!$routeDocument instanceof RouteDocument || $routeDocument->getTargetDocument()->getUuid() !== $document->getUuid()) { throw new ResourceLocatorAlreadyExistsException($route, $document->getTitle()); } return $routeDocument; }
public function testCopyLocale() { $snippet = $this->documentManager->create('snippet'); $snippet->setStructureType('hotel'); $snippet->setTitle('Hotel de'); $this->documentManager->persist($snippet, 'de'); $this->documentManager->flush(); $this->client->request('POST', '/snippets/' . $snippet->getUuid() . '?action=copy-locale&dest=en&language=de'); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $newPage = $this->documentManager->find($snippet->getUuid(), 'en'); $this->assertEquals('Hotel de', $newPage->getTitle()); $newPage = $this->documentManager->find($snippet->getUuid(), 'de'); $this->assertEquals('Hotel de', $newPage->getTitle()); }
/** * Upgrades the node to new date representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain date fields * @param bool $up */ private function upgradeNode(NodeInterface $node, $locale, array $properties, $up) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { if ($property['property'] instanceof BlockMetadata) { $this->upgradeBlockProperty($property['property'], $property['components'], $node, $locale, $up); } else { $this->upgradeProperty($property['property'], $node, $locale, $up); } } $this->documentManager->persist($document, $locale, ['auto_name' => false]); }
/** * Upgrades the node to new URL representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain URL fields * @param bool $addScheme Adds the scheme to URLs if true, removes the scheme otherwise */ private function upgradeNode(NodeInterface $node, $locale, array $properties, $addScheme) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { $propertyValue = $document->getStructure()->getProperty($property['property']->getName()); if ($property['property'] instanceof BlockMetadata) { $this->upgradeBlockProperty($property['property'], $property['components'], $propertyValue, $addScheme); } else { $this->upgradeProperty($propertyValue, $addScheme); } } $this->documentManager->persist($document, $locale); }
/** * Bind data array to given document. * * TODO this logic have to be extracted in a proper way. * * @param CustomUrlDocument $document * @param array $data * @param string $locale */ private function bind(CustomUrlDocument $document, $data, $locale) { $document->setTitle($data['title']); unset($data['title']); $metadata = $this->metadataFactory->getMetadataForAlias('custom_url'); $accessor = PropertyAccess::createPropertyAccessor(); foreach ($metadata->getFieldMappings() as $fieldName => $mapping) { if (!array_key_exists($fieldName, $data)) { continue; } $value = $data[$fieldName]; if (array_key_exists('type', $mapping) && $mapping['type'] === 'reference') { $value = $this->documentManager->find($value['uuid'], $locale, ['load_ghost_content' => true]); } $accessor->setValue($document, $fieldName, $value); } $document->setLocale($locale); }
/** * {@inheritdoc} */ public function generate($title, $parentUuid, $webspaceKey, $languageCode, $segmentKey = null) { // title should not have a slash $title = str_replace('/', '-', $title); if ($parentUuid !== null) { $parentDocument = $this->documentManager->find($parentUuid, $languageCode, ['load_ghost_content' => false]); // find uuid of published ancestor for generating parent-path-segment $resolvedParentUuid = $this->documentInspector->getUuid($this->getPublishedAncestorOrSelf($parentDocument)); // using loadByContentUuid because loadByContent returns the wrong language for shadow-pages $parentPath = $this->loadByContentUuid($resolvedParentUuid, $webspaceKey, $languageCode); } else { $parentPath = '/'; } // get generated path from childClass $path = $this->resourceLocatorGenerator->generate($title, $parentPath); // cleanup path $path = $this->cleaner->cleanup($path, $languageCode); // get unique path $path = $this->mapper->getUniquePath($path, $webspaceKey, $languageCode, $segmentKey); return $path; }
/** * {@inheritdoc} */ public function getObject($id, $locale) { return $this->documentManager->find($id, $locale); }
public function testGetPermissionsForNotExistingDocument() { $this->documentManager->find('1', null, ['rehydrate' => false])->willThrow(DocumentNotFoundException::class); $this->assertEquals([], $this->phpcrAccessControlProvider->getPermissions('Acme\\Document', '1')); }
/** * Finds the correct route for the current request. * It loads the correct data with the content mapper. * * @param Request $request A request against which to match * * @return \Symfony\Component\Routing\RouteCollection with all Routes that * could potentially match $request. Empty collection if nothing can * match */ public function getRouteCollectionForRequest(Request $request) { $collection = new RouteCollection(); // no portal information without localization supported if ($this->requestAnalyzer->getCurrentLocalization() === null && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_PARTIAL && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_REDIRECT) { return $collection; } $htmlExtension = '.html'; $resourceLocator = $this->requestAnalyzer->getResourceLocator(); if ($this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_REDIRECT || $this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_PARTIAL) { // redirect webspace correctly with locale $collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute()); } elseif ($request->getRequestFormat() === 'html' && substr($request->getPathInfo(), -strlen($htmlExtension)) === $htmlExtension) { // redirect *.html to * (without url) $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->getUrlWithoutEndingTrailingSlash($resourceLocator))); } else { // just show the page $portal = $this->requestAnalyzer->getPortal(); $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization(); $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($portal->getWebspace()->getKey()); try { // load content by url ignore ending trailing slash /** @var PageDocument $document */ $document = $this->documentManager->find($resourceLocatorStrategy->loadByResourceLocator(rtrim($resourceLocator, '/'), $portal->getWebspace()->getKey(), $locale), $locale, ['load_ghost_content' => false]); if (!$document->getTitle()) { // If the title is empty the document does not exist in this locale // Necessary because of https://github.com/sulu/sulu/issues/2724, otherwise locale could be checked return $collection; } if (preg_match('/\\/$/', $resourceLocator) && $this->requestAnalyzer->getResourceLocatorPrefix()) { // redirect page to page without slash at the end $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . rtrim($resourceLocator, '/'))); } elseif ($document->getRedirectType() === RedirectType::INTERNAL) { // redirect internal link $redirectUrl = $this->requestAnalyzer->getResourceLocatorPrefix() . $document->getRedirectTarget()->getResourceSegment(); if ($request->getQueryString()) { $redirectUrl .= '?' . $request->getQueryString(); } $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $redirectUrl)); } elseif ($document->getRedirectType() === RedirectType::EXTERNAL) { $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $document->getRedirectExternal())); } elseif (!$this->checkResourceLocator()) { return $collection; } else { // convert the page to a StructureBridge because of BC /** @var PageBridge $structure */ $structure = $this->structureManager->wrapStructure($this->documentInspector->getMetadata($document)->getAlias(), $this->documentInspector->getStructureMetadata($document)); $structure->setDocument($document); // show the page $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getStructureRoute($request, $structure)); } } catch (ResourceLocatorNotFoundException $exc) { // just do not add any routes to the collection } catch (ResourceLocatorMovedException $exc) { // old url resource was moved $collection->add($exc->getNewResourceLocatorUuid() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $exc->getNewResourceLocator())); } catch (RepositoryException $exc) { // just do not add any routes to the collection } } return $collection; }