/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $definitions = $this->currencyExchangeRateProviderManager->getDefinitions();
     $configuration = $this->exchangeRateProvider->loadConfiguration();
     $form['exchange_rate_providers'] = array('#header' => array($this->t('Title'), $this->t('Enabled'), $this->t('Weight'), $this->t('Operations')), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'form-select')), '#type' => 'table');
     $weight = 0;
     foreach ($configuration as $plugin_id => $enabled) {
         $weight++;
         $plugin_definition = $definitions[$plugin_id];
         $form['exchange_rate_providers'][$plugin_id] = array('#attributes' => array('class' => array('draggable')), '#weight' => $weight);
         $form['exchange_rate_providers'][$plugin_id]['label'] = array('#description' => $plugin_definition['description'], '#markup' => $plugin_definition['label'], '#title' => $this->t('Title'), '#title_display' => 'invisible', '#type' => 'item');
         $form['exchange_rate_providers'][$plugin_id]['enabled'] = array('#default_value' => $enabled, '#title' => $this->t('Enabled'), '#title_display' => 'invisible', '#type' => 'checkbox');
         $form['exchange_rate_providers'][$plugin_id]['weight'] = array('#default_value' => $weight, '#title' => $this->t('Weight'), '#title_display' => 'invisible', '#type' => 'weight');
         $operations_provider = $this->currencyExchangeRateProviderManager->getOperationsProvider($plugin_id);
         $form['exchange_rate_providers'][$plugin_id]['operations'] = array('#links' => $operations_provider ? $operations_provider->getOperations($plugin_id) : [], '#title' => $this->t('Operations'), '#type' => 'operations');
     }
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['save'] = array('#button_type' => 'primary', '#type' => 'submit', '#value' => $this->t('Save'));
     return $form;
 }
 /**
  * Views the configured fixed rates.
  *
  * @return array
  *   A renderable array.
  */
 public function overview()
 {
     /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */
     $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates');
     $rates = $plugin->loadALl();
     $form['rates'] = array('#empty' => $this->t('There are no exchange rates yet. <a href="@path">Add an exchange rate</a>.', array('@path' => $this->urlGenerator->generateFromRoute('currency.exchange_rate_provider.fixed_rates.add'))), '#header' => array($this->t('From'), $this->t('To'), $this->t('Exchange rate'), $this->t('Operations')), '#type' => 'table');
     foreach ($rates as $currency_code_from => $currency_codes_to) {
         foreach ($currency_codes_to as $currency_code_to => $rate) {
             $currency_from = $this->currencyStorage->load($currency_code_from);
             $currency_to = $this->currencyStorage->load($currency_code_to);
             if ($currency_from && $currency_to) {
                 $row['currency_from'] = array('#markup' => $currency_from->label(), '#type' => 'item');
                 $row['currency_to'] = array('#markup' => $currency_to->label(), '#type' => 'item');
                 $row['rate'] = array('#markup' => $this->currencyAmountFormatterManager->getDefaultPlugin()->formatAmount($currency_to, $rate), '#type' => 'item');
                 $row['operations'] = array('#links' => array(array('title' => $this->t('edit'), 'route_name' => 'currency.exchange_rate_provider.fixed_rates.edit', 'route_parameters' => array('currency_code_from' => $currency_code_from, 'currency_code_to' => $currency_code_to))), '#type' => 'operations');
                 $form['rates'][] = $row;
             }
         }
     }
     return $form;
 }
 /**
  * @covers ::buildForm
  */
 public function testBuildForm()
 {
     $plugin_id_a = $this->randomMachineName();
     $plugin_id_b = $this->randomMachineName();
     $plugin_id_c = $this->randomMachineName();
     $plugin_definitions = [$plugin_id_a => ['description' => NULL, 'label' => $this->randomMachineName(), 'operations' => []], $plugin_id_b => ['description' => $this->randomMachineName(), 'label' => $this->randomMachineName(), 'operations' => []], $plugin_id_c => ['description' => $this->randomMachineName(), 'label' => $this->randomMachineName(), 'operations' => [['href' => $this->randomMachineName(), 'title' => $this->randomMachineName()]]]];
     $configuration = [$plugin_id_a => TRUE, $plugin_id_b => FALSE, $plugin_id_c => TRUE];
     $this->exchangeRateProvider->expects($this->atLeastOnce())->method('loadConfiguration')->willReturn($configuration);
     $this->currencyExchangeRateProviderManager->expects($this->atLeastOnce())->method('getDefinitions')->willReturn($plugin_definitions);
     $form = [];
     $form_state = new FormState();
     $build = $this->sut->buildForm($form, $form_state);
     foreach ([$plugin_id_a, $plugin_id_b, $plugin_id_c] as $weight => $plugin_id) {
         $this->assertInternalType('array', $build['exchange_rate_providers'][$plugin_id]['weight']);
         $this->assertInternalType('array', $build['exchange_rate_providers'][$plugin_id]['label']);
         $this->assertSame($plugin_definitions[$plugin_id]['label'], $build['exchange_rate_providers'][$plugin_id]['label']['#markup']);
         $this->assertInternalType('array', $build['exchange_rate_providers'][$plugin_id]['weight']);
         $this->assertSame($weight + 1, $build['exchange_rate_providers'][$plugin_id]['weight']['#default_value']);
     }
     $this->assertInternalType('array', $build['actions']);
 }
 /**
  * @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);
 }
 /**
  * @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);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */
     $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates');
     $values = $form_state->getValues();
     $currency_code_from = $values['currency_code_from'];
     $currency_code_to = $values['currency_code_to'];
     $currency_from = $this->currencyStorage->load($currency_code_from);
     $currency_to = $this->currencyStorage->load($currency_code_to);
     $triggering_element = $form_state->getTriggeringElement();
     switch ($triggering_element['#name']) {
         case 'save':
             $plugin->save($currency_code_from, $currency_code_to, $values['rate']['amount']);
             drupal_set_message($this->t('The exchange rate for @currency_title_from to @currency_title_to has been saved.', array('@currency_title_from' => $currency_from->label(), '@currency_title_to' => $currency_to->label())));
             break;
         case 'delete':
             $plugin->delete($currency_code_from, $currency_code_to);
             drupal_set_message($this->t('The exchange rate for @currency_title_from to @currency_title_to has been deleted.', array('@currency_title_from' => $currency_from->label(), '@currency_title_to' => $currency_to->label())));
             break;
     }
     $form_state->setRedirect('currency.exchange_rate_provider.fixed_rates.overview');
 }