Skip to content

Commit c15564f

Browse files
committed
Issue #2867019 part II: Fix the build instability.
1 parent 9131c8f commit c15564f

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

modules/promotion/src/Entity/Promotion.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
643643
* The default value (date string).
644644
*/
645645
public static function getDefaultStartDate() {
646-
return gmdate('Y-m-d');
646+
$timestamp = \Drupal::time()->getRequestTime();
647+
return gmdate('Y-m-d', $timestamp);
647648
}
648649

649650
/**
@@ -656,7 +657,8 @@ public static function getDefaultStartDate() {
656657
*/
657658
public static function getDefaultEndDate() {
658659
// Today + 1 year.
659-
return gmdate('Y-m-d', time() + 31536000);
660+
$timestamp = \Drupal::time()->getRequestTime();
661+
return gmdate('Y-m-d', $timestamp + 31536000);
660662
}
661663

662664
/**

modules/promotion/src/PromotionStorage.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\commerce\CommerceContentEntityStorage;
66
use Drupal\commerce_order\Entity\OrderTypeInterface;
77
use Drupal\commerce_store\Entity\StoreInterface;
8+
use Drupal\Component\Datetime\TimeInterface;
89
use Drupal\Core\Cache\CacheBackendInterface;
910
use Drupal\Core\Database\Connection;
1011
use Drupal\Core\Entity\EntityManagerInterface;
@@ -25,6 +26,13 @@ class PromotionStorage extends CommerceContentEntityStorage implements Promotion
2526
*/
2627
protected $usage;
2728

29+
/**
30+
* The time.
31+
*
32+
* @var \Drupal\Component\Datetime\TimeInterface
33+
*/
34+
protected $time;
35+
2836
/**
2937
* Constructs a new PromotionStorage object.
3038
*
@@ -42,11 +50,14 @@ class PromotionStorage extends CommerceContentEntityStorage implements Promotion
4250
* The event dispatcher.
4351
* @param \Drupal\commerce_promotion\PromotionUsageInterface $usage
4452
* The usage.
53+
* @param \Drupal\Component\Datetime\TimeInterface $time
54+
* The time.
4555
*/
46-
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage) {
56+
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage, TimeInterface $time) {
4757
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager, $event_dispatcher);
4858

4959
$this->usage = $usage;
60+
$this->time = $time;
5061
}
5162

5263
/**
@@ -60,22 +71,24 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
6071
$container->get('cache.entity'),
6172
$container->get('language_manager'),
6273
$container->get('event_dispatcher'),
63-
$container->get('commerce_promotion.usage')
74+
$container->get('commerce_promotion.usage'),
75+
$container->get('datetime.time')
6476
);
6577
}
6678

6779
/**
6880
* {@inheritdoc}
6981
*/
7082
public function loadAvailable(OrderTypeInterface $order_type, StoreInterface $store) {
83+
$today = gmdate('Y-m-d', $this->time->getRequestTime());
7184
$query = $this->getQuery();
7285
$or_condition = $query->orConditionGroup()
73-
->condition('end_date', gmdate('Y-m-d'), '>=')
74-
->notExists('end_date', gmdate('Y-m-d'));
86+
->condition('end_date', $today, '>=')
87+
->notExists('end_date', $today);
7588
$query
7689
->condition('stores', [$store->id()], 'IN')
7790
->condition('order_types', [$order_type->id()], 'IN')
78-
->condition('start_date', gmdate('Y-m-d'), '<=')
91+
->condition('start_date', $today, '<=')
7992
->condition('status', TRUE)
8093
->condition($or_condition);
8194
// Only load promotions without coupons. Promotions with coupons are loaded

modules/promotion/tests/src/Functional/CouponRedemptionTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ protected function setUp() {
8686
'name' => 'Promotion (with coupon)',
8787
'order_types' => ['default'],
8888
'stores' => [$this->store->id()],
89-
'status' => TRUE,
9089
'offer' => [
9190
'target_plugin_id' => 'commerce_promotion_order_percentage_off',
9291
'target_plugin_configuration' => [
9392
'amount' => '0.10',
9493
],
9594
],
9695
'conditions' => [],
96+
'start_date' => '2017-01-01',
97+
'status' => TRUE,
9798
]);
9899

99100
$coupon = $this->createEntity('commerce_promotion_coupon', [
@@ -128,14 +129,17 @@ public function testCouponRedemption() {
128129
$this->getSession()->getPage()->pressButton('Apply');
129130

130131
$this->assertSession()->pageTextContains('Coupon applied');
131-
$this->assertSession()->elementTextContains('css', '.order-total-line', 'Discount');
132+
// The view is processed before the coupon element, so it
133+
// won't reflect the updated order until the page reloads.
134+
$this->drupalGet(Url::fromRoute('commerce_cart.page'));
132135
$this->assertSession()->pageTextContains('-$99.90');
133136

134137
$this->assertSession()->fieldNotExists('coupons[code]');
135138
$this->assertSession()->buttonNotExists('Apply');
136139
$this->getSession()->getPage()->pressButton('Remove promotion');
137140

138-
$this->assertSession()->elementTextNotContains('css', '.order-total-line', 'Discount');
141+
$this->drupalGet(Url::fromRoute('commerce_cart.page'));
142+
$this->assertSession()->pageTextNotContains('-$99.90');
139143
$this->assertSession()->fieldExists('coupons[code]');
140144
$this->assertSession()->buttonExists('Apply');
141145
}

0 commit comments

Comments
 (0)