예제 #1
0
 /**
  * {@inheritdoc}
  */
 function getValueOptions()
 {
     if (is_null($this->valueOptions)) {
         $this->valueOptions = $this->formHelper->getCurrencyOptions();
     }
     return $this->valueOptions;
 }
 /**
  * @covers ::getValueOptions
  */
 public function testGetValueOptions()
 {
     $options = array($this->randomMachineName() => $this->randomMachineName());
     $this->formHelper->expects($this->atLeastOnce())->method('getCurrencyOptions')->willReturn($options);
     $method = new \ReflectionMethod($this->sut, 'getValueOptions');
     $method->setAccessible(TRUE);
     $this->assertSame($options, $method->invoke($this->sut));
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $currency_locales = $this->configImporter->getImportableCurrencyLocales();
     if (empty($currency_locales)) {
         $form['message'] = ['#markup' => $this->t('All currency locales have been imported already.')];
     } else {
         $form['locale'] = ['#options' => $this->formHelper->getCurrencyLocaleOptions($currency_locales), '#required' => TRUE, '#title' => $this->t('Currency locale'), '#type' => 'select'];
         $form['actions'] = ['#type' => 'actions'];
         $form['actions']['import'] = ['#dropbutton' => 'submit', '#name' => 'import', '#type' => 'submit', '#value' => $this->t('Import')];
         $form['actions']['import_edit'] = ['#dropbutton' => 'submit', '#name' => 'import_edit', '#type' => 'submit', '#value' => $this->t('Import and edit')];
     }
     return $form;
 }
 /**
  * @covers ::buildForm
  *
  * @dataProvider providerTestBuildForm
  */
 public function testBuildForm($rate_rate)
 {
     $currency_code_from = $this->randomMachineName();
     $currency_code_to = $this->randomMachineName();
     $rate = NULL;
     if (!is_null($rate_rate)) {
         $rate = $this->getMock(ExchangeRateInterface::class);
         $rate->expects($this->once())->method('getRate')->willReturn($rate_rate);
     }
     $plugin = $this->getMock(ExchangeRateProviderInterface::class);
     $plugin->expects($this->once())->method('load')->with($currency_code_from, $currency_code_to)->willReturn($rate);
     $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with('currency_fixed_rates')->willReturn($plugin);
     $currency_options = array('XXX' => $this->randomMachineName(), $this->randomMachineName() => $this->randomMachineName());
     $this->formHelper->expects($this->once())->method('getCurrencyOptions')->willReturn($currency_options);
     unset($currency_options['XXX']);
     $form = array();
     $form_state = $this->getMock(FormStateInterface::class);
     $build = $this->sut->buildForm($form, $form_state, $currency_code_from, $currency_code_to);
     $expected_build['currency_code_from'] = array('#default_value' => $currency_code_from, '#disabled' => !is_null($rate_rate), '#empty_value' => '', '#options' => $currency_options, '#required' => TRUE, '#type' => 'select');
     unset($build['currency_code_from']['#title']);
     $expected_build['currency_code_to'] = array('#default_value' => $currency_code_to, '#disabled' => !is_null($rate_rate), '#empty_value' => '', '#options' => $currency_options, '#required' => TRUE, '#type' => 'select');
     unset($build['currency_code_to']['#title']);
     $expected_build['rate'] = array('#limit_currency_codes' => array($currency_code_to), '#default_value' => array('amount' => $rate_rate, 'currency_code' => $currency_code_to), '#required' => TRUE, '#type' => 'currency_amount');
     unset($build['rate']['#title']);
     $expected_build['actions'] = array('#type' => 'actions');
     $expected_build['actions']['save'] = array('#button_type' => 'primary', '#name' => 'save', '#type' => 'submit');
     unset($build['actions']['save']['#value']);
     if (!is_null($rate_rate)) {
         $expected_build['actions']['delete'] = array('#button_type' => 'danger', '#limit_validation_errors' => array(array('currency_code_from'), array('currency_code_to')), '#name' => 'delete', '#type' => 'submit');
         unset($build['actions']['delete']['#value']);
     }
     $this->assertSame($expected_build, $build);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $currency_code_from = 'XXX', $currency_code_to = 'XXX')
 {
     $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates');
     $rate = $plugin->load($currency_code_from, $currency_code_to);
     $options = $this->formHelper->getCurrencyOptions();
     unset($options['XXX']);
     $form['currency_code_from'] = array('#default_value' => $currency_code_from, '#disabled' => !is_null($rate), '#empty_value' => '', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Source currency'), '#type' => 'select');
     $form['currency_code_to'] = array('#default_value' => $currency_code_to, '#disabled' => !is_null($rate), '#empty_value' => '', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Destination currency'), '#type' => 'select');
     $form['rate'] = array('#limit_currency_codes' => array($currency_code_to), '#default_value' => array('amount' => !is_null($rate) ? $rate->getRate() : NULL, 'currency_code' => $currency_code_to), '#required' => TRUE, '#title' => $this->t('Exchange rate'), '#type' => 'currency_amount');
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['save'] = array('#button_type' => 'primary', '#name' => 'save', '#type' => 'submit', '#value' => $this->t('Save'));
     if (!is_null($rate)) {
         $form['actions']['delete'] = array('#button_type' => 'danger', '#limit_validation_errors' => array(array('currency_code_from'), array('currency_code_to')), '#name' => 'delete', '#type' => 'submit', '#value' => $this->t('Delete'));
     }
     return $form;
 }
 /**
  * @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));
 }
 /**
  * Implements form #process callback.
  */
 public function process(array $element, FormStateInterface $form_state, array $form)
 {
     // Validate element configuration.
     if ($element['#minimum_amount'] !== FALSE && !is_numeric($element['#minimum_amount'])) {
         throw new \InvalidArgumentException('The minimum amount must be a number.');
     }
     if ($element['#maximum_amount'] !== FALSE && !is_numeric($element['#maximum_amount'])) {
         throw new \InvalidArgumentException('The maximum amount must be a number.');
     }
     if (!is_array($element['#limit_currency_codes'])) {
         throw new \InvalidArgumentException('#limit_currency_codes must be an array.');
     }
     if ($element['#limit_currency_codes'] && $element['#default_value']['currency_code'] && !in_array($element['#default_value']['currency_code'], $element['#limit_currency_codes'])) {
         throw new \InvalidArgumentException('The default currency is not in the list of allowed currencies.');
     }
     // Load the default currency.
     /** @var \Drupal\currency\Entity\CurrencyInterface $currency */
     $currency = NULL;
     if ($element['#default_value']['currency_code']) {
         $currency = $this->currencyStorage->load($element['#default_value']['currency_code']);
     }
     if (!$currency) {
         $currency = $this->currencyStorage->load('XXX');
     }
     // Modify the element.
     $element['#tree'] = TRUE;
     $element['#theme_wrappers'][] = 'form_element';
     $element['#attached']['library'] = ['currency/element.currency_amount'];
     // Add the currency element.
     if (count($element['#limit_currency_codes']) == 1) {
         $element['currency_code'] = ['#value' => reset($element['#limit_currency_codes']), '#type' => 'value'];
     } else {
         $element['currency_code'] = ['#default_value' => $currency->id(), '#type' => 'select', '#title' => $this->t('Currency'), '#title_display' => 'invisible', '#options' => $element['#limit_currency_codes'] ? array_intersect_key($this->formHelper->getCurrencyOptions(), array_flip($element['#limit_currency_codes'])) : $this->formHelper->getCurrencyOptions(), '#required' => $element['#required']];
     }
     // Add the amount element.
     $description = NULL;
     if ($element['#minimum_amount'] !== FALSE) {
         $description = $this->t('The minimum amount is @amount.', ['@amount' => $currency->formatAmount($element['#minimum_amount'])]);
     }
     $element['amount'] = ['#default_value' => $element['#default_value']['amount'], '#type' => 'textfield', '#title' => $this->t('Amount'), '#title_display' => 'invisible', '#description' => $description, '#prefix' => count($element['#limit_currency_codes']) == 1 ? $currency->getSign() : NULL, '#required' => $element['#required'], '#size' => 9];
     return $element;
 }