-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2863864 - Disable promotions via cron if end date or usage limit hit #708
Open
swickham78
wants to merge
21
commits into
drupalcommerce:8.x-2.x
Choose a base branch
from
swickham78:promotions_disable_expired
base: 8.x-2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−0
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
527df0b
Disable expired promotions on cron run.
221cbd1
Merge branch '8.x-2.x' of https://github.com/swickham78/commerce into…
2b8f266
Issue #2863864: Moved max usage load of promotions to its own method …
caf23a5
Issue #2863864: Added tests for loading of expired and maxed usaged p…
863ce8f
Issue #2863864: Fix merge conflict w/ recent work.
24856a6
Merge branch '8.x-2.x' of https://github.com/swickham78/commerce into…
e7d8a40
Issue #2863864: Updates to meet coding standards.
0794a1b
Disable expired promotions on cron run.
e0f422f
Issue #2863864: Moved max usage load of promotions to its own method …
73e867a
Issue #2863864: Added tests for loading of expired and maxed usaged p…
70ce340
Issue #2863864: Updates to meet coding standards.
029e4c4
Added cron function test.
4cb8f54
Merge branch 'promotions_disable_expired' of https://github.com/swick…
a557186
Disable expired promotions on cron run.
f659ea1
Issue #2863864: Moved max usage load of promotions to its own method …
6bba454
Issue #2863864: Added tests for loading of expired and maxed usaged p…
4ac0f14
Issue #2863864: Updates to meet coding standards.
d3fda01
Issue #2863864: Moved max usage load of promotions to its own method …
9aef5f3
Issue #2863864: Updates to meet coding standards.
04ef19f
Added cron function test.
d5a4e42
Merge branch 'promotions_disable_expired' of https://github.com/swick…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,20 @@ interface PromotionStorageInterface extends ContentEntityStorageInterface { | |
*/ | ||
public function loadAvailable(OrderTypeInterface $order_type, StoreInterface $store); | ||
|
||
/** | ||
* Return active promotions that have passed their end date. | ||
* | ||
* @return \Drupal\commerce_promotion\Entity\PromotionInterface[] | ||
* The expired promotion entities. | ||
*/ | ||
public function loadExpired(); | ||
|
||
/** | ||
* Returns active promotions which have a met their maximum usage. | ||
* | ||
* @return \Drupal\commerce_promotion\Entity\PromotionInterface[] | ||
* Promotions with maxed usage. | ||
*/ | ||
public function loadMaxedUsage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bojanz name bikeshedding. |
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
namespace Drupal\Tests\commerce_promotion\Kernel; | ||
|
||
use Drupal\commerce_order\Entity\Order; | ||
use Drupal\commerce_order\Entity\OrderType; | ||
use Drupal\commerce_order\Entity\OrderItemType; | ||
use Drupal\commerce_promotion\Entity\Coupon; | ||
use Drupal\commerce_promotion\Entity\Promotion; | ||
use Drupal\Core\Datetime\DrupalDateTime; | ||
|
@@ -22,6 +24,20 @@ class PromotionStorageTest extends CommerceKernelTestBase { | |
*/ | ||
protected $promotionStorage; | ||
|
||
/** | ||
* The usage. | ||
* | ||
* @var \Drupal\commerce_promotion\PromotionUsageInterface | ||
*/ | ||
protected $usage; | ||
|
||
/** | ||
* The test order. | ||
* | ||
* @var \Drupal\commerce_order\Entity\OrderInterface | ||
*/ | ||
protected $order; | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
|
@@ -54,6 +70,25 @@ protected function setUp() { | |
$this->installSchema('commerce_promotion', ['commerce_promotion_usage']); | ||
|
||
$this->promotionStorage = $this->container->get('entity_type.manager')->getStorage('commerce_promotion'); | ||
$this->usage = $this->container->get('commerce_promotion.usage'); | ||
|
||
OrderItemType::create([ | ||
'id' => 'test', | ||
'label' => 'Test', | ||
'orderType' => 'default', | ||
])->save(); | ||
|
||
$this->order = Order::create([ | ||
'type' => 'default', | ||
'state' => 'completed', | ||
'mail' => '[email protected]', | ||
'ip_address' => '127.0.0.1', | ||
'order_id' => '6', | ||
'order_number' => '6', | ||
'store_id' => $this->store, | ||
'uid' => $this->createUser()->id(), | ||
'order_items' => [], | ||
]); | ||
} | ||
|
||
/** | ||
|
@@ -213,4 +248,78 @@ public function testWeight() { | |
$this->assertEquals($promotion1->label(), $promotion->label()); | ||
} | ||
|
||
/** | ||
* Tests that active promotions which have expired are loaded. | ||
*/ | ||
public function testLoadExpired() { | ||
$order_type = OrderType::load('default'); | ||
|
||
$valid_promotion = Promotion::create([ | ||
'name' => 'Valid Promotion', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'start_date' => gmdate('Y-m-d', time()), | ||
'end_date' => gmdate('Y-m-d', time() + 86400), | ||
]); | ||
$this->assertEquals(SAVED_NEW, $valid_promotion->save()); | ||
|
||
$expired_promotion = Promotion::create([ | ||
'name' => 'Expired Promotion', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'start_date' => gmdate('Y-m-d', time() - 172800), | ||
'end_date' => gmdate('Y-m-d', time() - 86400), | ||
]); | ||
$this->assertEquals(SAVED_NEW, $expired_promotion->save()); | ||
|
||
$promotions = $this->promotionStorage->loadExpired(); | ||
$this->assertCount(1, $promotions); | ||
|
||
$promotion = reset($promotions); | ||
$this->assertEquals($expired_promotion->label(), $promotion->label()); | ||
} | ||
|
||
/** | ||
* Tests that active promotions which have met their maximum usage are loaded. | ||
*/ | ||
public function testLoadUsed() { | ||
$order_type = OrderType::load('default'); | ||
|
||
$promotion1 = Promotion::create([ | ||
'name' => 'Promotion 1', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'usage_limit' => 1, | ||
]); | ||
$this->assertEquals(SAVED_NEW, $promotion1->save()); | ||
$this->usage->addUsage($this->order, $promotion1); | ||
|
||
$promotion2 = Promotion::create([ | ||
'name' => 'Promotion 2', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'usage_limit' => 2, | ||
]); | ||
$this->assertEquals(SAVED_NEW, $promotion2->save()); | ||
$this->usage->addUsage($this->order, $promotion2); | ||
|
||
$promotion3 = Promotion::create([ | ||
'name' => 'Promotion 3', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
]); | ||
$this->assertEquals(SAVED_NEW, $promotion3->save()); | ||
|
||
$promotions = $this->promotionStorage->loadMaxedUsage(); | ||
$this->assertCount(1, $promotions); | ||
|
||
$promotion = reset($promotions); | ||
$this->assertEquals($promotion1->label(), $promotion->label()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,7 @@ protected function setUp() { | |
'state' => 'draft', | ||
'mail' => '[email protected]', | ||
'ip_address' => '127.0.0.1', | ||
'order_id' => '6', | ||
'order_number' => '6', | ||
'store_id' => $this->store, | ||
'uid' => $this->createUser(), | ||
|
@@ -237,4 +238,59 @@ public function testPromotionFiltering() { | |
$this->assertEmpty($valid_promotions); | ||
} | ||
|
||
/** | ||
* Tests the Promotions module cron job. | ||
*/ | ||
public function testPromotionCron() { | ||
$order_type = OrderType::load($this->order->bundle()); | ||
|
||
// Date restricted promotions. | ||
$valid_promotion = Promotion::create([ | ||
'name' => 'Valid Promotion', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'start_date' => gmdate('Y-m-d', time()), | ||
'end_date' => gmdate('Y-m-d', time() + 86400), | ||
]); | ||
$this->assertEquals(SAVED_NEW, $valid_promotion->save()); | ||
|
||
$expired_promotion = Promotion::create([ | ||
'name' => 'Expired Promotion', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'start_date' => gmdate('Y-m-d', time() - 172800), | ||
'end_date' => gmdate('Y-m-d', time() - 86400), | ||
]); | ||
$this->assertEquals(SAVED_NEW, $expired_promotion->save()); | ||
|
||
// Usage restricted promotions. | ||
$promotion1 = Promotion::create([ | ||
'name' => 'Promotion 1', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'usage_limit' => 1, | ||
]); | ||
$this->assertEquals(SAVED_NEW, $promotion1->save()); | ||
$this->usage->addUsage($this->order, $promotion1); | ||
|
||
$promotion2 = Promotion::create([ | ||
'name' => 'Promotion 2', | ||
'order_types' => [$order_type], | ||
'stores' => [$this->store->id()], | ||
'status' => TRUE, | ||
'usage_limit' => 2, | ||
]); | ||
$this->assertEquals(SAVED_NEW, $promotion2->save()); | ||
$this->usage->addUsage($this->order, $promotion2); | ||
|
||
commerce_promotion_cron(); | ||
|
||
$valid_promotions = $this->promotionStorage->loadAvailable($order_type, $this->store); | ||
$this->assertCount(2, $valid_promotions); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bojanz I think this is fine versus a queue. There shouldn't be that much activity, generally.