-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2831954 by bojanz, GoZ, vasike: Add test coverage for multicur…
…rency.
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
modules/price/tests/modules/commerce_price_test/src/TestMulticurrencyPriceResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
124
modules/price/tests/src/Kernel/PriceMulticurrencyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |