public function testToString() { $url = '/test'; $slug = new Slug(); $slug->setUrl($url); $this->assertEquals($url, (string) $slug); }
/** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('parentPage', EntityIdentifierType::NAME, ['class' => $this->dataClass, 'multiple' => false])->add('title', 'text', ['label' => 'orob2b.cms.page.title.label', 'required' => true, 'constraints' => [new NotBlank()]])->add('content', OroRichTextType::NAME, ['label' => 'orob2b.cms.page.content.label', 'required' => false, 'wysiwyg_options' => ['statusbar' => true, 'resize' => true]]); $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { /** @var Page $page */ $page = $event->getData(); $form = $event->getForm(); if ($page && $page->getId()) { $form->add('slug', SlugType::NAME, ['label' => 'orob2b.redirect.slug.entity_label', 'mapped' => false, 'type' => 'update', 'current_slug' => $page->getCurrentSlug()->getSlugUrl()]); } else { $form->add('slug', SlugType::NAME, ['label' => 'orob2b.redirect.slug.entity_label', 'mapped' => false, 'type' => 'create']); } }); $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) { $slugData = $event->getForm()->get('slug')->getData(); /** @var Page $page */ $page = $event->getData(); if ($slugData['mode'] === 'new') { if (isset($slugData['redirect']) && $slugData['redirect']) { // Leave the old slug for page. And add a new slug as current for page $slug = new Slug(); $slug->setUrl($slugData['slug']); $page->setCurrentSlug($slug); } else { // Change current slug url $page->setCurrentSlugUrl($slugData['slug']); } } }); }
/** * Set unique url for Slug entity * * @param Slug $slug */ public function makeUrlUnique(Slug $slug) { $existingSlug = $this->findSlugByUrl($slug->getUrl()); if (null !== $existingSlug && $existingSlug->getId() !== $slug->getId()) { $incrementedUrl = $this->incrementUrl($slug->getUrl()); while (null !== $this->findSlugByUrl($incrementedUrl)) { $incrementedUrl = $this->incrementUrl($incrementedUrl); } $slug->setUrl($incrementedUrl); } }
public function testMakeUrlUnique() { $manager = $this->registry->getManagerForClass('OroB2BRedirectBundle:Slug'); $slug = new Slug(); $slug->setUrl('domain.com/hvac-equipment/detection-kits'); $slug->setRouteName('orob2b_cms_page_view'); $slug->setRouteParameters(['id' => 1]); $manager->persist($slug); $slug1 = new Slug(); $slug1->setUrl('domain.com/hvac-equipment/detection-kits1-1'); $slug1->setRouteName('orob2b_cms_page_view'); $slug1->setRouteParameters(['id' => 1]); $manager->persist($slug1); $slug2 = new Slug(); $slug2->setUrl('domain.com/hvac-equipment/detection-kits1-2'); $slug2->setRouteName('orob2b_cms_page_view'); $slug2->setRouteParameters(['id' => 1]); $manager->persist($slug2); $manager->flush(); $testSlug = new Slug(); $testSlug->setUrl('domain.com/hvac-equipment/detection-kits'); $testSlug->setRouteName('orob2b_cms_page_view'); $testSlug->setRouteParameters(['id' => 2]); $this->slugManager->makeUrlUnique($testSlug); $manager->persist($testSlug); $manager->flush(); $this->assertEquals('domain.com/hvac-equipment/detection-kits-1', $testSlug->getUrl()); $testSlug1 = new Slug(); $testSlug1->setUrl('domain.com/hvac-equipment/detection-kits'); $testSlug1->setRouteName('orob2b_cms_page_view'); $testSlug1->setRouteParameters(['id' => 21]); $this->slugManager->makeUrlUnique($testSlug1); $manager->persist($testSlug1); $manager->flush(); $this->assertEquals('domain.com/hvac-equipment/detection-kits-2', $testSlug1->getUrl()); $testSlug2 = new Slug(); $testSlug2->setUrl('domain.com/hvac-equipment/detection-kits1-1'); $testSlug2->setRouteName('orob2b_cms_page_view'); $testSlug2->setRouteParameters(['id' => 21]); $this->slugManager->makeUrlUnique($testSlug2); $manager->persist($testSlug2); $manager->flush(); $this->assertEquals('domain.com/hvac-equipment/detection-kits1-3', $testSlug2->getUrl()); }
/** * @dataProvider onKernelRequestDataProvider * @param boolean $installed * @param string $requestType * @param boolean $existingController * @param array $slug_params * @param array $expected */ public function testOnKernelRequest($installed, $requestType, $existingController, array $slug_params, array $expected) { $this->listener = new ForwardListener($this->router, $this->registry, $installed); /** * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject $kernel */ $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $request = Request::create('http://localhost' . $slug_params['url']); if ($existingController) { $request->attributes->add(['_controller' => 'ExistingController']); } $event = new GetResponseEvent($kernel, $request, $requestType); $slug = new Slug(); $slug->setRouteName($slug_params['route_name']); $slug->setUrl($slug_params['url']); $slug->setRouteParameters($slug_params['route_parameters']); if ($requestType === HttpKernelInterface::MASTER_REQUEST) { $slugRepository = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); if ($slug_params['url'] !== '/') { $slug_params['url'] = rtrim($slug_params['url'], '/'); } if ($slug_params['url'] === '/missing-slug') { $slugRepository->expects($this->any())->method('findOneBy')->with(['url' => $slug_params['url']])->will($this->returnValue(null)); } else { $slugRepository->expects($this->any())->method('findOneBy')->with(['url' => $slug_params['url']])->will($this->returnValue($slug)); } $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $em->expects($this->any())->method('getRepository')->with('OroB2BRedirectBundle:Slug')->will($this->returnValue($slugRepository)); $this->registry->expects($this->any())->method('getManagerForClass')->with('OroB2BRedirectBundle:Slug')->will($this->returnValue($em)); $route = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); $route->expects($this->any())->method('getDefault')->with('_controller')->will($this->returnValue('TestController')); $routeCollection = $this->getMock('Symfony\\Component\\Routing\\RouteCollection'); $routeCollection->expects($this->any())->method('get')->with('test_route')->will($this->returnValue($route)); $this->router->expects($this->any())->method('getRouteCollection')->will($this->returnValue($routeCollection)); } $this->listener->onKernelRequest($event); if ($requestType === HttpKernelInterface::MASTER_REQUEST) { $parameters = $request->attributes->all(); $this->assertEquals($expected, $parameters); } }
/** * @param int $pageId * @depends testPersistPageSlug */ public function testUpdatePageSlug($pageId) { $page = $this->entityManager->find('OroB2BCMSBundle:Page', $pageId); $currentSlug = $page->getCurrentSlug(); $currentSlug->setRouteName('incorrect_route')->setRouteParameters(['invalid' => 'parameters']); $newSlug = new Slug(); $newSlug->setUrl('/update')->setRouteName('incorrect_route')->setRouteParameters(['invalid' => 'parameters']); $page->setCurrentSlug($newSlug); $this->entityManager->flush($page); $expectedRouteName = 'orob2b_cms_page_view'; $expectedRouteParameters = ['id' => $pageId]; foreach ($page->getSlugs() as $slug) { $this->assertEquals($expectedRouteName, $slug->getRouteName()); $this->assertEquals($expectedRouteParameters, $slug->getRouteParameters()); } // make sure data updated correctly $this->entityManager->clear(); $updatedPage = $this->entityManager->find('OroB2BCMSBundle:Page', $pageId); foreach ($updatedPage->getSlugs() as $slug) { $this->assertEquals($expectedRouteName, $slug->getRouteName()); $this->assertEquals($expectedRouteParameters, $slug->getRouteParameters()); } }
public function testSetCurrentSlugUrl() { $rootSlug = new Slug(); $rootSlug->setUrl('/root'); $rootPage = new Page(); $rootPage->setCurrentSlug($rootSlug); $childSlug = new Slug(); $childSlug->setUrl('/first'); $childPage = new Page(); $childPage->setCurrentSlug($childSlug); $rootPage->addChildPage($childPage); $childPage->setCurrentSlugUrl('first-altered'); $this->assertEquals('/root', $rootPage->getCurrentSlugUrl()); $this->assertEquals('/root/first-altered', $childPage->getCurrentSlugUrl()); $rootPage->setCurrentSlugUrl('root-altered'); $this->assertEquals('/root-altered', $rootPage->getCurrentSlugUrl()); $this->assertEquals('/root-altered/first-altered', $childPage->getCurrentSlugUrl()); }
/** * Refresh slug URLs for current and child pages */ protected function refreshSlugUrls() { $parentSlugUrl = ''; if ($this->parentPage) { $parentSlugUrl = $this->parentPage->currentSlug->getUrl(); } $slugUrl = $this->currentSlug->getSlugUrl(); $this->currentSlug->setUrl($parentSlugUrl . Slug::DELIMITER . $slugUrl); foreach ($this->childPages as $childPage) { $childPage->refreshSlugUrls(); } }