From 63f0b5279d950092d9aa8f451dcb032809132828 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 24 Jul 2024 20:13:01 +0100 Subject: [PATCH 1/4] Added check for purchase_date Signed-off-by: snipe --- app/Models/License.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/License.php b/app/Models/License.php index d8bc3f03b..2680424a8 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -50,7 +50,7 @@ class License extends Depreciable 'category_id' => 'required|exists:categories,id', 'company_id' => 'integer|nullable', 'purchase_cost'=> 'numeric|nullable|gte:0', - 'purchase_date' => 'date_format:Y-m-d|nullable|max:10', + 'purchase_date' => 'date_format:Y-m-d|nullable|max:10|required_with:depreciation_id', 'expiration_date' => 'date_format:Y-m-d|nullable|max:10', 'termination_date' => 'date_format:Y-m-d|nullable|max:10', 'min_amt' => 'numeric|nullable|gte:0', From adf58a06da86bca9a1320be636b199ae0fa20a32 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 24 Jul 2024 20:13:11 +0100 Subject: [PATCH 2/4] Added check for purchase date Signed-off-by: snipe --- app/Models/Depreciable.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/Models/Depreciable.php b/app/Models/Depreciable.php index 721135873..6139cda08 100644 --- a/app/Models/Depreciable.php +++ b/app/Models/Depreciable.php @@ -158,17 +158,20 @@ class Depreciable extends SnipeModel public function time_until_depreciated() { - // @link http://www.php.net/manual/en/class.datetime.php - $d1 = new \DateTime(); - $d2 = $this->depreciated_date(); + if ($this->depreciated_date()) { + // @link http://www.php.net/manual/en/class.datetime.php + $d1 = new \DateTime(); + $d2 = $this->depreciated_date(); - // @link http://www.php.net/manual/en/class.dateinterval.php - $interval = $d1->diff($d2); - if (! $interval->invert) { - return $interval; - } else { - return new \DateInterval('PT0S'); //null interval (zero seconds from now) + // @link http://www.php.net/manual/en/class.dateinterval.php + $interval = $d1->diff($d2); + if (! $interval->invert) { + return $interval; + } else { + return new \DateInterval('PT0S'); //null interval (zero seconds from now) + } } + return false; } public function depreciated_date() From ef145e47b45d9601e23d5c317bdb28f555b71ef9 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 24 Jul 2024 20:13:15 +0100 Subject: [PATCH 3/4] Added tests Signed-off-by: snipe --- .../Feature/Licenses/Ui/CreateLicenseTest.php | 42 +++++++++++++++++++ tests/Feature/Licenses/Ui/LicenseViewTest.php | 41 ++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/Feature/Licenses/Ui/CreateLicenseTest.php create mode 100644 tests/Feature/Licenses/Ui/LicenseViewTest.php diff --git a/tests/Feature/Licenses/Ui/CreateLicenseTest.php b/tests/Feature/Licenses/Ui/CreateLicenseTest.php new file mode 100644 index 000000000..f24c3bd2c --- /dev/null +++ b/tests/Feature/Licenses/Ui/CreateLicenseTest.php @@ -0,0 +1,42 @@ +create(); + $this->actingAs(User::factory()->create()) + ->get(route('licenses.create', $license)) + ->assertForbidden(); + } + + + + public function testLicenseWithoutPurchaseDateFailsValidation() + { + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('licenses.create')) + ->post(route('licenses.store'), [ + 'name' => 'Test Invalid License', + 'seats' => '10', + 'category_id' => Category::factory()->forLicenses()->create()->id, + 'depreciation_id' => Depreciation::factory()->create()->id + ]); + $response->assertStatus(302); + $response->assertRedirect(route('licenses.create')); + $response->assertInvalid(['purchase_date']); + $response->assertSessionHasErrors(['purchase_date']); + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(AssetModel::where('name', 'Test Invalid License')->exists()); + + } +} diff --git a/tests/Feature/Licenses/Ui/LicenseViewTest.php b/tests/Feature/Licenses/Ui/LicenseViewTest.php new file mode 100644 index 000000000..295b23c5f --- /dev/null +++ b/tests/Feature/Licenses/Ui/LicenseViewTest.php @@ -0,0 +1,41 @@ +create(); + $this->actingAs(User::factory()->create()) + ->get(route('licenses.show', $license)) + ->assertForbidden(); + } + + + public function testLicenseWithNoPurchaseDateDoesNotTriggerDepreciation() + { + $depreciation = Depreciation::factory()->create(['months' => 12]); + $license = License::factory()->create(['depreciation_id' => $depreciation->id, 'purchase_date' => null]); + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('licenses.show', $license)) + ->assertOk(); + } + + public function testLicenseWithPurchaseDateDepreciatesCorrectly() + { + $depreciation = Depreciation::factory()->create(['months' => 12]); + $license = License::factory()->create(['depreciation_id' => $depreciation->id, 'purchase_date' => '2020-01-01']); + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('licenses.show', $license)) + ->assertOk() + ->assertSee([ + '2021-01-01' + ], false); + } +} From da4ec145d764cdbcc4557e0af74f9ede8157312a Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 24 Jul 2024 20:17:30 +0100 Subject: [PATCH 4/4] Removed test no longer needed due to validation Signed-off-by: snipe --- tests/Feature/Licenses/Ui/LicenseViewTest.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/Feature/Licenses/Ui/LicenseViewTest.php b/tests/Feature/Licenses/Ui/LicenseViewTest.php index 295b23c5f..3b1f7830d 100644 --- a/tests/Feature/Licenses/Ui/LicenseViewTest.php +++ b/tests/Feature/Licenses/Ui/LicenseViewTest.php @@ -16,17 +16,7 @@ class LicenseViewTest extends TestCase ->get(route('licenses.show', $license)) ->assertForbidden(); } - - - public function testLicenseWithNoPurchaseDateDoesNotTriggerDepreciation() - { - $depreciation = Depreciation::factory()->create(['months' => 12]); - $license = License::factory()->create(['depreciation_id' => $depreciation->id, 'purchase_date' => null]); - $this->actingAs(User::factory()->superuser()->create()) - ->get(route('licenses.show', $license)) - ->assertOk(); - } - + public function testLicenseWithPurchaseDateDepreciatesCorrectly() { $depreciation = Depreciation::factory()->create(['months' => 12]);