View source
<?php
namespace Drupal\Tests\commerce_currency_resolver\Functional;
use Drupal\commerce_exchanger\Entity\ExchangeRates;
use Drupal\commerce_order\Entity\Order;
use Drupal\Tests\commerce_cart\Functional\CartBrowserTestBase;
use Drupal\Tests\commerce_currency_resolver\Traits\CurrentCurrencyTrait;
class CurrentCurrencyCartTest extends CartBrowserTestBase {
use CurrentCurrencyTrait;
protected $currentCurrency;
public static $modules = [
'commerce_exchanger',
'commerce_currency_resolver',
'commerce_test',
];
protected function setUp() {
parent::setUp();
$currency_importer = $this->container
->get('commerce_price.currency_importer');
$currency_importer
->import('HRK');
$exchange_rates = ExchangeRates::create([
'id' => 'testing',
'label' => 'Manual',
'plugin' => 'manual',
'status' => TRUE,
'configuration' => [
'cron' => FALSE,
'use_cross_sync' => FALSE,
'demo_amount' => 100,
'base_currency' => 'USD',
'mode' => 'live',
],
]);
$exchange_rates
->save();
$this
->config($exchange_rates
->getExchangerConfigName())
->setData([
'rates' => [
'HRK' => [
'USD' => [
'value' => 0.15,
'sync' => 0,
],
],
'USD' => [
'HRK' => [
'value' => 6.85,
'sync' => 0,
],
],
],
])
->save();
$this
->config('commerce_currency_resolver.settings')
->set('currency_exchange_rates', 'testing')
->save();
$this->store
->setDefaultCurrencyCode('HRK');
$this->store
->save();
$this
->reloadEntity($this->store);
$this->currentCurrency = $this->container
->get('commerce_currency_resolver.current_currency');
$variation_display = commerce_get_entity_display('commerce_product_variation', 'default', 'view');
$variation_display
->setComponent('price', [
'label' => 'above',
'type' => 'commerce_price_calculated',
'settings' => [],
]);
$variation_display
->save();
}
public function testProductAddToCartForm() {
$this
->assertEqual('USD', $this->variation
->getPrice()
->getCurrencyCode());
$this
->assertEqual('999', $this->variation
->getPrice()
->getNumber());
$this
->assertEqual('HRK', $this->currentCurrency
->getCurrency());
$this
->postAddToCart($this->variation
->getProduct());
$this
->assertSession()
->pageTextContains('HRK6,843.15');
$this->cart = Order::load($this->cart
->id());
$this
->drupalGet('cart');
$this
->assertEqual(999 * 6.85, $this->cart
->getTotalPrice()
->getNumber());
$this
->assertEqual('HRK', $this->cart
->getTotalPrice()
->getCurrencyCode());
$this
->drupalGet('product/1');
$this
->assertSession()
->pageTextContains('HRK6,843.15');
$this
->assertEqual('HRK', $this->currentCurrency
->getCurrency());
$this->store
->setDefaultCurrencyCode('USD');
$this->store
->save();
$this
->reloadEntity($this->store);
$this
->resetCurrencyContainer();
$this
->assertEqual('USD', $this->currentCurrency
->getCurrency());
$this
->postAddToCart($this->variation
->getProduct());
$order = Order::load($this->cart
->id());
$this
->drupalGet('cart');
$this
->assertEqual(2 * 999, $order
->getTotalPrice()
->getNumber());
$this
->assertEqual('USD', $order
->getTotalPrice()
->getCurrencyCode());
}
}