| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +namespace Drupal\Tests\commerce_price\Kernel;  | 
 | 4 | + | 
 | 5 | +use Drupal\commerce_price\Price;  | 
 | 6 | +use Drupal\commerce_product\Entity\ProductVariation;  | 
 | 7 | +use Drupal\commerce_store\StoreCreationTrait;  | 
 | 8 | +use Drupal\field\Entity\FieldConfig;  | 
 | 9 | +use Drupal\field\Entity\FieldStorageConfig;  | 
 | 10 | +use Drupal\language\Entity\ConfigurableLanguage;  | 
 | 11 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;  | 
 | 12 | + | 
 | 13 | +/**  | 
 | 14 | + * Tests price for multicurrency.  | 
 | 15 | + *  | 
 | 16 | + * @group commerce  | 
 | 17 | + */  | 
 | 18 | +class PriceMulticurrencyTest extends CommerceKernelTestBase {  | 
 | 19 | + | 
 | 20 | +  use StoreCreationTrait;  | 
 | 21 | + | 
 | 22 | +  /**  | 
 | 23 | +   * Modules to enable.  | 
 | 24 | +   *  | 
 | 25 | +   * @var array  | 
 | 26 | +   */  | 
 | 27 | +  public static $modules = [  | 
 | 28 | +    'language',  | 
 | 29 | +    'path',  | 
 | 30 | +    'commerce_price_test',  | 
 | 31 | +    'commerce_product',  | 
 | 32 | +  ];  | 
 | 33 | + | 
 | 34 | +  /**  | 
 | 35 | +   * @var \Drupal\commerce_product\Entity\ProductVariationInterface  | 
 | 36 | +   */  | 
 | 37 | +  protected $variation;  | 
 | 38 | + | 
 | 39 | +  /**  | 
 | 40 | +   * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface  | 
 | 41 | +   */  | 
 | 42 | +  protected $productVariationDefaultDisplay;  | 
 | 43 | + | 
 | 44 | +  /**  | 
 | 45 | +   * @var \Drupal\Core\Entity\EntityViewBuilderInterface  | 
 | 46 | +   */  | 
 | 47 | +  protected $productVariationViewBuilder;  | 
 | 48 | + | 
 | 49 | +  /**  | 
 | 50 | +   * {@inheritdoc}  | 
 | 51 | +   */  | 
 | 52 | +  protected function setUp() {  | 
 | 53 | +    parent::setUp();  | 
 | 54 | + | 
 | 55 | +    // Add FR language.  | 
 | 56 | +    ConfigurableLanguage::createFromLangcode('fr')->save();  | 
 | 57 | +    // Import EUR currency.  | 
 | 58 | +    $this->container->get('commerce_price.currency_importer')->import('EUR');  | 
 | 59 | + | 
 | 60 | +    $this->installEntitySchema('commerce_product_attribute');  | 
 | 61 | +    $this->installEntitySchema('commerce_product_attribute_value');  | 
 | 62 | +    $this->installEntitySchema('commerce_product_variation');  | 
 | 63 | +    $this->installEntitySchema('commerce_product_variation_type');  | 
 | 64 | +    $this->installEntitySchema('commerce_product');  | 
 | 65 | +    $this->installEntitySchema('commerce_product_type');  | 
 | 66 | +    $this->installConfig(['commerce_product']);  | 
 | 67 | + | 
 | 68 | +    $this->productVariationDefaultDisplay = commerce_get_entity_display('commerce_product_variation', 'default', 'view');  | 
 | 69 | +    $this->productVariationViewBuilder = $this->container->get('entity_type.manager')->getViewBuilder('commerce_product_variation');  | 
 | 70 | + | 
 | 71 | +    // Create extra Price EUR field.  | 
 | 72 | +    $field_storage = FieldStorageConfig::create([  | 
 | 73 | +      'field_name' => 'price_eur',  | 
 | 74 | +      'entity_type' => 'commerce_product_variation',  | 
 | 75 | +      'type' => 'commerce_price',  | 
 | 76 | +      'cardinality' => 1,  | 
 | 77 | +    ]);  | 
 | 78 | +    $field_storage->save();  | 
 | 79 | + | 
 | 80 | +    $field = FieldConfig::create([  | 
 | 81 | +      'field_storage' => $field_storage,  | 
 | 82 | +      'bundle' => 'default',  | 
 | 83 | +      'label' => 'Price EUR',  | 
 | 84 | +      'required' => TRUE,  | 
 | 85 | +      'translatable' => FALSE,  | 
 | 86 | +    ]);  | 
 | 87 | +    $field->save();  | 
 | 88 | + | 
 | 89 | +    // Create product variation.  | 
 | 90 | +    $variation = ProductVariation::create([  | 
 | 91 | +      'type' => 'default',  | 
 | 92 | +      'sku' => strtolower($this->randomMachineName()),  | 
 | 93 | +      'price' => new Price('12.00', 'USD'),  | 
 | 94 | +      'price_eur' => new Price('10.00', 'EUR'),  | 
 | 95 | +    ]);  | 
 | 96 | +    $variation->save();  | 
 | 97 | +    $this->variation = $variation;  | 
 | 98 | +  }  | 
 | 99 | + | 
 | 100 | +  /**  | 
 | 101 | +   * Tests the multicurrency price.  | 
 | 102 | +   */  | 
 | 103 | +  public function testPriceMulticurrency() {  | 
 | 104 | +    // Set the calculated price formatter which use the price resolver.  | 
 | 105 | +    $this->productVariationDefaultDisplay->setComponent('price', [  | 
 | 106 | +      'type' => 'commerce_price_calculated',  | 
 | 107 | +      'settings' => [],  | 
 | 108 | +    ]);  | 
 | 109 | +    $this->productVariationDefaultDisplay->save();  | 
 | 110 | + | 
 | 111 | +    // Check the default price.  | 
 | 112 | +    $build = $this->productVariationViewBuilder->viewField($this->variation->price, 'default');  | 
 | 113 | +    $this->render($build);  | 
 | 114 | +    $this->assertText('$12.00');  | 
 | 115 | + | 
 | 116 | +    // Change the language to 'fr'.  | 
 | 117 | +    \Drupal::configFactory()->getEditable('system.site')->set('default_langcode', 'fr')->save();  | 
 | 118 | +    // Check the price for 'fr' language.  | 
 | 119 | +    $build = $this->productVariationViewBuilder->viewField($this->variation->price, 'default');  | 
 | 120 | +    $this->render($build);  | 
 | 121 | +    $this->assertText('€10.00');  | 
 | 122 | +  }  | 
 | 123 | + | 
 | 124 | +}  | 
0 commit comments