public function ShipmentManagerTest::testPromotions in Commerce Shipping 8.2
Tests the applying of display-inclusive promotions.
@covers ::calculateRates
File
- tests/
src/ Kernel/ ShipmentManagerTest.php, line 248
Class
- ShipmentManagerTest
- Tests the shipment manager.
Namespace
Drupal\Tests\commerce_shipping\KernelCode
public function testPromotions() {
$first_promotion = Promotion::create([
'name' => 'Promotion 1',
'order_types' => [
$this->order
->bundle(),
],
'stores' => [
$this->store
->id(),
],
'offer' => [
'target_plugin_id' => 'shipment_fixed_amount_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'filter' => 'include',
'shipping_methods' => [
[
'shipping_method' => $this->shippingMethods[0]
->uuid(),
],
],
'amount' => [
'number' => '1.00',
'currency_code' => 'USD',
],
],
],
'status' => TRUE,
]);
$first_promotion
->save();
$second_promotion = Promotion::create([
'name' => 'Promotion 1',
'order_types' => [
$this->order
->bundle(),
],
'stores' => [
$this->store
->id(),
],
'offer' => [
'target_plugin_id' => 'shipment_percentage_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'filter' => 'include',
'shipping_methods' => [
[
'shipping_method' => $this->shippingMethods[1]
->uuid(),
],
],
'percentage' => '0.5',
],
],
'status' => TRUE,
]);
$second_promotion
->save();
$coupon = Coupon::create([
'promotion_id' => $second_promotion
->id(),
'code' => '50% off shipping',
'status' => TRUE,
]);
$coupon
->save();
$this->order
->set('coupons', [
$coupon,
]);
$this->order
->setRefreshState(Order::REFRESH_SKIP);
$this->order
->save();
$rates = $this->shipmentManager
->calculateRates($this->shipment);
$this
->assertCount(2, $rates);
$first_rate = reset($rates);
$second_rate = end($rates);
// The first rate should be reduced by the 50% off coupon.
$this
->assertArrayHasKey($first_rate
->getId(), $rates);
$this
->assertEquals('2', $first_rate
->getShippingMethodId());
$this
->assertEquals('default', $first_rate
->getService()
->getId());
$this
->assertEquals('Overnight shipping', $first_rate
->getService()
->getLabel());
$this
->assertEquals(new Price('20.00', 'USD'), $first_rate
->getOriginalAmount());
$this
->assertEquals(new Price('10.00', 'USD'), $first_rate
->getAmount());
// The second rate should be reduced by the $1 off promotion.
$this
->assertArrayHasKey($second_rate
->getId(), $rates);
$this
->assertEquals('1', $second_rate
->getShippingMethodId());
$this
->assertEquals('default', $second_rate
->getService()
->getId());
$this
->assertEquals('Standard shipping', $second_rate
->getService()
->getLabel());
$this
->assertEquals(new Price('5.00', 'USD'), $second_rate
->getOriginalAmount());
$this
->assertEquals(new Price('4.00', 'USD'), $second_rate
->getAmount());
// Test applying a combination offer.
$second_promotion
->set('offer', [
'target_plugin_id' => 'combination_offer',
'target_plugin_configuration' => [
'offers' => [
[
'target_plugin_id' => 'shipment_percentage_off',
'target_plugin_configuration' => [
'display_inclusive' => TRUE,
'filter' => 'include',
'shipping_methods' => [
[
'shipping_method' => $this->shippingMethods[1]
->uuid(),
],
],
'percentage' => '0.2',
],
],
],
],
]);
$second_promotion
->save();
$rates = $this->shipmentManager
->calculateRates($this->shipment);
$this
->assertCount(2, $rates);
$first_rate = reset($rates);
// The first rate should be reduced by the 20% off coupon.
$this
->assertArrayHasKey($first_rate
->getId(), $rates);
$this
->assertEquals('2', $first_rate
->getShippingMethodId());
$this
->assertEquals('default', $first_rate
->getService()
->getId());
$this
->assertEquals('Overnight shipping', $first_rate
->getService()
->getLabel());
$this
->assertEquals(new Price('20.00', 'USD'), $first_rate
->getOriginalAmount());
$this
->assertEquals(new Price('16.00', 'USD'), $first_rate
->getAmount());
}