Skip to content

Commit

Permalink
Issue #2867019 part II: Fix the build instability.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Apr 21, 2017
1 parent 9131c8f commit c15564f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
6 changes: 4 additions & 2 deletions modules/promotion/src/Entity/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
23 changes: 18 additions & 5 deletions modules/promotion/src/PromotionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;
}

/**
Expand All @@ -60,22 +71,24 @@ 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')
);
}

/**
* {@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
Expand Down
10 changes: 7 additions & 3 deletions modules/promotion/tests/src/Functional/CouponRedemptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ 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' => [
'amount' => '0.10',
],
],
'conditions' => [],
'start_date' => '2017-01-01',
'status' => TRUE,
]);

$coupon = $this->createEntity('commerce_promotion_coupon', [
Expand Down Expand Up @@ -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');
}
Expand Down

0 comments on commit c15564f

Please sign in to comment.