/** * Tests the unique machine name generator. * * @see \Drupal\block\BlockForm::getUniqueMachineName() */ public function testGetUniqueMachineName() { $blocks = array(); $blocks['test'] = $this->getBlockMockWithMachineName('test'); $blocks['other_test'] = $this->getBlockMockWithMachineName('other_test'); $blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test'); $blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test'); $query = $this->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface'); $query->expects($this->exactly(5))->method('condition')->will($this->returnValue($query)); $query->expects($this->exactly(5))->method('execute')->will($this->returnValue(array('test', 'other_test', 'other_test_1', 'other_test_2'))); $this->storage->expects($this->exactly(5))->method('getQuery')->will($this->returnValue($query)); $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler); // Ensure that the block with just one other instance gets the next available // name suggestion. $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test'])); // Ensure that the block with already three instances (_0, _1, _2) gets the // 4th available name. $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test'])); $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1'])); $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2'])); // Ensure that a block without an instance yet gets the suggestion as // unique machine name. $last_block = $this->getBlockMockWithMachineName('last_test'); $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block)); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // TODO: Change the autogenerated stub $condition_plugin_manager = $this->getMock('Drupal\\Core\\Executable\\ExecutableManagerInterface'); $condition_plugin_manager->expects($this->any())->method('getDefinitions')->will($this->returnValue(array())); $container = new ContainerBuilder(); $container->set('plugin.manager.condition', $condition_plugin_manager); \Drupal::setContainer($container); $this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])->getMock(); $this->executable->expects($this->any())->method('setDisplay')->with('block_1')->will($this->returnValue(TRUE)); $this->executable->expects($this->any())->method('getShowAdminLinks')->willReturn(FALSE); $this->executable->display_handler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->setMethods(NULL)->getMock(); $this->view = $this->getMockBuilder('Drupal\\views\\Entity\\View')->disableOriginalConstructor()->getMock(); $this->view->expects($this->any())->method('id')->willReturn('test_view'); $this->executable->storage = $this->view; $this->executableFactory = $this->getMockBuilder('Drupal\\views\\ViewExecutableFactory')->disableOriginalConstructor()->getMock(); $this->executableFactory->expects($this->any())->method('get')->with($this->view)->will($this->returnValue($this->executable)); $this->displayHandler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->getMock(); $this->displayHandler->expects($this->any())->method('blockSettings')->willReturn([]); $this->displayHandler->expects($this->any())->method('getPluginId')->willReturn('block'); $this->executable->display_handler = $this->displayHandler; $this->storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')->disableOriginalConstructor()->getMock(); $this->storage->expects($this->any())->method('load')->with('test_view')->will($this->returnValue($this->view)); $this->account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); }
/** * @covers ::execute */ public function testExecute() { $currency = $this->getMock(CurrencyInterface::class); $this->currencyStorage->expects($this->once())->method('create')->with(array())->willReturn($currency); $form = $this->getMock(EntityFormInterface::class); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($currency)->willReturn($form); $this->assertSame($form, $this->sut->execute()); }
/** * @covers ::execute */ public function testExecute() { $payment_status = $this->getMock(PaymentStatusInterface::class); $this->paymentStatusStorage->expects($this->once())->method('create')->willReturn($payment_status); $form = $this->getMock(FormInterface::class); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment_status)->willReturn($form); $this->assertSame($form, $this->sut->execute()); }
/** * @covers ::getOperations */ public function testGetOperations() { $entity_id = $this->randomMachineName(); $plugin_id = 'payment_config:' . $entity_id; $payment_status = $this->getMock(PaymentStatusInterface::class); $this->paymentStatusStorage->expects($this->once())->method('load')->with($entity_id)->willReturn($payment_status); $operations = array('foo' => array('title' => $this->randomMachineName())); $this->paymentStatusListBuilder->expects($this->once())->method('getOperations')->with($payment_status)->willReturn($operations); $this->assertSame($operations, $this->sut->getOperations($plugin_id)); }
/** * @covers ::getPaymentMethodConfiguration */ public function testGetPaymentMethodConfiguration() { $entity_id = $this->randomMachineName(); $plugin_id = 'payment_basic:' . $entity_id; $payment_method_configuration = $this->getMock(PaymentMethodConfigurationInterface::class); $this->paymentMethodConfigurationStorage->expects($this->once())->method('load')->with($entity_id)->willReturn($payment_method_configuration); $method = new \ReflectionMethod($this->sut, 'getPaymentMethodConfiguration'); $method->setAccessible(TRUE); $this->assertEquals($payment_method_configuration, $method->invoke($this->sut, $plugin_id)); }
/** * @covers ::process * @covers ::processCallback */ function testProcess() { $cache_contexts = Cache::mergeContexts(['baz', 'qux']); $cache_tags = Cache::mergeTags(['foo', 'bar']); $map = [['100', TRUE, LanguageInterface::TYPE_CONTENT, '€100.00'], ['100.7654', TRUE, LanguageInterface::TYPE_CONTENT, '€100.77'], ['1.99', TRUE, LanguageInterface::TYPE_CONTENT, '€1.99'], ['2.99', TRUE, LanguageInterface::TYPE_CONTENT, '€2.99']]; $currency = $this->getMock(CurrencyInterface::class); $currency->expects($this->any())->method('formatAmount')->willReturnMap($map); $currency->expects($this->atLeastOnce())->method('getCacheContexts')->willReturn($cache_contexts); $currency->expects($this->atLeastOnce())->method('getCacheTags')->willReturn($cache_tags); $this->currencyStorage->expects($this->any())->method('load')->with('EUR')->willReturn($currency); $this->input->expects($this->any())->method('parseAmount')->will($this->returnArgument(0)); $langcode = $this->randomMachineName(2); $tokens_valid = ['[currency-localize:EUR:100]' => '€100.00', '[currency-localize:EUR:100.7654]' => '€100.77', '[currency-localize:EUR:1.99]' => '€1.99', '[currency-localize:EUR:2.99]' => '€2.99']; $tokens_invalid = ['[currency-localize]', '[currency-localize:]', '[currency-localize::]', '[currency-localize:EUR]', '[currency-localize:123:456]', '[currency-localize:123]']; foreach ($tokens_valid as $token => $replacement) { $result = $this->sut->process($token, $langcode); $this->assertInstanceOf(FilterProcessResult::class, $result); $this->assertSame($replacement, $result->getProcessedText()); $this->assertSame($cache_contexts, $result->getCacheContexts()); $this->assertSame($cache_tags, $result->getCacheTags()); } foreach ($tokens_invalid as $token) { $result = $this->sut->process($token, $langcode); $this->assertInstanceOf(FilterProcessResult::class, $result); $this->assertSame($token, $result->getProcessedText()); $this->assertEmpty($result->getCacheContexts()); $this->assertEmpty($result->getCacheTags()); } }
/** * @covers ::validateCurrencyNumber * @dataProvider providerTestValidateCurrencyNumber */ public function testValidateCurrencyNumber($valid, $currency_number, $currency_is_new, $currency_number_exists = FALSE) { $element = array('#value' => $currency_number); $form = array(); $form_state = $this->getMock(FormStateInterface::class); $this->currency->expects($this->any())->method('isNew')->willReturn($currency_is_new); if (!$valid) { $form_state->expects($this->once())->method('setError')->with($element, 'The currency number must be three digits.'); } elseif ($currency_number_exists) { $loaded_currency_code = $this->randomMachineName(); $loaded_currency_label = $this->randomMachineName(); $loaded_currency_url = new Url($this->randomMachineName()); $loaded_currency = $this->getMock(CurrencyInterface::class); $loaded_currency->expects($this->any())->method('id')->willReturn($loaded_currency_code); $loaded_currency->expects($this->any())->method('label')->willReturn($loaded_currency_label); $loaded_currency->expects($this->atLeastOnce())->method('urlInfo')->willReturn($loaded_currency_url); $this->currencyStorage->expects($this->once())->method('loadByProperties')->with(array('currencyNumber' => $currency_number))->willReturn(array($loaded_currency)); $form_state->expects($this->once())->method('setError'); $this->linkGenerator->expects($this->once())->method('generate')->with($loaded_currency_label, $loaded_currency_url); } else { $this->currencyStorage->expects($this->once())->method('loadByProperties')->with(array('currencyNumber' => $currency_number))->willReturn(FALSE); $form_state->expects($this->never())->method('setError'); $form_state->expects($this->never())->method('setErrorByName'); } $this->sut->validateCurrencyNumber($element, $form_state, $form); }
/** * @covers ::getDerivativeDefinitions */ public function testGetDerivativeDefinitions() { $status_a = $this->getMock(PaymentStatusInterface::class); $status_a->expects($this->once())->method('getDescription')->willReturn($this->randomMachineName()); $status_a->expects($this->once())->method('id')->willReturn($this->randomMachineName()); $status_a->expects($this->once())->method('label')->willReturn($this->randomMachineName()); $status_a->expects($this->once())->method('getParentId')->willReturn($this->randomMachineName()); $status_b = $this->getMock(PaymentStatusInterface::class); $status_b->expects($this->once())->method('getDescription')->willReturn($this->randomMachineName()); $status_b->expects($this->once())->method('id')->willReturn($this->randomMachineName()); $status_b->expects($this->once())->method('label')->willReturn($this->randomMachineName()); $status_b->expects($this->once())->method('getParentId')->willReturn($this->randomMachineName()); $this->paymentStatusStorage->expects($this->once())->method('loadMultiple')->willReturn(array($status_a, $status_b)); $derivatives = $this->sut->getDerivativeDefinitions([]); $this->assertCount(2, $derivatives); }
/** * @covers ::paymentMethodConfigurationIdExists */ public function testPaymentMethodConfigurationIdExists() { $payment_method_configuration_id = $this->randomMachineName(); $this->paymentMethodConfigurationStorage->expects($this->at(0))->method('load')->with($payment_method_configuration_id)->willReturn($this->paymentMethodConfiguration); $this->paymentMethodConfigurationStorage->expects($this->at(1))->method('load')->with($payment_method_configuration_id)->willReturn(NULL); $this->assertTrue($this->sut->paymentMethodConfigurationIdExists($payment_method_configuration_id)); $this->assertFalse($this->sut->paymentMethodConfigurationIdExists($payment_method_configuration_id)); }
/** * @covers ::calculateDependencies */ public function testCalculateDependencies() { /* @var $view_this \Drupal\views\Entity\View */ /* @var $view_other \Drupal\views\Entity\View */ $view_this = $this->getMock('Drupal\\views\\ViewEntityInterface'); $view_this->expects($this->any())->method('getConfigDependencyKey')->willReturn('config'); $view_this->expects($this->any())->method('getConfigDependencyName')->willReturn('view.this'); $view_this->expects($this->any())->method('id')->willReturn('this'); $view_other = $this->getMock('Drupal\\views\\ViewEntityInterface'); $view_other->expects($this->any())->method('getConfigDependencyKey')->willReturn('config'); $view_other->expects($this->any())->method('getConfigDependencyName')->willReturn('view.other'); $this->entityStorage->expects($this->any())->method('load')->willReturnMap([['this', $view_this], ['other', $view_other]]); $this->viewHandler->view->storage = $view_this; $this->viewHandler->options['view_to_insert'] = 'other:default'; $this->assertArrayEquals(array('config' => array('view.other')), $this->viewHandler->calculateDependencies()); $this->viewHandler->options['view_to_insert'] = 'this:default'; $this->assertArrayEquals(array(), $this->viewHandler->calculateDependencies()); }
/** * @covers ::render */ function testRenderWithNonExistingCurrency() { $currency_code = $this->randomMachineName(); $field_alias = $this->randomMachineName(); $this->sut->field_alias = $field_alias; $result_row = new ResultRow([$field_alias => $currency_code]); $this->currencyStorage->expects($this->atLeastOnce())->method('load')->with($currency_code)->willReturn(NULL); $this->assertInstanceOf(MarkupInterface::class, $this->sut->render($result_row)); }
/** * @covers ::importCurrencyLocale */ public function testImportCurrencyLocaleWithExistingCurrency() { $locale = $this->randomMachineName(); $currency_locale = $this->getMock(CurrencyLocaleInterface::class); $this->currencyLocaleStorage->expects($this->never())->method('create'); $this->currencyLocaleStorage->expects($this->once())->method('load')->with($locale)->willReturn($currency_locale); $this->configStorage->expects($this->never())->method('read'); $this->sut->setConfigStorage($this->configStorage); $this->assertFalse($this->sut->importCurrencyLocale($locale)); }
/** * Tests the building of a full page variant. * * @covers ::build * @covers ::getRegionAssignments */ public function testBuild() { $theme = $this->randomMachineName(); $display_variant = $this->setUpDisplayVariant(); $this->themeNegotiator->expects($this->any())->method('determineActiveTheme')->with($this->routeMatch)->will($this->returnValue($theme)); $display_variant->expects($this->once())->method('getRegionNames')->will($this->returnValue(array('top' => 'Top', 'bottom' => 'Bottom'))); $blocks_config = array('block1' => array(TRUE, 'top', 0), 'block2' => array(FALSE, 'bottom', 0), 'block3' => array(TRUE, 'bottom', 5), 'block4' => array(TRUE, 'bottom', -5)); $blocks = array(); foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->once())->method('access')->will($this->returnValue($block_config[0])); $block->expects($this->any())->method('get')->will($this->returnValueMap(array(array('region', $block_config[1]), array('weight', $block_config[2]), array('status', TRUE)))); $blocks[$block_id] = $block; } $this->blockViewBuilder->expects($this->exactly(3))->method('view')->will($this->returnValue(array())); $this->blockStorage->expects($this->once())->method('loadByProperties')->with(array('theme' => $theme))->will($this->returnValue($blocks)); $expected = array('top' => array('block1' => array(), '#sorted' => TRUE), 'bottom' => array('block4' => array(), 'block3' => array(), '#sorted' => TRUE)); $this->assertSame($expected, $display_variant->build()); }
/** * @covers ::resolveCurrencyLocale * * @expectedException \RuntimeException */ function testResolveCurrencyLocaleMissingFallback() { $this->prepareLanguageManager(); $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $config->expects($this->any())->method('get')->with('country.default')->willReturn(NULL); $this->configFactory->expects($this->once())->method('get')->with('system.data')->willReturn($config); $this->currencyLocaleStorage->expects($this->any())->method('load')->with(LocaleResolverInterface::DEFAULT_LOCALE)->willReturn(NULL); // Test loading the fallback locale. $this->sut->resolveCurrencyLocale(); }
/** * Tests the authenticate method with a correct password and new password hash. * * @covers ::authenticate */ public function testAuthenticateWithCorrectPasswordAndNewPasswordHash() { $this->testUser->expects($this->once())->method('id')->will($this->returnValue(1)); $this->testUser->expects($this->once())->method('setPassword')->with($this->password); $this->testUser->expects($this->once())->method('save'); $this->userStorage->expects($this->once())->method('loadByProperties')->with(array('name' => $this->username))->will($this->returnValue(array($this->testUser))); $this->passwordService->expects($this->once())->method('check')->with($this->password, $this->testUser->getPassword())->will($this->returnValue(TRUE)); $this->passwordService->expects($this->once())->method('needsRehash')->with($this->testUser->getPassword())->will($this->returnValue(TRUE)); $this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password)); }
/** * Tests the access method with a non existing entity. */ public function testAccessWithNotExistingEntity() { $request = new Request(); $request->attributes->set('entity_type', 'entity_test'); $request->attributes->set('entity', 1); $this->entityManager->expects($this->once())->method('getDefinition')->with('entity_test')->will($this->returnValue(array('id' => 'entity_test'))); $this->entityStorage->expects($this->once())->method('load')->with(1)->will($this->returnValue(NULL)); $account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $this->assertSame(AccessCheckInterface::KILL, $this->editAccessCheck->access($request, $account)); }
/** * Sets up a bunch of valid mocks like the view entity and executable. */ protected function setupValidMocks() { $view = $this->getMockBuilder('Drupal\\views\\Entity\\View')->disableOriginalConstructor()->getMock(); $this->viewStorage->expects($this->once())->method('load')->with('test_view')->will($this->returnValue($view)); $executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->getMock(); $executable->expects($this->once())->method('access')->will($this->returnValue(TRUE)); $executable->expects($this->once())->method('preview')->will($this->returnValue(array('#markup' => 'View result'))); $this->executableFactory->expects($this->once())->method('get')->with($view)->will($this->returnValue($executable)); return array($view, $executable); }
/** * @covers ::paymentStatusIdExists */ public function testPaymentStatusIdExists() { $method = new \ReflectionMethod($this->sut, 'paymentStatusIdExists'); $method->setAccessible(TRUE); $payment_method_configuration_id = $this->randomMachineName(); $this->paymentStatusStorage->expects($this->at(0))->method('load')->with($payment_method_configuration_id)->willReturn($this->paymentStatus); $this->paymentStatusStorage->expects($this->at(1))->method('load')->with($payment_method_configuration_id)->willReturn(NULL); $this->assertTrue($method->invoke($this->sut, $payment_method_configuration_id)); $this->assertFalse($method->invoke($this->sut, $payment_method_configuration_id)); }
/** * @covers ::load * @covers ::render * @covers ::getEntityIds * * @depends testBuildHeader */ public function testRender() { $query = $this->getMock(QueryInterface::class); $query->expects($this->atLeastOnce())->method('pager')->willReturnSelf(); $this->entityStorage->expects($this->atLeastOnce())->method('getQuery')->willReturn($query); $this->entityStorage->expects($this->once())->method('loadMultiple')->willReturn([]); $build = $this->sut->render(); unset($build['table']['#header']); $expected_build = array('#type' => 'table', '#title' => NULL, '#rows' => [], '#cache' => ['contexts' => NULL, 'tags' => NULL]); $this->assertInstanceOf(TranslatableMarkup::class, $build['table']['#empty']); unset($build['table']['#empty']); $this->assertEquals($expected_build, $build['table']); }
/** * @covers ::calculateDependencies */ public function testCalculateDependenciesWithEntityId() { $this->setupEntityManager(); $entity = $this->getMock('Drupal\\Core\\Entity\\EntityInterface'); $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity->expects($this->once())->method('getConfigDependencyName')->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4'); $this->entityManager->expects($this->once())->method('loadEntityByConfigTarget')->willReturn($entity); $this->entityStorage->expects($this->never())->method('loadByProperties'); $entity_type->expects($this->once())->method('getConfigDependencyKey')->willReturn('content'); $this->entityManager->expects($this->once())->method('getDefinition')->willReturn($entity_type); $options = ['target' => 1]; $this->entityHandler->init($this->executable, $this->display, $options); $this->assertEquals(['content' => ['entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4']], $this->entityHandler->calculateDependencies()); }
/** * Tests the sendMailMessages method. * * @dataProvider getSendMailMessages * * @covers ::sendMailMessages */ public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) { $this->logger->expects($this->once())->method('notice'); $this->mailManager->expects($this->any())->method('mail')->willReturnCallback(function ($module, $key, $to, $langcode, $params, $from) use(&$results) { $result = array_shift($results); $this->assertEquals($module, $result['module']); $this->assertEquals($key, $result['key']); $this->assertEquals($to, $result['to']); $this->assertEquals($langcode, $result['langcode']); $this->assertArrayEquals($params, $result['params']); $this->assertEquals($from, $result['from']); }); $this->userStorage->expects($this->any())->method('load')->willReturn(clone $sender); $this->contactMailHandler->sendMailMessages($message, $sender); }
/** * @covers ::buildPaymentView * * @dataProvider providerTestBuildPaymentViewWithPayment */ public function testBuildPaymentViewWithPayment($view_access) { $element = array('#default_value' => mt_rand()); $form_state = $this->getMock(FormStateInterface::class); $currency = $this->getMock(CurrencyInterface::class); $payment_status = $this->getMock(PaymentStatusInterface::class); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->atLeastOnce())->method('access')->willReturn($view_access); $payment->expects($this->atLeastOnce())->method('getCurrency')->willReturn($currency); $payment->expects($this->atLeastOnce())->method('getPaymentStatus')->willReturn($payment_status); $payment->expects($view_access ? $this->once() : $this->never())->method('url'); $this->paymentStorage->expects($this->once())->method('load')->with($element['#default_value'])->willReturn($payment); $method = new \ReflectionMethod($this->sut, 'buildPaymentView'); $method->setAccessible(TRUE); $build = $method->invoke($this->sut, $element, $form_state); $this->assertInternalType('array', $build); }
/** * @covers ::process * * @depends testGetInfo * * @dataProvider providerTestProcess */ public function testProcess($default_currency_loadable) { $currency_code_a = $this->randomMachineName(); $currency_code_b = $this->randomMachineName(); $currency_code_c = $this->randomMachineName(); $currency = $this->getMock(CurrencyInterface::class); $currency_options = [$currency_code_a => $this->randomMachineName(), $currency_code_b => $this->randomMachineName(), $currency_code_c => $this->randomMachineName()]; $this->formHelper->expects($this->atLeastOnce())->method('getCurrencyOptions')->willReturn($currency_options); $map = [[$currency_code_b, $default_currency_loadable ? $currency : NULL], ['XXX', $default_currency_loadable ? NULL : $currency]]; $this->currencyStorage->expects($this->atLeastOnce())->method('load')->willReturnMap($map); $limit_currency_codes = [$currency_code_a, $currency_code_b]; $element = ['#default_value' => ['amount' => mt_rand(), 'currency_code' => $currency_code_b], '#required' => TRUE, '#limit_currency_codes' => $limit_currency_codes] + $this->sut->getInfo(); $form_state = new FormState(); $form = []; $element = $this->sut->process($element, $form_state, $form); $this->assertEmpty(array_diff($limit_currency_codes, array_keys($element['currency_code']['#options']))); $this->assertEmpty(array_diff(array_keys($element['currency_code']['#options']), $limit_currency_codes)); }
/** * @covers ::submitForm */ public function testSubmitFormWitDelete() { $currency_code_from = $this->randomMachineName(); $currency_code_to = $this->randomMachineName(); $values = ['currency_code_from' => $currency_code_from, 'currency_code_to' => $currency_code_to]; $form = ['actions' => ['save' => ['#name' => 'save', '#foo' => $this->randomMachineName()], 'delete' => ['#name' => 'delete', '#foo' => $this->randomMachineName()]]]; $form_state = $this->getMock(FormStateInterface::class); $form_state->expects($this->atLeastOnce())->method('getTriggeringElement')->willReturn($form['actions']['delete']); $form_state->expects($this->atLeastOnce())->method('getValues')->willReturn($values); $form_state->expects($this->atLeastOnce())->method('setRedirect')->with('currency.exchange_rate_provider.fixed_rates.overview'); $exchange_rate_provider = $this->getMockBuilder(FixedRates::class)->disableOriginalConstructor()->getMock(); $exchange_rate_provider->expects($this->once())->method('delete')->with($currency_code_from, $currency_code_to); $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with('currency_fixed_rates')->willReturn($exchange_rate_provider); $currency_from = $this->getMock(CurrencyInterface::class); $currency_to = $this->getMock(CurrencyInterface::class); $map = [[$currency_code_from, $currency_from], [$currency_code_to, $currency_to]]; $this->currencyStorage->expects($this->atLeastOnce())->method('load')->willReturnMap($map); $this->sut->submitForm($form, $form_state); }
/** * Tests the retrieval of block entities that are context-aware. * * @covers ::getVisibleBlocksPerRegion */ public function testGetVisibleBlocksPerRegionWithContext() { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->once())->method('setContexts')->willReturnSelf(); $block->expects($this->once())->method('access')->willReturn(TRUE); $block->expects($this->once())->method('getRegion')->willReturn('top'); $blocks['block_id'] = $block; $contexts = []; $this->blockStorage->expects($this->once())->method('loadByProperties')->with(['theme' => $this->theme])->willReturn($blocks); $result = []; foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts) as $region => $resulting_blocks) { $result[$region] = []; foreach ($resulting_blocks as $plugin_id => $block) { $result[$region][] = $plugin_id; } } $expected = ['top' => ['block_id'], 'center' => [], 'bottom' => []]; $this->assertSame($expected, $result); }
/** * @covers ::render * * @dataProvider providerTestRender * * @depends testGetAmount * @depends testGetCurrencyWithoutLoadableCurrencies * @depends testGetCurrencyWithFallbackCurrency * @depends testGetCurrencyWithFixedCurrency * @depends testGetCurrencyWithCurrencyCodeField */ function testRender($round) { $amount = mt_rand(); $formatted_amount = $this->randomMachineName(); $field_alias = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $currency = $this->getMock(CurrencyInterface::class); $currency->expects($this->atLeastOnce())->method('formatAmount')->with($amount, $round)->willReturn($formatted_amount); $this->currencyStorage->expects($this->atLeastOnce())->method('load')->with($currency_code)->willReturn($currency); $result_row = new ResultRow([$field_alias => $amount]); $configuration = []; $plugin_id = $this->randomMachineName(); $this->pluginDefinition['currency_code'] = $currency_code; $options = ['currency_round' => $round]; $this->sut = new Amount($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->moduleHandler, $this->renderer, $this->currencyStorage); $this->sut->init($this->viewsViewExecutable, $this->viewsDisplayHandler, $options); $this->sut->field_alias = $field_alias; $this->assertSame($formatted_amount, $this->sut->render($result_row)); }
/** * @covers ::viewElements */ public function testViewElements() { $entity_type_id = $this->randomMachineName(); $bundle = $this->randomMachineName(); $field_name = $this->randomMachineName(); $destination_url = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $plugin_id = $this->randomMachineName(); $plugin_configuration = [$this->randomMachineName() => $this->randomMachineName()]; $plugin_id_property = $this->getMock(TypedDataInterface::class); $plugin_id_property->expects($this->once())->method('getValue')->willReturn($plugin_id); $plugin_configuration_property = $this->getMock(TypedDataInterface::class); $plugin_configuration_property->expects($this->once())->method('getValue')->willReturn($plugin_configuration); $map = [['plugin_id', $plugin_id_property], ['plugin_configuration', $plugin_configuration_property]]; $item = $this->getMockBuilder(PaymentFormFieldType::class)->disableOriginalConstructor()->getMock(); $item->expects($this->exactly(2))->method('get')->willReturnMap($map); $entity = $this->getMock(EntityInterface::class); $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle); $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id); $iterator = new \ArrayIterator([$item]); $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->setMethods(['getEntity', 'getIterator'])->getMock(); $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity); $items->expects($this->atLeastOnce())->method('getIterator')->willReturn($iterator); $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name); $this->fieldDefinition->expects($this->atLeastOnce())->method('getSetting')->with('currency_code')->willReturn($currency_code); $payment_type = $this->getMockBuilder(PaymentFormPaymentType::class)->disableOriginalConstructor()->getMock(); $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id); $payment_type->expects($this->once())->method('setBundle')->with($bundle); $payment_type->expects($this->once())->method('setFieldName')->with($field_name); $payment_type->expects($this->once())->method('setDestinationUrl')->with($destination_url); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code); $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type); $payment_line_item = $this->getMock(PaymentLineItemInterface::class); $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id, $plugin_configuration)->willReturn($payment_line_item); $this->paymentStorage->expects($this->once())->method('create')->with(['bundle' => 'payment_form'])->willReturn($payment); $this->request->expects($this->atLeastOnce())->method('getUri')->willReturn($destination_url); $form = ['#foo' => $this->randomMachineName()]; $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment, 'payment_form')->willReturn($form); $this->assertSame($form, $this->sut->viewElements($items, 'en')); }
/** * Tests the building of a full page variant. * * @covers ::build * @covers ::getRegionAssignments * * @dataProvider providerBuild */ public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) { $theme = $this->randomMachineName(); $display_variant = $this->setUpDisplayVariant(); $this->themeNegotiator->expects($this->any())->method('determineActiveTheme')->with($this->routeMatch)->will($this->returnValue($theme)); $display_variant->expects($this->once())->method('getRegionNames')->will($this->returnValue(array('top' => 'Top', 'center' => 'Center', 'bottom' => 'Bottom'))); $display_variant->setMainContent(['#markup' => 'Hello kittens!']); $blocks = array(); $block_plugin = $this->getMock('Drupal\\Core\\Block\\BlockPluginInterface'); $main_content_block_plugin = $this->getMock('Drupal\\Core\\Block\\MainContentBlockPluginInterface'); foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->once())->method('access')->will($this->returnValue($block_config[0])); $block->expects($this->any())->method('get')->will($this->returnValueMap(array(array('region', $block_config[1]), array('weight', $block_config[2]), array('status', TRUE)))); $block->expects($this->any())->method('getPlugin')->willReturn($block_config[3] ? $main_content_block_plugin : $block_plugin); $blocks[$block_id] = $block; } $this->blockViewBuilder->expects($this->exactly($visible_block_count))->method('view')->will($this->returnValue(array())); $this->blockStorage->expects($this->once())->method('loadByProperties')->with(array('theme' => $theme))->will($this->returnValue($blocks)); $this->assertSame($expected_render_array, $display_variant->build()); }
/** * @covers ::getCurrencyLocaleOptions */ public function testCurrencyLocaleOptionsWithLimitation() { $this->currencyStorage->expects($this->never())->method('loadMultiple'); $currency_locale_id_a = $this->randomMachineName(); $currency_locale_label_a = $this->randomMachineName(); $currency_locale_a = $this->getMock(CurrencyLocaleInterface::class); $currency_locale_a->expects($this->atLeastOnce())->method('id')->willReturn($currency_locale_id_a); $currency_locale_a->expects($this->atLeastOnce())->method('label')->willReturn($currency_locale_label_a); $currency_locale_a->expects($this->atLeastOnce())->method('status')->willReturn(TRUE); $currency_locale_b = $this->getMock(CurrencyLocaleInterface::class); $currency_locale_b->expects($this->atLeastOnce())->method('status')->willReturn(FALSE); $currency_locale_id_c = $this->randomMachineName(); $currency_locale_label_c = $this->randomMachineName(); $currency_locale_c = $this->getMock(CurrencyLocaleInterface::class); $currency_locale_c->expects($this->atLeastOnce())->method('id')->willReturn($currency_locale_id_c); $currency_locale_c->expects($this->atLeastOnce())->method('label')->willReturn($currency_locale_label_c); $currency_locale_c->expects($this->atLeastOnce())->method('status')->willReturn(TRUE); $expected_options = [$currency_locale_id_a => $currency_locale_label_a, $currency_locale_id_c => $currency_locale_label_c]; natcasesort($expected_options); $this->assertSame($expected_options, $this->sut->getCurrencyLocaleOptions([$currency_locale_a, $currency_locale_b, $currency_locale_c])); }