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 onPermissionUpdate(PermissionUpdateEvent $permissionUpdateEvent) { if ($permissionUpdateEvent->getType() !== SecurityBehavior::class) { return; } $document = $this->documentManager->find($permissionUpdateEvent->getIdentifier()); $this->searchManager->deindex($document); }
/** * {@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(); } }
public function indexDocument($title, $url) { /* @var ContentMapperInterface $mapper */ $document = $this->documentManager->create('page'); $document->setTitle($title); $document->setStructureType('default'); $document->setParent($this->homeDocument); $document->setResourceSegment($url); $this->documentManager->persist($document, 'de'); return $document; }
private function createPage() { $page = new PageDocument(); $page->setTitle('Hello'); $page->setResourceSegment('/hello'); $page->setParent($this->contentDocument); $page->setStructureType('internallinks'); $page->getStructure()->bind(['title' => 'World', 'internalLinks' => [$this->contentDocument->getUuid()]], true); $this->documentManager->persist($page, 'fr'); $this->documentManager->flush(); return $page; }
/** * {@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 indexDocument($title, $url) { /* @var ContentMapperInterface $mapper */ $document = $this->documentManager->create('page'); $document->setTitle($title); $document->setStructureType('default'); $document->setParent($this->homeDocument); $document->setResourceSegment($url); $webspaceReflection = new \ReflectionProperty(PageDocument::class, 'webspaceName'); $webspaceReflection->setAccessible(true); $webspaceReflection->setValue($document, 'sulu_io'); $this->documentManager->persist($document, 'de'); return $document; }
/** * 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; }
/** * 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); } }
private function loadFixtures() { // HOTELS $this->hotel1 = $this->documentManager->create('snippet'); $this->hotel1->setStructureType('hotel'); $this->hotel1->setTitle('Le grande budapest'); $this->documentManager->persist($this->hotel1, 'de'); $this->hotel2 = $this->documentManager->create('snippet'); $this->hotel2->setStructureType('hotel'); $this->hotel2->setTitle('L\'Hôtel New Hampshire'); $this->documentManager->persist($this->hotel2, 'de'); // CARS $car = $this->documentManager->create('snippet'); $car->setStructureType('car'); $car->setTitle('Skoda'); $this->documentManager->persist($car, 'de'); $car = $this->documentManager->create('snippet'); $car->setStructureType('car'); $car->setTitle('Volvo'); $this->documentManager->persist($car, 'de'); $car = $this->documentManager->create('snippet'); $car->setStructureType('car'); $car->setTitle('Ford'); $this->documentManager->persist($car, 'de'); $car = $this->documentManager->create('snippet'); $car->setStructureType('car'); $car->setTitle('VW'); $this->documentManager->persist($car, 'en'); $this->documentManager->flush(); }
/** * Removes related route of given document. * * @param StructureBehavior $document */ private function removeRoute(StructureBehavior $document) { foreach ($this->documentInspector->getReferrers($document) as $referrer) { if ($referrer instanceof RouteBehavior) { $this->documentManager->remove($referrer); } } }
/** * {@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(); }
/** * @param PersistEvent $event */ public function handleChangeParent(PersistEvent $event) { $document = $event->getDocument(); $node = $this->inspector->getNode($document); $parentNode = $event->getParentNode(); if ($parentNode->getPath() === $node->getParent()->getPath()) { return; } $this->documentManager->move($document, $parentNode->getPath()); }
public function testIndexNoMedia() { $document = $this->documentManager->create('page'); $document->setStructureType('images'); $document->setTitle('Hallo'); $document->setResourceSegment('/hallo'); $document->setParent($this->webspaceDocument); $this->documentManager->persist($document, 'de'); $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; }
/** * It can deserialize persisted documents with routes. */ public function testDeserializationPersisted() { $page = $this->createPage(['title' => 'Hello']); $this->manager->persist($page, 'de'); $this->manager->flush(); $result = $this->serializer->serialize($page, 'json'); $page = $this->serializer->deserialize($result, PageDocument::class, 'json'); $this->assertInstanceOf(PageDocument::class, $page); $this->assertEquals('Hello', $page->getStructure()->getProperty('title')->getValue()); $this->assertEquals('de', $this->registry->getOriginalLocaleForDocument($page)); }
/** * Removes the routes for the given document. * * @param RemoveEvent $event */ public function handleRemove(RemoveEvent $event) { $document = $event->getDocument(); if (!$document instanceof CustomUrlBehavior) { return; } foreach ($this->inspector->getReferrers($document) as $referrer) { if ($referrer instanceof RouteBehavior) { $this->documentManager->remove($referrer); } } }
/** * 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); }
/** * @param $template * @param $data * @param $parent * * @return PageDocument */ private function savePage($template, $data, $parent, $tags = []) { $data = array_merge(['structureType' => $template, 'parent' => $parent, 'workflowStage' => 2, 'webspace_key' => 'sulu_io'], $data); $document = $this->documentManager->create('page'); $options = ['csrf_protection' => false, 'webspace_key' => 'sulu_io']; $form = $this->formFactory->create('page', $document, $options); $clearMissing = false; $form->submit($data, $clearMissing); $this->documentManager->persist($document, 'en', ['user' => 1]); $this->documentManager->flush(); $node = $this->inspector->getNode($document); $node->setProperty('i18n:en-excerpt-tags', $tags); $node->getSession()->save(); return $document; }
/** * Changes the old route to a history route and redirect to the new route. * * @param RouteBehavior $oldDocument * @param RouteBehavior $newDocument */ private function changeOldPathToHistoryRoutes(RouteBehavior $oldDocument, RouteBehavior $newDocument) { $oldDocument->setTargetDocument($newDocument); $oldDocument->setHistory(true); $oldRouteNode = $this->documentInspector->getNode($oldDocument); $oldRouteNode->setProperty(self::NODE_HISTORY_FIELD, true); foreach ($this->documentInspector->getReferrers($oldDocument) as $referrer) { if ($referrer instanceof RouteBehavior) { $referrer->setTargetDocument($newDocument); $referrer->setHistory(true); $this->documentManager->persist($referrer, null, ['path' => $this->documentInspector->getPath($referrer)]); $this->documentManager->publish($referrer, null); } } }
public function testGetExternalLink() { $client = $this->createAuthenticatedClient(); $externalLinkPage = $this->documentManager->create('page'); $externalLinkPage->setTitle('page'); $externalLinkPage->setStructureType('external-link'); $externalLinkPage->setRedirectType(RedirectType::EXTERNAL); $externalLinkPage->setRedirectExternal('http://www.sulu.io'); $externalLinkPage->setResourceSegment('/test'); $this->documentManager->persist($externalLinkPage, 'en', ['parent_path' => '/cmf/sulu_io/contents']); $this->documentManager->flush(); $client->request('GET', '/api/nodes/' . $externalLinkPage->getUuid() . '?webspace=sulu_io&language=en'); $response = json_decode($client->getResponse()->getContent(), true); $this->assertEquals('external', $response['linked']); $this->assertEquals('http://www.sulu.io', $response['external']); }
/** * @param string $title * @param string $locale * @param string $link * * @return PageDocument */ private function createExternalLinkPage($title, $locale, $link) { $data['title'] = $title; $data['url'] = '/' . $title; /** @var PageDocument $document */ $document = $this->documentManager->create('page'); $document->setStructureType('simple'); $document->setTitle($title); $document->setResourceSegment($data['url']); $document->setLocale($locale); $document->setRedirectType(RedirectType::EXTERNAL); $document->setRedirectExternal($link); $document->getStructure()->bind($data); $this->documentManager->persist($document, $locale, ['path' => $this->sessionManager->getContentPath('sulu_io') . '/' . $title, 'auto_create' => true]); $this->documentManager->flush(); return $document; }
/** * 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); }
public function testGetWithPermissions() { // create secured page $securedPage = $this->documentManager->create('page'); $securedPage->setTitle('secured'); $securedPage->setResourceSegment('/secured'); $securedPage->setStructureType('default'); $this->documentManager->persist($securedPage, 'en', ['parent_path' => '/cmf/sulu_io/contents']); $this->documentManager->flush(); $this->documentManager->clear(); $client = $this->createAuthenticatedClient(); $client->request('GET', '/api/nodes?uuid=' . $securedPage->getUuid() . '&tree=true&webspace=sulu_io&language=en'); $response = json_decode($client->getResponse()->getContent(), true); $this->assertArrayHasKey('_permissions', $response['_embedded']['nodes'][0]['_embedded'][0]); $client->request('GET', '/api/nodes/' . $securedPage->getUuid() . '?language=en&webspace=sulu_io'); $response = json_decode($client->getResponse()->getContent(), true); $this->assertArrayHasKey('_permissions', $response); }
/** * 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); }