public function ProductVariationStorageTest::testLoadFromContext in Commerce Core 8.2
Tests loadFromContext() method.
File
- modules/
product/ tests/ src/ Kernel/ ProductVariationStorageTest.php, line 104
Class
- ProductVariationStorageTest
- Tests the product variation storage.
Namespace
Drupal\Tests\commerce_product\KernelCode
public function testLoadFromContext() {
$variations = [];
for ($i = 1; $i <= 3; $i++) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
]);
$variation
->save();
$variations[] = $variation;
}
$variations = array_reverse($variations);
$product = Product::create([
'type' => 'default',
'variations' => $variations,
]);
$product
->save();
$request = Request::create('');
$request->query
->add([
'v' => end($variations)
->id(),
]);
// Push the request to the request stack so `current_route_match` works.
$this->container
->get('request_stack')
->push($request);
$this
->assertNotEquals($request->query
->get('v'), $product
->getDefaultVariation()
->id());
$context_variation = $this->variationStorage
->loadFromContext($product);
$this
->assertEquals($request->query
->get('v'), $context_variation
->id());
// Invalid variation ID returns default variation.
$request = Request::create('');
$request->query
->add([
'v' => '1111111',
]);
// Push the request to the request stack so `current_route_match` works.
$this->container
->get('request_stack')
->push($request);
$this
->assertNotEquals($request->query
->get('v'), $product
->getDefaultVariation()
->id());
$context_variation = $this->variationStorage
->loadFromContext($product);
$this
->assertEquals($product
->getDefaultVariation()
->id(), $context_variation
->id());
}