/** * Tests overriding an existing route. * * @covers ::alterRoutes * @covers ::findPageRouteName * * @dataProvider providerTestAlterRoutesOverrideExisting */ public function testAlterRoutesOverrideExisting($page_path, $existing_route_path, $requirements = []) { $route_name = 'test_route'; // Set up a page with the same path as an existing route. $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE)->shouldBeCalled(); $page->getPath()->willReturn($page_path)->shouldBeCalled(); $variant1 = $this->prophesize(PageVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants()->willReturn(['variant1' => $variant1->reveal()]); $page->id()->willReturn('page1'); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()])->shouldBeCalledTimes(1); $this->cacheTagsInvalidator->invalidateTags(["page_manager_route_name:{$route_name}"])->shouldBeCalledTimes(1); $collection = new RouteCollection(); $collection->add($route_name, new Route($existing_route_path, ['default_exists' => 'default_value'], $requirements, ['parameters' => ['foo' => 'bar']])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); // The normal route name is not used, the existing route name is instead. $this->assertSame(1, $collection->count()); $this->assertNull($collection->get('page_manager.page_view_page1')); $this->assertNull($collection->get('page_manager.page_view_page1_variant1')); $route = $collection->get($route_name); $expected_defaults = ['_entity_view' => 'page_manager_page_variant', '_title' => NULL, 'page_manager_page_variant' => 'variant1', 'page_manager_page' => 'page1', 'page_manager_page_variant_weight' => 0, 'base_route_name' => $route_name]; $expected_requirements = $requirements; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['page_manager_page_variant' => ['type' => 'entity:page_variant'], 'page_manager_page' => ['type' => 'entity:page'], 'foo' => 'bar'], '_admin_route' => FALSE]; $this->assertMatchingRoute($route, $existing_route_path, $expected_defaults, $expected_requirements, $expected_options); }
/** * Tests overriding an existing route with configured parameters. * * @covers ::alterRoutes * @covers ::findPageRouteName * * @dataProvider providerTestAlterRoutesOverrideExisting */ public function testAlterRoutesOverrideExistingWithConfiguredParameters($page_path, $existing_route_path, $requirements = []) { $route_name = 'test_route'; // Set up a page with the same path as an existing route. /** @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface $page */ $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE); $page->getPath()->willReturn($page_path); $page->id()->willReturn('page1'); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $page->getParameters()->willReturn(['foo' => ['machine_name' => 'foo', 'type' => 'integer', 'label' => 'Foo'], 'test_route' => ['machine_name' => 'test_route', 'type' => '', 'label' => '']]); $variant1 = $this->prophesize(PageVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants()->willReturn(['variant1' => $variant1->reveal()]); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()]); $collection = new RouteCollection(); $collection->add($route_name, new Route($existing_route_path, ['default_exists' => 'default_value'], $requirements, ['parameters' => ['foo' => ['bar' => 'bar']]])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); $expected_defaults = ['_entity_view' => 'page_manager_page_variant', '_title' => NULL, 'page_manager_page_variant' => 'variant1', 'page_manager_page' => 'page1', 'page_manager_page_variant_weight' => 0, 'base_route_name' => $route_name]; $expected_requirements = $requirements + ['_page_access' => 'page_manager_page.view']; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['foo' => ['bar' => 'bar', 'type' => 'integer'], 'page_manager_page_variant' => ['type' => 'entity:page_variant'], 'page_manager_page' => ['type' => 'entity:page']], '_admin_route' => FALSE]; $this->assertMatchingRoute($collection->get($route_name), $existing_route_path, $expected_defaults, $expected_requirements, $expected_options); }
/** * @covers ::loadMultiple */ public function testLoadMultiple() { $sourceCurrencyCodeA = 'EUR'; $destinationCurrencyCodeA = 'NLG'; $rateA = '2.20371'; $sourceCurrencyCodeB = 'NLG'; $destinationCurrencyCodeB = 'EUR'; $rateB = '0.453780216'; // Convert both currencies to each other and themselves. $requested_rates_provider = [$sourceCurrencyCodeA => [$destinationCurrencyCodeA, $sourceCurrencyCodeA], $sourceCurrencyCodeB => [$destinationCurrencyCodeB, $sourceCurrencyCodeB]]; // By the time plugin A will be called, the identical source and destination // currencies will have been processed. $requested_rates_plugin_a = [$sourceCurrencyCodeA => [$destinationCurrencyCodeA], $sourceCurrencyCodeB => [$destinationCurrencyCodeB]]; // By the time plugin B will be called, the 'A' source and destination // currencies will have been processed. $requested_rates_plugin_b = [$sourceCurrencyCodeB => [$destinationCurrencyCodeB]]; $exchangeRateProviderA = $this->getMock(ExchangeRateProviderInterface::class); $returnedRatesA = [$sourceCurrencyCodeA => [$destinationCurrencyCodeA => new ExchangeRate($sourceCurrencyCodeA, $destinationCurrencyCodeA, $rateA)], $sourceCurrencyCodeB => [$destinationCurrencyCodeB => null]]; $exchangeRateProviderA->expects($this->once())->method('loadMultiple')->with($requested_rates_plugin_a)->willReturn($returnedRatesA); $exchangeRateProviderB = $this->getMock(ExchangeRateProviderInterface::class); $returnedRatesB = [$sourceCurrencyCodeA => [$destinationCurrencyCodeA => null], $sourceCurrencyCodeB => [$destinationCurrencyCodeB => new ExchangeRate($sourceCurrencyCodeA, $destinationCurrencyCodeA, $rateB)]]; $exchangeRateProviderB->expects($this->once())->method('loadMultiple')->with($requested_rates_plugin_b)->willReturn($returnedRatesB); $exchangeRateProviderC = $this->getMock(ExchangeRateProviderInterface::class); $exchangeRateProviderC->expects($this->never())->method('loadMultiple'); $this->sut->expects($this->atLeastOnce())->method('getExchangeRateProviders')->willReturn([$exchangeRateProviderA, $exchangeRateProviderB, $exchangeRateProviderC]); $exchangeRates = $this->sut->loadMultiple($requested_rates_provider); $this->assertSame($returnedRatesA[$sourceCurrencyCodeA][$destinationCurrencyCodeA], $exchangeRates[$sourceCurrencyCodeA][$destinationCurrencyCodeA]); $this->assertSame('1', $exchangeRates[$sourceCurrencyCodeA][$sourceCurrencyCodeA]->getRate()); $this->assertSame($returnedRatesB[$sourceCurrencyCodeB][$destinationCurrencyCodeB], $exchangeRates[$sourceCurrencyCodeB][$destinationCurrencyCodeB]); $this->assertSame('1', $exchangeRates[$sourceCurrencyCodeB][$sourceCurrencyCodeB]->getRate()); }
/** * @covers ::doLoadMultiple * @covers ::buildCacheId * @covers ::getFromPersistentCache */ public function testLoadMultiplePersistentCached() { $this->setUpModuleHandlerNoImplementations(); $key = 'values:' . $this->entityTypeId . ':1'; $id = 1; $entity = $this->getMockBuilder('\\Drupal\\Tests\\Core\\Entity\\Sql\\SqlContentEntityStorageTestEntityInterface')->getMockForAbstractClass(); $entity->expects($this->any())->method('id')->will($this->returnValue($id)); $this->entityType->expects($this->atLeastOnce())->method('isPersistentlyCacheable')->will($this->returnValue(TRUE)); $this->entityType->expects($this->atLeastOnce())->method('id')->will($this->returnValue($this->entityTypeId)); $this->entityType->expects($this->atLeastOnce())->method('getClass')->will($this->returnValue(get_class($entity))); $this->cache->expects($this->once())->method('getMultiple')->with(array($key))->will($this->returnValue(array($key => (object) array('data' => $entity)))); $this->cache->expects($this->never())->method('set'); $this->setUpEntityStorage(); $entities = $this->entityStorage->loadMultiple(array($id)); $this->assertEquals($entity, $entities[$id]); }
/** * Tests overriding an existing route. * * @covers ::alterRoutes */ public function testAlterRoutesOverrideExisting() { // Set up a page with the same path as an existing route. $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE)->shouldBeCalledTimes(1); $page->getPath()->willReturn('/test_route')->shouldBeCalledTimes(1); $page->isFallbackPage()->willReturn(FALSE); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()])->shouldBeCalledTimes(1); $collection = new RouteCollection(); $collection->add('test_route', new Route('test_route', [], [], ['parameters' => ['foo' => 'bar']])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); // The normal route name is not used, the existing route name is instead. $this->assertSame(1, $collection->count()); $this->assertNull($collection->get('page_manager.page_view_page1')); $route = $collection->get('test_route'); $expected_defaults = ['_entity_view' => 'page_manager_page', 'page_manager_page' => 'page1', '_title' => NULL]; $expected_requirements = ['_entity_access' => 'page_manager_page.view']; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['page_manager_page' => ['type' => 'entity:page'], 'foo' => 'bar'], '_admin_route' => FALSE]; $this->assertMatchingRoute($route, '/test_route', $expected_defaults, $expected_requirements, $expected_options); }