Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2831954 by bojanz, GoZ, vasike: Add test coverage for multicurrency. #626

Open
wants to merge 3 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ services:
class: Drupal\commerce_price_test\TestPriceResolver
tags:
- { name: commerce_price.price_resolver, priority: 100 }
commerce_test.test_multicurrency_price_resolver:
class: Drupal\commerce_price_test\TestMulticurrencyPriceResolver
arguments: ['@language_manager']
tags:
- { name: commerce_price.price_resolver, priority: 200 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\commerce_price_test;

use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_price\Resolver\PriceResolverInterface;
use Drupal\Core\Language\LanguageManagerInterface;

/**
* Test multicurrency price resolver.
*/
class TestMulticurrencyPriceResolver implements PriceResolverInterface {

/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* Constructs a new CommerceMulticurrencyResolver object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(LanguageManagerInterface $language_manager) {
$this->languageManager = $language_manager;
}

/**
* {@inheritdoc}
*/
public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
// Define mapping between language and currency.
$currency_by_language = ['fr' => 'EUR'];

// Get current language.
$language = $this->languageManager->getCurrentLanguage()->getId();

// Get value from currency price field, if exists.
if (isset($currency_by_language[$language]) && $entity->hasField('price_' . strtolower($currency_by_language[$language]))) {
$price = $entity->get('price_' . strtolower($currency_by_language[$language]))->getValue();
$price = reset($price);

return new Price($price['number'], $price['currency_code']);
}
}

}
124 changes: 124 additions & 0 deletions modules/price/tests/src/Kernel/PriceMulticurrencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Drupal\Tests\commerce_price\Kernel;

use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_store\StoreCreationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;

/**
* Tests price for multicurrency.
*
* @group commerce
*/
class PriceMulticurrencyTest extends CommerceKernelTestBase {

use StoreCreationTrait;

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'language',
'path',
'commerce_price_test',
'commerce_product',
];

/**
* @var \Drupal\commerce_product\Entity\ProductVariationInterface
*/
protected $variation;

/**
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $productVariationDefaultDisplay;

/**
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
*/
protected $productVariationViewBuilder;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

// Add FR language.
ConfigurableLanguage::createFromLangcode('fr')->save();
// Import EUR currency.
$this->container->get('commerce_price.currency_importer')->import('EUR');

$this->installEntitySchema('commerce_product_attribute');
$this->installEntitySchema('commerce_product_attribute_value');
$this->installEntitySchema('commerce_product_variation');
$this->installEntitySchema('commerce_product_variation_type');
$this->installEntitySchema('commerce_product');
$this->installEntitySchema('commerce_product_type');
$this->installConfig(['commerce_product']);

$this->productVariationDefaultDisplay = commerce_get_entity_display('commerce_product_variation', 'default', 'view');
$this->productVariationViewBuilder = $this->container->get('entity_type.manager')->getViewBuilder('commerce_product_variation');

// Create extra Price EUR field.
$field_storage = FieldStorageConfig::create([
'field_name' => 'price_eur',
'entity_type' => 'commerce_product_variation',
'type' => 'commerce_price',
'cardinality' => 1,
]);
$field_storage->save();

$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'default',
'label' => 'Price EUR',
'required' => TRUE,
'translatable' => FALSE,
]);
$field->save();

// Create product variation.
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this->randomMachineName()),
'price' => new Price('12.00', 'USD'),
'price_eur' => new Price('10.00', 'EUR'),
]);
$variation->save();
$this->variation = $variation;
}

/**
* Tests the multicurrency price.
*/
public function testPriceMulticurrency() {
// Set the calculated price formatter which use the price resolver.
$this->productVariationDefaultDisplay->setComponent('price', [
'type' => 'commerce_price_calculated',
'settings' => [],
]);
$this->productVariationDefaultDisplay->save();

// Check the default price.
$build = $this->productVariationViewBuilder->viewField($this->variation->price, 'default');
$this->render($build);
$this->assertText('$12.00');

// Change the language to 'fr'.
\Drupal::configFactory()->getEditable('system.site')->set('default_langcode', 'fr')->save();
// Check the price for 'fr' language.
$build = $this->productVariationViewBuilder->viewField($this->variation->price, 'default');
$this->render($build);
$this->assertText('€10.00');
}

}