public function testResolve() { $webspace = new Webspace(); $webspace->setKey('sulu_io'); $portal = new Portal(); $locale = new Localization(); $locale->setLanguage('de'); $locale->setDefault(true); $portal->addLocalization($locale); $localization = new Localization(); $localization->setLanguage('de'); $localization->setCountry('at'); $requestAnalyzer = $this->prophesize('Sulu\\Component\\Webspace\\Analyzer\\WebsiteRequestAnalyzer'); $requestAnalyzer->getWebspace()->willReturn($webspace); $requestAnalyzer->getCurrentLocalization()->willReturn($localization); $requestAnalyzer->getPortalUrl()->willReturn('sulu.io/de'); $requestAnalyzer->getResourceLocatorPrefix()->willReturn('/de'); $requestAnalyzer->getResourceLocator()->willReturn('/search'); $requestAnalyzer->getGetParameters()->willReturn(['p' => 1]); $requestAnalyzer->getPostParameters()->willReturn([]); $requestAnalyzer->getPortal()->willReturn($portal); $requestAnalyzer->getAnalyticsKey()->willReturn('analyticsKey'); $result = $this->resolver->resolve($requestAnalyzer->reveal()); $this->assertEquals(['request' => ['webspaceKey' => 'sulu_io', 'locale' => 'de_at', 'defaultLocale' => 'de', 'portalUrl' => 'sulu.io/de', 'resourceLocatorPrefix' => '/de', 'resourceLocator' => '/search', 'get' => ['p' => 1], 'post' => [], 'analyticsKey' => 'analyticsKey']], $result); }
private function getLocalization($language, $country = '', $shadow = null) { $locale = new Localization(); $locale->setLanguage($language); $locale->setCountry($country); $locale->setShadow($shadow); return $locale; }
/** * Test get alternate links. */ public function testGetAlternateLinksDifferentDefaultLocale() { $locale = new Localization(); $locale->setLanguage('de'); $this->portal->setXDefaultLocalization($locale); $extension = new MetaTwigExtension($this->requestAnalyzer->reveal(), $this->contentPath->reveal()); $urls = $extension->getAlternateLinks(['de' => '/test', 'en' => '/test-en', 'en-us' => '/test-en-us', 'fr' => '/test-fr']); $this->assertEquals(['<link rel="alternate" href="/de/test" hreflang="x-default" />', '<link rel="alternate" href="/de/test" hreflang="de" />', '<link rel="alternate" href="/en/test-en" hreflang="en" />', '<link rel="alternate" href="/en/test-en-us" hreflang="en-us" />', '<link rel="alternate" href="/fr/test-fr" hreflang="fr" />'], explode(PHP_EOL, $urls)); }
/** * Localize given domain. * * @param string $domain * @param Localization $locale * * @return string */ protected function localizeDomain($domain, Localization $locale) { if (!$this->urlReplacer->hasLocalizationReplacer($domain) && !$this->urlReplacer->hasLanguageReplacer($domain)) { $domain = $this->urlReplacer->appendLocalizationReplacer($domain); } $domain = $this->urlReplacer->replaceLanguage($domain, $locale->getLanguage()); $domain = $this->urlReplacer->replaceCountry($domain, $locale->getCountry()); $domain = $this->urlReplacer->replaceLocalization($domain, $locale->getLocalization()); return $this->urlReplacer->cleanup($domain); }
/** * Converts locale string to localization object. * * @param string $locale E.g. de_at or de. * * @return Localization */ private function parse($locale) { $parts = explode('_', $locale); $localization = new Localization(); $localization->setLanguage($parts[0]); if (sizeof($parts) > 1) { $localization->setCountry($parts[1]); } return $localization; }
public function testGetAllLocalizationsWithSameLocalizations() { $localization1 = new Localization(); $localization1->setLanguage('de'); $localization2 = new Localization(); $localization2->setLanguage('en'); $this->addLocalizationProvider([$localization1, $localization2]); $this->addLocalizationProvider([$localization2]); $localizations = $this->localizationManager->getLocalizations(); $this->assertCount(2, $localizations); $this->assertContains($localization1, $localizations); $this->assertContains($localization2, $localizations); }
/** * Finds the next available child-localization in which the node has a translation. * * @param string[] $availableLocales * @param Localization $localization The localization to start the search for * * @return null|Localization */ private function findAvailableChildLocalization(array $availableLocales, Localization $localization) { $childrenLocalizations = $localization->getChildren(); if (!empty($childrenLocalizations)) { foreach ($childrenLocalizations as $childrenLocalization) { // return the localization if a translation exists in the child localization if (in_array($childrenLocalization->getLocale(), $availableLocales)) { return $childrenLocalization; } // recursively call this function for checking children return $this->findAvailableChildLocalization($availableLocales, $childrenLocalization); } } // return null if nothing was found return; }
private function upgradeNode(StructureBridge $page, Webspace $webspace, Localization $localization, OutputInterface $output, $depth = 0) { /** @var SessionManagerInterface $sessionManager */ $sessionManager = $this->getContainer()->get('sulu.phpcr.session'); $session = $sessionManager->getSession(); $node = $session->getNodeByIdentifier($page->getUuid()); /** @var RlpStrategyInterface $strategy */ $strategy = $this->getContainer()->get('sulu.content.rlp.strategy.tree'); /** @var ResourceLocator $resourceLocator */ $resourceLocator = $this->getContainer()->get('sulu.content.type.resource_locator'); if (!$page->hasTag('sulu.rlp')) { return; } $property = $page->getPropertyByTagName('sulu.rlp'); if ($property->getContentTypeName() !== 'resource_locator' && $page->getNodeType() !== Structure::NODE_TYPE_CONTENT) { return; } $transProperty = new TranslatedProperty($property, $localization->getLocalization(), $this->getContainer()->getParameter('sulu.content.language.namespace')); try { // load value $rl = $strategy->loadByContent($node, $webspace->getKey(), $localization->getLocalization()); // save value $property->setValue($rl); $resourceLocator->write($node, $transProperty, 1, $webspace->getKey(), $localization->getLocalization()); $session->save(); $prefix = ' '; for ($i = 0; $i < $depth; ++$i) { $prefix .= '-'; } $output->writeln($prefix . '> "' . $page->getPropertyValue('title') . '": ' . $rl); } catch (ResourceLocatorNotFoundException $ex) { } }
/** * Returns the localization for this PortalInformation. * * @return string */ public function getLocale() { if (null === $this->localization) { return; } return $this->localization->getLocalization(); }
protected function setUp() { parent::setUp(); $this->requestAnalyzer = $this->prophesize(RequestAnalyzerInterface::class); $webspace = new Webspace(); $webspace->setKey('sulu_test'); $locale = new Localization(); $locale->setLanguage('en'); $portal = new Portal(); $portal->setDefaultLocalization($locale); $this->requestAnalyzer->getWebspace()->willReturn($webspace); $this->requestAnalyzer->getPortal()->willReturn($portal); $this->requestAnalyzer->getCurrentLocalization()->willReturn($locale); $this->contentPath = $this->prophesize(ContentPathInterface::class); $this->contentPath->getContentPath('/test', 'sulu_test', 'de')->willReturn('/de/test'); $this->contentPath->getContentPath('/test-en', 'sulu_test', 'en')->willReturn('/en/test-en'); $this->contentPath->getContentPath('/test-en-us', 'sulu_test', 'en-us')->willReturn('/en/test-en-us'); $this->contentPath->getContentPath('/test-en-us', 'sulu_test', 'en_us')->willReturn('/en/test-en-us'); $this->contentPath->getContentPath('/test-fr', 'sulu_test', 'fr')->willReturn('/fr/test-fr'); }
protected function prepareWebspaceManager() { if ($this->webspaceManager !== null) { return; } $this->webspace = new Webspace(); $this->webspace->setKey('sulu_io'); $local1 = new Localization(); $local1->setLanguage('en'); $local2 = new Localization(); $local2->setLanguage('en'); $local2->setCountry('us'); $this->webspace->setLocalizations([$local1, $local2]); $this->webspace->setName('Default'); $theme = new Theme(); $theme->setKey('test'); $theme->addDefaultTemplate('page', 'default'); $this->webspace->setTheme($theme); $this->webspace->setNavigation(new Navigation([new NavigationContext('main', []), new NavigationContext('footer', [])])); $this->webspaceManager = $this->getMock('Sulu\\Component\\Webspace\\Manager\\WebspaceManagerInterface'); $this->webspaceManager->expects($this->any())->method('findWebspaceByKey')->will($this->returnValue($this->webspace)); }
/** * It should return any localizations if neither parent nor children. */ public function testWebspaceAnyLocalization() { $this->inspector->getWebspace($this->document->reveal())->willReturn(self::FIX_WEBSPACE); $this->inspector->getLocales($this->document->reveal())->willReturn(['de']); $this->webspace->getLocalization(self::FIX_LOCALE)->willReturn($this->localization1->reveal()); $this->localization1->getLocalization()->willReturn('en'); $this->localization2->getLocalization()->willReturn('de'); $this->hydrateEvent->getOption('load_ghost_content', true)->willReturn(true); $this->localization1->getParent()->willReturn(null); $this->localization1->getChildren()->willReturn([]); $this->webspace->getLocalizations()->willReturn([$this->localization2->reveal()]); $this->registry->updateLocale($this->document->reveal(), 'de', 'en')->shouldBeCalled(); $this->hydrateEvent->setLocale('de')->shouldBeCalled(); $this->subscriber->handleHydrate($this->hydrateEvent->reveal()); }
/** * Create route-document for given domain. * * @param string $domain * @param CustomUrlBehavior $document * @param Localization $locale * @param string $persistedLocale * @param string $routesPath * * @return RouteDocument * * @throws ResourceLocatorAlreadyExistsException */ protected function createRoute($domain, CustomUrlBehavior $document, Localization $locale, $persistedLocale, $routesPath) { $path = sprintf('%s/%s', $routesPath, $domain); $routeDocument = $this->findOrCreateRoute($path, $persistedLocale, $document, $domain); $routeDocument->setTargetDocument($document); $routeDocument->setLocale($locale->getLocalization()); $routeDocument->setHistory(false); $this->documentManager->persist($routeDocument, $persistedLocale, ['path' => $path, 'auto_create' => true]); $this->documentManager->publish($routeDocument, $persistedLocale); return $routeDocument; }
/** * Adds a localization to the webspace. * * @param Localization $localization */ public function addLocalization(Localization $localization) { $this->localizations[] = $localization; if ($localization->isDefault()) { $this->setDefaultLocalization($localization); } if ($localization->isXDefault()) { $this->xDefaultLocalization = $localization; } }
public function setUp() { $webspaces = []; $portals = []; $portalInformations = ['prod' => [], 'dev' => []]; $this->webspaceCollection = new WebspaceCollection(); // first portal $portal = new Portal(); $portal->setName('Portal1'); $portal->setKey('portal1'); $theme = new Theme(); $theme->setKey('portal1theme'); $environment = new Environment(); $url = new Url(); $url->setUrl('www.portal1.com'); $url->setLanguage('en'); $url->setCountry('us'); $environment->addUrl($url); $environment->setType('prod'); $url = new Url(); $url->setUrl('portal1.com'); $url->setRedirect('www.portal1.com'); $environment->addUrl($url); $portal->addEnvironment($environment); $localizationEnUs = new Localization(); $localizationEnUs->setCountry('us'); $localizationEnUs->setLanguage('en'); $localizationEnUs->setShadow('auto'); $localizationEnUs->setDefault(true); $localizationEnCa = new Localization(); $localizationEnCa->setCountry('ca'); $localizationEnCa->setLanguage('en'); $localizationEnCa->setDefault(false); $localizationEnUs->addChild($localizationEnCa); $localizationFrCa = new Localization(); $localizationFrCa->setCountry('ca'); $localizationFrCa->setLanguage('fr'); $localizationFrCa->setDefault(false); $portal->addLocalization($localizationEnUs); $portal->addLocalization($localizationEnCa); $portal->addLocalization($localizationFrCa); $portal->setDefaultLocalization($localizationEnUs); $portal->setResourceLocatorStrategy('tree'); $webspace = new Webspace(); $webspace->addLocalization($localizationEnUs); $webspace->addLocalization($localizationFrCa); $segmentSummer = new Segment(); $segmentSummer->setName('Summer'); $segmentSummer->setKey('s'); $segmentSummer->setDefault(true); $segmentWinter = new Segment(); $segmentWinter->setName('Winter'); $segmentWinter->setKey('w'); $segmentWinter->setDefault(false); $webspace->addSegment($segmentSummer); $webspace->addSegment($segmentWinter); $webspace->setTheme($theme); $webspace->addPortal($portal); $webspace->setKey('default'); $webspace->setName('Default'); $webspace->addPortal($portal); $webspace->setNavigation(new Navigation([new NavigationContext('main', [])])); $portals[] = $portal; $webspaces[] = $webspace; $portalInformations['prod']['www.portal1.com'] = new PortalInformation(RequestAnalyzerInterface::MATCH_TYPE_FULL, $webspace, $portal, $localizationEnUs, 'www.portal1.com', $segmentSummer); $portalInformations['dev']['portal1.lo'] = new PortalInformation(RequestAnalyzerInterface::MATCH_TYPE_FULL, $webspace, $portal, $localizationEnUs, 'portal1.lo', $segmentSummer); $this->webspaceCollection->setWebspaces($webspaces); $this->webspaceCollection->setPortals($portals); $this->webspaceCollection->setPortalInformations($portalInformations); }
protected function setUp() { parent::setUp(); $this->contentMapper = $this->prophesize('Sulu\\Component\\Content\\Mapper\\ContentMapperInterface'); $this->requestAnalyzer = $this->prophesize('Sulu\\Component\\Webspace\\Analyzer\\RequestAnalyzerInterface'); $this->contentTypeManager = $this->prophesize('Sulu\\Component\\Content\\ContentTypeManagerInterface'); $this->structureManager = $this->prophesize('Sulu\\Component\\Content\\Compat\\StructureManagerInterface'); $this->sessionManager = $this->prophesize('Sulu\\Component\\PHPCR\\SessionManager\\SessionManagerInterface'); $this->session = $this->prophesize('PHPCR\\SessionInterface'); $this->node = $this->prophesize('PHPCR\\NodeInterface'); $this->parentNode = $this->prophesize('PHPCR\\NodeInterface'); $this->startPageNode = $this->prophesize('PHPCR\\NodeInterface'); $webspace = new Webspace(); $webspace->setKey('sulu_test'); $locale = new Localization(); $locale->setCountry('us'); $locale->setLanguage('en'); $this->requestAnalyzer->getWebspace()->willReturn($webspace); $this->requestAnalyzer->getCurrentLocalization()->willReturn($locale); $this->contentTypeManager->get('text_line')->willReturn(new TextLine('')); $this->sessionManager->getSession()->willReturn($this->session->reveal()); $this->sessionManager->getContentNode('sulu_test')->willReturn($this->startPageNode->reveal()); $this->session->getNodeByIdentifier('123-123-123')->willReturn($this->node->reveal()); $this->session->getNodeByIdentifier('321-321-321')->willReturn($this->parentNode->reveal()); $this->node->getIdentifier()->willReturn('123-123-123'); $this->node->getParent()->willReturn($this->parentNode->reveal()); $this->node->getDepth()->willReturn(4); $this->parentNode->getIdentifier()->willReturn('321-321-321'); $this->parentNode->getDepth()->willReturn(3); $this->startPageNode->getDepth()->willReturn(3); $this->structureResolver = new StructureResolver($this->contentTypeManager->reveal(), $this->structureManager->reveal()); }
/** * @dataProvider provideAnalyzeWithFormat */ public function testAnalyzeWithFormat($config, $expected = []) { $webspace = new Webspace(); $webspace->setKey('sulu'); $portal = new Portal(); $portal->setKey('sulu'); $localization = new Localization(); $localization->setCountry('at'); $localization->setLanguage('de'); $portalInformation = new PortalInformation($config['match_type'], $webspace, $portal, $localization, $config['portal_url'], null, $config['redirect']); $this->prepareWebspaceManager($portalInformation); $requestFormat = false; $request = $this->getMock('\\Symfony\\Component\\HttpFoundation\\Request'); $request->request = new ParameterBag(['post' => 1]); $request->query = new ParameterBag(['get' => 1]); $request->expects($this->any())->method('getHost')->will($this->returnValue('sulu.lo')); $request->expects($this->any())->method('getPathInfo')->will($this->returnValue($config['path_info'])); $request->expects($this->once())->method('setLocale')->with('de_at'); if ($expected['format']) { $request->expects($this->once())->method('setRequestFormat')->will($this->returnCallback(function ($format) use(&$requestFormat) { $requestFormat = $format; })); } $request->expects($this->once())->method('getRequestFormat')->will($this->returnCallback(function () use(&$requestFormat) { return $requestFormat; })); $this->requestAnalyzer->analyze($request); $this->assertEquals('de_at', $this->requestAnalyzer->getCurrentLocalization()->getLocalization()); $this->assertEquals('sulu', $this->requestAnalyzer->getWebspace()->getKey()); $this->assertEquals('sulu', $this->requestAnalyzer->getPortal()->getKey()); $this->assertEquals(null, $this->requestAnalyzer->getSegment()); $this->assertEquals($expected['portal_url'], $this->requestAnalyzer->getPortalUrl()); $this->assertEquals($expected['redirect'], $this->requestAnalyzer->getRedirect()); $this->assertEquals($expected['resource_locator'], $this->requestAnalyzer->getResourceLocator()); $this->assertEquals($expected['resource_locator_prefix'], $this->requestAnalyzer->getResourceLocatorPrefix()); $this->assertEquals($expected['format'], $request->getRequestFormat()); $this->assertEquals(['post' => 1], $this->requestAnalyzer->getPostParameters()); $this->assertEquals(['get' => 1], $this->requestAnalyzer->getGetParameters()); }
/** * Generates a localization from the given node. * * @param \DOMElement|\DOMNode $localizationNode * @param bool $flat * @param null $parent * * @return Localization */ protected function generateLocalizationFromNode(\DOMElement $localizationNode, $flat = false, $parent = null) { $localization = new Localization(); $localization->setLanguage($localizationNode->attributes->getNamedItem('language')->nodeValue); // set parent if given if ($parent) { $localization->setParent($parent); } // set optional nodes $countryNode = $localizationNode->attributes->getNamedItem('country'); if ($countryNode) { $localization->setCountry($countryNode->nodeValue); } $shadowNode = $localizationNode->attributes->getNamedItem('shadow'); if ($shadowNode) { $localization->setShadow($shadowNode->nodeValue); } $defaultNode = $localizationNode->attributes->getNamedItem('default'); if ($defaultNode) { $localization->setDefault($defaultNode->nodeValue == 'true'); } else { $localization->setDefault(false); } $xDefaultNode = $localizationNode->attributes->getNamedItem('x-default'); if ($xDefaultNode) { $localization->setXDefault($xDefaultNode->nodeValue == 'true'); } else { $localization->setXDefault(false); } // set child nodes if (!$flat) { foreach ($this->xpath->query('x:localization', $localizationNode) as $childNode) { $localization->addChild($this->generateLocalizationFromNode($childNode, $flat, $localization)); } } return $localization; }
public function testGetCollectionMovedResourceLocator() { // Set up test $path = '/qwertz/'; $prefix = '/de'; $uuid = 1; $portal = new Portal(); $portal->setKey('portal'); $theme = new Theme(); $theme->setKey('theme'); $webspace = new Webspace(); $webspace->setTheme($theme); $portal->setWebspace($webspace); $localization = new Localization(); $localization->setLanguage('de'); $structure = $this->getStructureMock($uuid); $requestAnalyzer = $this->getRequestAnalyzerMock($portal, $path, $prefix, $localization, RequestAnalyzerInterface::MATCH_TYPE_FULL, 'sulu.lo', 'sulu.lo/en-us'); $activeTheme = $this->getActiveThemeMock(); $contentMapper = $this->getContentMapperMock(); $contentMapper->expects($this->any())->method('loadByResourceLocator')->will($this->throwException(new ResourceLocatorMovedException('/new-test', '123-123-123'))); $portalRouteProvider = new ContentRouteProvider($contentMapper, $requestAnalyzer, $activeTheme); $request = $this->getRequestMock($path); // Test the route provider $routes = $portalRouteProvider->getRouteCollectionForRequest($request); $this->assertCount(1, $routes); $route = $routes->getIterator()->current(); $this->assertEquals('SuluWebsiteBundle:Default:redirect', $route->getDefaults()['_controller']); $this->assertEquals('/de/new-test', $route->getDefaults()['url']); }
private function upgradeNode(NodeInterface $node, Webspace $webspace, Localization $localization, OutputInterface $output, $depth = 0) { $locale = $localization->getLocale(); $localizedTemplatePropertyName = $this->propertyEncoder->localizedSystemName('template', $locale); if (!$node->hasProperty($localizedTemplatePropertyName)) { return; } $structureMetadata = $this->structureMetadataFactory->getStructureMetadata($this->metadataFactory->getMetadataForPhpcrNode($node)->getAlias(), $node->getPropertyValue($localizedTemplatePropertyName)); $property = $structureMetadata->getPropertyByTagName('sulu.rlp'); if (!$property) { return; } $nodeType = $node->getPropertyValue($this->propertyEncoder->localizedSystemName('nodeType', $locale)); if ($property->getContentTypeName() !== 'resource_locator' && $nodeType !== Structure::NODE_TYPE_CONTENT) { return; } $baseRoutePath = $this->sessionManager->getRoutePath($webspace->getKey(), $localization->getLocale()); foreach ($node->getReferences('sulu:content') as $routeProperty) { if (strpos($routeProperty->getPath(), $baseRoutePath) !== 0) { continue; } $routeNode = $routeProperty->getParent(); if ($routeNode->getPropertyValue('sulu:history') === true) { continue; } $resourceLocator = substr($routeNode->getPath(), strlen($baseRoutePath)); if ($resourceLocator) { // only set if resource locator is not empty // if the resource locator is empty it is the homepage, whose url should not be changed $node->setProperty($this->propertyEncoder->localizedContentName($property->getName(), $locale), $resourceLocator); $prefix = ' '; for ($i = 0; $i < $depth; ++$i) { $prefix .= '-'; } $title = $node->getPropertyValue($this->propertyEncoder->localizedContentName('title', $locale)); $output->writeln($prefix . '> "' . $title . '": ' . $resourceLocator); } break; } }