-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPriceMulticurrencyTest.php
More file actions
124 lines (105 loc) · 3.61 KB
/
PriceMulticurrencyTest.php
File metadata and controls
124 lines (105 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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');
}
}