protected function setupUrlGenerator() { $this->urlGenerator->expects($this->any())->method('generateFromRoute')->willReturnCallback(function ($route, $parameters, $options) { $query_string = ''; if (!empty($options['query'])) { $query_string = '?' . $options['query']; } return '/current-path' . $query_string; }); }
/** * {@inheritdoc} */ protected function setUp() { $this->eventDispatcher = $this->getMock(EventDispatcherInterface::class); $this->entityManager = $this->getMock(EntityManagerInterface::class); $this->urlGenerator = $this->getMock(UrlGeneratorInterface::class); $this->urlGenerator->expects($this->any())->method('generateFromRoute')->willReturn('http://example.com'); $this->stringTranslation = $this->getStringTranslationStub(); $this->payment = $this->getMock(PaymentInterface::class); $this->sut = new PaymentReference([], 'payment_reference', [], $this->eventDispatcher, $this->urlGenerator, $this->entityManager, $this->stringTranslation); $this->sut->setPayment($this->payment); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->urlGenerator = $this->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface'); $this->urlGenerator->expects($this->any())->method('generateFromPath')->will($this->returnArgument(0)); $this->router = $this->getMock('Drupal\\Tests\\Core\\Routing\\TestRouterInterface'); $container = new ContainerBuilder(); $container->set('router', $this->router); $container->set('url_generator', $this->urlGenerator); \Drupal::setContainer($container); }
/** * @covers ::execute */ public function testExecute() { $url = $this->randomMachineName(); $currency = $this->getMock(CurrencyInterface::class); $currency->expects($this->once())->method('enable'); $currency->expects($this->once())->method('save'); $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with('entity.currency.collection')->willReturn($url); $response = $this->sut->execute($currency); $this->assertInstanceOf(RedirectResponse::class, $response); $this->assertSame($url, $response->getTargetUrl()); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $map = array(); $map[] = array('view.frontpage.page_1', array(), array(), '/node'); $map[] = array('node_view', array('node' => '1'), array(), '/node/1'); $map[] = array('node_edit', array('node' => '2'), array(), '/node/2/edit'); $this->map = $map; $this->urlGenerator = $this->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface'); $this->urlGenerator->expects($this->any())->method('generateFromRoute')->will($this->returnValueMap($this->map)); $this->router = $this->getMock('Drupal\\Tests\\Core\\Routing\\TestRouterInterface'); $container = new ContainerBuilder(); $container->set('router', $this->router); $container->set('url_generator', $this->urlGenerator); \Drupal::setContainer($container); }
/** * Assert user redirected to homepage when controller invoked. */ private function assertRedirectedToFrontPageOnHandle() { // URL Generator will generate a path to the homepage. $this->urlGenerator->expects($this->once())->method('generate')->with('<front>')->will($this->returnValue('http://example.com/front')); $response = $this->serviceController->handle(); $this->assertTrue($response->isRedirect('http://example.com/front')); }
/** * Tests the redirectForm() method when no redirect is expected. * * @covers ::redirectForm * * @dataProvider providerTestRedirectWithoutResult */ public function testRedirectWithoutResult($form_state) { $form_submitter = $this->getFormSubmitter(); $this->urlGenerator->expects($this->never())->method('generateFromPath'); $this->urlGenerator->expects($this->never())->method('generateFromRoute'); $form_state += $this->getFormStateDefaults(); $redirect = $form_submitter->redirectForm($form_state); $this->assertNull($redirect); }
/** * Test generating the logout url with front page specified. * * @covers ::getServerLogoutUrl */ public function testGetServerLogoutUrlFrontPage() { $config_factory = $this->getConfigFactoryStub(array('cas.settings' => array('logout.logout_destination' => '<front>'))); $cas_helper = $this->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')->setConstructorArgs(array($config_factory, $this->urlGenerator, $this->connection, $this->loggerFactory, $this->session))->setMethods(array('getServerBaseUrl'))->getMock(); $cas_helper->expects($this->once())->method('getServerBaseUrl')->will($this->returnValue('https://example.com/')); $request = $this->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Request')->disableOriginalConstructor()->getMock(); $this->urlGenerator->expects($this->once())->method('generate')->will($this->returnValue('https://example.com/frontpage')); $this->assertEquals('https://example.com/logout?service=https%3A//example.com/frontpage', $cas_helper->getServerLogoutUrl($request)); }
/** * Tests the fromUri() method with an invalid entity: URI. * * @covers ::fromUri * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException */ public function testInvalidEntityUriParameter() { // Make the mocked URL generator behave like the actual one. $this->urlGenerator->expects($this->once()) ->method('generateFromRoute') ->with('entity.test_entity.canonical', ['test_entity' => '1/blah']) ->willThrowException(new InvalidParameterException('Parameter "test_entity" for route "/test_entity/{test_entity}" must match "[^/]++" ("1/blah" given) to generate a corresponding URL..')); Url::fromUri('entity:test_entity/1/blah')->toString(); }
/** * Tests the redirectForm() method when no redirect is expected. * * @covers ::redirectForm */ public function testRedirectWithoutResult() { $form_submitter = $this->getFormSubmitter(); $this->urlGenerator->expects($this->never())->method('generateFromPath'); $this->urlGenerator->expects($this->never())->method('generateFromRoute'); $form_state = $this->getMock('Drupal\\Core\\Form\\FormStateInterface'); $form_state->expects($this->once())->method('getRedirect')->willReturn(FALSE); $redirect = $form_submitter->redirectForm($form_state); $this->assertNull($redirect); }
/** * Tests the getPathByAlias() method. * * @covers ::getSystemPath */ public function testGetSystemPath() { $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity_type->expects($this->any())->method('getLinkTemplates')->will($this->returnValue(array('canonical' => 'entity.test_entity_type.canonical'))); $this->entityManager->expects($this->any())->method('getDefinition')->with('test_entity_type')->will($this->returnValue($entity_type)); $no_link_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type')); $this->assertSame('', $no_link_entity->getSystemPath('banana')); $this->urlGenerator->expects($this->once())->method('getPathFromRoute')->with('entity.test_entity_type.canonical', array('test_entity_type' => 'test_entity_id'))->will($this->returnValue('entity/test_entity_type/test_entity_id')); $valid_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type')); $this->assertSame('entity/test_entity_type/test_entity_id', $valid_entity->getSystemPath()); }
/** * Tests the redirectForm() method when no redirect is expected. * * @covers ::redirectForm */ public function testRedirectWithoutResult() { $form_submitter = $this->getFormSubmitter(); $this->urlGenerator->expects($this->never())->method('generateFromRoute'); $this->unroutedUrlAssembler->expects($this->never())->method('assemble'); $container = new ContainerBuilder(); $container->set('url_generator', $this->urlGenerator); $container->set('unrouted_url_assembler', $this->unroutedUrlAssembler); \Drupal::setContainer($container); $form_state = $this->getMock('Drupal\\Core\\Form\\FormStateInterface'); $form_state->expects($this->once())->method('getRedirect')->willReturn(FALSE); $redirect = $form_submitter->redirectForm($form_state); $this->assertNull($redirect); }
/** * Tests link rendering with a URL and options. * * @dataProvider providerTestRenderAsLinkWithUrlAndOptions * @covers ::renderAsLink */ public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL) { $alter += ['make_link' => TRUE, 'url' => $url]; $final_html = isset($final_html) ? $final_html : $link_html; $this->setUpUrlIntegrationServices(); $this->setupDisplayWithEmptyArgumentsAndFields(); $field = $this->setupTestField(['alter' => $alter]); $field->field_alias = 'key'; $row = new ResultRow(['key' => 'value']); $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions); $expected_link_url->setUrlGenerator($this->urlGenerator); $expected_url_options = $expected_url->getOptions(); unset($expected_url_options['attributes']); $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with($expected_url->getRouteName(), $expected_url->getRouteParameters(), $expected_url_options)->willReturn($url_path); $result = $field->advancedRender($row); $this->assertEquals($final_html, $result); }
/** * Tests the url() method. * * @covers ::url */ public function testUrl() { $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity_type->expects($this->any())->method('getLinkTemplates')->will($this->returnValue(array('canonical' => 'test_entity_type.view'))); $this->entityManager->expects($this->any())->method('getDefinition')->with('test_entity_type')->will($this->returnValue($entity_type)); $invalid_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(array(), 'test_entity_type')); $this->assertSame('', $invalid_entity->url()); $no_link_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type')); $this->assertSame('', $no_link_entity->url('banana')); $valid_entity = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(array('id' => 'test_entity_id'), 'test_entity_type')); $language = new Language(array('id' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->urlGenerator->expects($this->any())->method('generateFromRoute')->willReturnCallback(function ($route_name, $route_parameters, $options) use($language) { if ($route_name === 'entity.test_entity_type.canonical' && $route_parameters === array('test_entity_type' => 'test_entity_id') && array_keys($options) === ['entity_type', 'entity', 'language'] && $options['language'] == $language) { return '/entity/test_entity_type/test_entity_id'; } if ($route_name === 'entity.test_entity_type.canonical' && $route_parameters === array('test_entity_type' => 'test_entity_id') && array_keys($options) === ['absolute', 'entity_type', 'entity', 'language'] && $options['language'] == $language) { return 'http://drupal/entity/test_entity_type/test_entity_id'; } }); $this->assertSame('/entity/test_entity_type/test_entity_id', $valid_entity->url()); $this->assertSame('http://drupal/entity/test_entity_type/test_entity_id', $valid_entity->url('canonical', array('absolute' => TRUE))); }
/** * @covers ::overview */ public function testOverview() { $currency_code_from = 'EUR'; $currency_code_to = 'NLG'; $rate = '2.20371'; $currency_from = $this->getMock(CurrencyInterface::class); $currency_from->expects($this->once())->method('label'); $currency_to = $this->getMock(CurrencyInterface::class); $currency_to->expects($this->once())->method('label'); $map = array(array($currency_code_from, $currency_from), array($currency_code_to, $currency_to)); $this->currencyStorage->expects($this->any())->method('load')->willReturnMap($map); $rates_configuration = array($currency_code_from => array($currency_code_to => $rate)); $fixed_rates = $this->getMockBuilder(FixedRates::class)->disableOriginalConstructor()->getMock(); $fixed_rates->expects($this->once())->method('loadAll')->willReturn($rates_configuration); $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with('currency_fixed_rates')->willReturn($fixed_rates); $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with('currency.exchange_rate_provider.fixed_rates.add'); $amount_formatter = $this->getMock(AmountFormatterInterface::class); $amount_formatter->expects($this->once())->method('formatAmount')->with($currency_to, $rate); $this->currencyAmountFormatterManager->expects($this->once())->method('getDefaultPlugin')->willReturn($amount_formatter); $build = $this->sut->overview(); $this->assertInternalType('array', $build); }
/** * Tests ConfigNamesMapper::getOverviewPath(). */ public function testGetOverviewPath() { $this->urlGenerator->expects($this->once())->method('getPathFromRoute')->with('config_translation.item.overview.system.site_information_settings', [])->willReturn('/admin/config/system/site-information/translate'); $result = $this->configNamesMapper->getOverviewPath(); $this->assertSame('/admin/config/system/site-information/translate', $result); }