From c15564f3a3bb9760f7e8ea4c4b0ce83cf544d7dc Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Fri, 21 Apr 2017 17:26:36 +0200 Subject: [PATCH] Issue #2867019 part II: Fix the build instability. --- modules/promotion/src/Entity/Promotion.php | 6 +++-- modules/promotion/src/PromotionStorage.php | 23 +++++++++++++++---- .../src/Functional/CouponRedemptionTest.php | 10 +++++--- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/modules/promotion/src/Entity/Promotion.php b/modules/promotion/src/Entity/Promotion.php index b9a82435ff..3cd1be6947 100644 --- a/modules/promotion/src/Entity/Promotion.php +++ b/modules/promotion/src/Entity/Promotion.php @@ -643,7 +643,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { * The default value (date string). */ public static function getDefaultStartDate() { - return gmdate('Y-m-d'); + $timestamp = \Drupal::time()->getRequestTime(); + return gmdate('Y-m-d', $timestamp); } /** @@ -656,7 +657,8 @@ public static function getDefaultStartDate() { */ public static function getDefaultEndDate() { // Today + 1 year. - return gmdate('Y-m-d', time() + 31536000); + $timestamp = \Drupal::time()->getRequestTime(); + return gmdate('Y-m-d', $timestamp + 31536000); } /** diff --git a/modules/promotion/src/PromotionStorage.php b/modules/promotion/src/PromotionStorage.php index e4ad80a8c6..e56012bd04 100644 --- a/modules/promotion/src/PromotionStorage.php +++ b/modules/promotion/src/PromotionStorage.php @@ -5,6 +5,7 @@ use Drupal\commerce\CommerceContentEntityStorage; use Drupal\commerce_order\Entity\OrderTypeInterface; use Drupal\commerce_store\Entity\StoreInterface; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; @@ -25,6 +26,13 @@ class PromotionStorage extends CommerceContentEntityStorage implements Promotion */ protected $usage; + /** + * The time. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + /** * Constructs a new PromotionStorage object. * @@ -42,11 +50,14 @@ class PromotionStorage extends CommerceContentEntityStorage implements Promotion * The event dispatcher. * @param \Drupal\commerce_promotion\PromotionUsageInterface $usage * The usage. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time. */ - public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage) { + public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage, TimeInterface $time) { parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager, $event_dispatcher); $this->usage = $usage; + $this->time = $time; } /** @@ -60,7 +71,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('cache.entity'), $container->get('language_manager'), $container->get('event_dispatcher'), - $container->get('commerce_promotion.usage') + $container->get('commerce_promotion.usage'), + $container->get('datetime.time') ); } @@ -68,14 +80,15 @@ public static function createInstance(ContainerInterface $container, EntityTypeI * {@inheritdoc} */ public function loadAvailable(OrderTypeInterface $order_type, StoreInterface $store) { + $today = gmdate('Y-m-d', $this->time->getRequestTime()); $query = $this->getQuery(); $or_condition = $query->orConditionGroup() - ->condition('end_date', gmdate('Y-m-d'), '>=') - ->notExists('end_date', gmdate('Y-m-d')); + ->condition('end_date', $today, '>=') + ->notExists('end_date', $today); $query ->condition('stores', [$store->id()], 'IN') ->condition('order_types', [$order_type->id()], 'IN') - ->condition('start_date', gmdate('Y-m-d'), '<=') + ->condition('start_date', $today, '<=') ->condition('status', TRUE) ->condition($or_condition); // Only load promotions without coupons. Promotions with coupons are loaded diff --git a/modules/promotion/tests/src/Functional/CouponRedemptionTest.php b/modules/promotion/tests/src/Functional/CouponRedemptionTest.php index a4a3f19156..8d2ade2346 100644 --- a/modules/promotion/tests/src/Functional/CouponRedemptionTest.php +++ b/modules/promotion/tests/src/Functional/CouponRedemptionTest.php @@ -86,7 +86,6 @@ protected function setUp() { 'name' => 'Promotion (with coupon)', 'order_types' => ['default'], 'stores' => [$this->store->id()], - 'status' => TRUE, 'offer' => [ 'target_plugin_id' => 'commerce_promotion_order_percentage_off', 'target_plugin_configuration' => [ @@ -94,6 +93,8 @@ protected function setUp() { ], ], 'conditions' => [], + 'start_date' => '2017-01-01', + 'status' => TRUE, ]); $coupon = $this->createEntity('commerce_promotion_coupon', [ @@ -128,14 +129,17 @@ public function testCouponRedemption() { $this->getSession()->getPage()->pressButton('Apply'); $this->assertSession()->pageTextContains('Coupon applied'); - $this->assertSession()->elementTextContains('css', '.order-total-line', 'Discount'); + // The view is processed before the coupon element, so it + // won't reflect the updated order until the page reloads. + $this->drupalGet(Url::fromRoute('commerce_cart.page')); $this->assertSession()->pageTextContains('-$99.90'); $this->assertSession()->fieldNotExists('coupons[code]'); $this->assertSession()->buttonNotExists('Apply'); $this->getSession()->getPage()->pressButton('Remove promotion'); - $this->assertSession()->elementTextNotContains('css', '.order-total-line', 'Discount'); + $this->drupalGet(Url::fromRoute('commerce_cart.page')); + $this->assertSession()->pageTextNotContains('-$99.90'); $this->assertSession()->fieldExists('coupons[code]'); $this->assertSession()->buttonExists('Apply'); }