From 3c3219c9ce409af485a004532df70f41c1bb5cd9 Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:53:14 +0800 Subject: [PATCH] scheduleCustomerGroup remove start date null to `now()` (#1172) currently when schedule product customer group if no start date passed, the code will set `$start = now()` this behaviour may not be desired and is not consistent with `scheduleChannel` which does not set to now when null also when start date is null the hub will show as "always available" - ~~Documentation updates~~ - [x] Automated tests - [x] Changelog entries (core ~~and hub~~) Co-authored-by: Alec Ritson --- packages/core/CHANGELOG.md | 5 +++++ .../Base/Traits/CanScheduleAvailability.php | 2 -- .../Base/Traits/HasCustomerGroupsTest.php | 22 +++++++++++++++++++ .../core/tests/Unit/Models/ProductTest.php | 22 +++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 56214f714d..ceb6608dbf 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [Unreleased] + +### Changed +- when `Product->scheduleCustomerGroup($customerGroups, $startDate = null)` when `$startDate` is `null` it will not set to `now()` and always available + ## 0.4 ### Fixed diff --git a/packages/core/src/Base/Traits/CanScheduleAvailability.php b/packages/core/src/Base/Traits/CanScheduleAvailability.php index 51f1f9a0f6..0ab9743bf6 100644 --- a/packages/core/src/Base/Traits/CanScheduleAvailability.php +++ b/packages/core/src/Base/Traits/CanScheduleAvailability.php @@ -42,8 +42,6 @@ protected function schedule( throw new SchedulingException($error); } - $starts = $starts ?: now(); - $relation->syncWithoutDetaching( $this->getScheduleMapping($models, array_merge([ 'starts_at' => $starts, diff --git a/packages/core/tests/Unit/Base/Traits/HasCustomerGroupsTest.php b/packages/core/tests/Unit/Base/Traits/HasCustomerGroupsTest.php index bfc6266552..4b5b2d4f78 100644 --- a/packages/core/tests/Unit/Base/Traits/HasCustomerGroupsTest.php +++ b/packages/core/tests/Unit/Base/Traits/HasCustomerGroupsTest.php @@ -36,6 +36,28 @@ public function can_schedule_using_single_model() ); } + /** @test */ + public function can_schedule_always_available() + { + $product = Product::factory()->create(); + + $customerGroup = CustomerGroup::factory()->create(); + + $product->scheduleCustomerGroup($customerGroup); + + $this->assertDatabaseHas( + 'lunar_customer_group_product', + [ + 'customer_group_id' => $customerGroup->id, + 'enabled' => 1, + 'visible' => 1, + 'purchasable' => 1, + 'starts_at' => null, + 'ends_at' => null, + ], + ); + } + /** @test */ public function can_schedule_using_array_of_models() { diff --git a/packages/core/tests/Unit/Models/ProductTest.php b/packages/core/tests/Unit/Models/ProductTest.php index e9bee674de..4daaaa343d 100644 --- a/packages/core/tests/Unit/Models/ProductTest.php +++ b/packages/core/tests/Unit/Models/ProductTest.php @@ -146,6 +146,28 @@ public function customer_groups_can_be_enabled() ); } + /** @test */ + public function customer_groups_can_be_scheduled_always_available() + { + $product = Product::factory()->create(); + + $customerGroup = CustomerGroup::factory()->create(); + + $product->scheduleCustomerGroup($customerGroup); + + $this->assertDatabaseHas( + 'lunar_customer_group_product', + [ + 'customer_group_id' => $customerGroup->id, + 'enabled' => 1, + 'purchasable' => 1, + 'visible' => 1, + 'starts_at' => null, + 'ends_at' => null, + ], + ); + } + /** @test */ public function customer_groups_can_be_scheduled_with_start_and_end() {