From 82de51f23e0dcd1d6c17375fdb94f05882634aa4 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Mon, 27 Apr 2020 21:02:14 -0400 Subject: [PATCH 1/8] Some unittest fixes for local running adapting to various factory changes. --- tests/unit/AssetModelTest.php | 4 ++-- tests/unit/AssetTest.php | 16 +++++++++++++--- tests/unit/BaseTest.php | 25 +++++++++++++++++-------- tests/unit/CategoryTest.php | 3 ++- tests/unit/CompanyTest.php | 7 ++----- tests/unit/NotificationTest.php | 18 +++++++++--------- 6 files changed, 45 insertions(+), 28 deletions(-) diff --git a/tests/unit/AssetModelTest.php b/tests/unit/AssetModelTest.php index edb24243e3..a65832b7c6 100644 --- a/tests/unit/AssetModelTest.php +++ b/tests/unit/AssetModelTest.php @@ -42,10 +42,10 @@ class AssetModelTest extends BaseTest public function testAnAssetModelContainsAssets() { $assetModel = $this->createValidAssetModel(); - factory(Asset::class)->create([ + $this->createValidAsset([ 'model_id' => $assetModel->id, ]); - $this->assertEquals(1,$assetModel->assets()->count()); + $this->assertEquals(1, $assetModel->assets()->count()); } public function testAnAssetModelHasACategory() diff --git a/tests/unit/AssetTest.php b/tests/unit/AssetTest.php index 7f93f53b1a..375da10f18 100644 --- a/tests/unit/AssetTest.php +++ b/tests/unit/AssetTest.php @@ -77,7 +77,11 @@ class AssetTest extends BaseTest */ public function testWarrantyExpiresAttribute() { - $asset = factory(Asset::class)->states('laptop-mbp')->create(['model_id' => $this->createValidAssetModel()->id]); + $asset = factory(Asset::class)->states('laptop-mbp')->create([ + 'model_id' => $this->createValidAssetModel()->id, + 'supplier_id' => $this->createvalidSupplier()->id, + 'rtd_location_id' => $this->createValidLocation()->id + ]); $asset->purchase_date = Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0); $asset->warranty_months = 24; @@ -109,7 +113,11 @@ class AssetTest extends BaseTest public function testModelIdMustExist() { $model = $this->createValidAssetModel(); - $asset = factory(Asset::class)->make(['model_id' => $model->id]); + $asset = factory(Asset::class)->make([ + 'model_id' => $model->id, + 'supplier_id' => $this->createValidSupplier()->id, + 'rtd_location_id' => $this->createValidLocation()->id + ]); $asset->save(); $this->assertTrue($asset->isValid()); $newId = $model->id + 1; @@ -188,7 +196,9 @@ class AssetTest extends BaseTest public function testAnAssetCanHaveUploads() { - $asset = $this->createValidAsset(); + $asset = $this->createValidAsset([ + 'supplier_id' => $this->createValidSupplier()->id + ]); $this->assertCount(0, $asset->uploads); factory(App\Models\Actionlog::class, 'asset-upload')->create(['item_id' => $asset->id]); $this->assertCount(1, $asset->fresh()->uploads); diff --git a/tests/unit/BaseTest.php b/tests/unit/BaseTest.php index 9b77c802b7..4bb208a025 100644 --- a/tests/unit/BaseTest.php +++ b/tests/unit/BaseTest.php @@ -16,7 +16,9 @@ class BaseTest extends \Codeception\TestCase\Test protected function signIn($user = null) { if (!$user) { - $user = factory(User::class)->states('superuser')->create(); + $user = factory(User::class)->states('superuser')->create([ + 'location_id' => $this->createValidLocation()->id + ]); } Auth::login($user); @@ -78,17 +80,24 @@ class BaseTest extends \Codeception\TestCase\Test protected function createValidUser($overrides= []) { - return factory(App\Models\User::class)->create($overrides); + return factory(App\Models\User::class)->create( + array_merge([ + 'location_id'=>$this->createValidLocation()->id + ], $overrides) + ); } - protected function createValidAsset($overrides = []) + protected function createValidAsset($overrides = [], $qty = 1) { - $locId = $this->createValidLocation(); + $locId = $this->createValidLocation()->id; $this->createValidAssetModel(); - return factory(\App\Models\Asset::class)->states('laptop-mbp')->create([ - 'rtd_location_id' => $locId, - 'location_id' => $locId - ], $overrides); + return factory(\App\Models\Asset::class, $qty)->states('laptop-mbp')->create( + array_merge([ + 'rtd_location_id' => $locId, + 'location_id' => $locId, + 'supplier_id' => $this->createValidSupplier()->id + ], $overrides) + ); } diff --git a/tests/unit/CategoryTest.php b/tests/unit/CategoryTest.php index 6d0d4e2fe4..518cfa8134 100644 --- a/tests/unit/CategoryTest.php +++ b/tests/unit/CategoryTest.php @@ -38,7 +38,8 @@ class CategoryTest extends BaseTest $this->assertCount(5, $category->models); $models->each(function($model) { - factory(App\Models\Asset::class, 2)->create(['model_id' => $model->id]); + // factory(App\Models\Asset::class, 2)->create(['model_id' => $model->id]); + $this->createValidAsset(['model_id' => $model->id], 2); }); $this->assertEquals(10, $category->itemCount()); } diff --git a/tests/unit/CompanyTest.php b/tests/unit/CompanyTest.php index 59930ba4c1..22ac12bd87 100644 --- a/tests/unit/CompanyTest.php +++ b/tests/unit/CompanyTest.php @@ -31,17 +31,14 @@ class CompanyTest extends BaseTest public function testACompanyCanHaveUsers() { $company = $this->createValidCompany(); - factory(App\Models\User::class, 1)->create(['company_id'=>$company->id]); + $user = $this->createValidUser(['company_id'=>$company->id]); $this->assertCount(1, $company->users); } public function testACompanyCanHaveAssets() { $company = $this->createValidCompany(); - factory(App\Models\Asset::class, 1)->states('laptop-mbp')->create([ - 'company_id' => $company->id, - 'model_id' => $this->createValidAssetModel()->id - ]); + $this->createValidAsset(['company_id' => $company->id]); $this->assertCount(1, $company->assets); } diff --git a/tests/unit/NotificationTest.php b/tests/unit/NotificationTest.php index 10bbe72289..4f1441b0e1 100644 --- a/tests/unit/NotificationTest.php +++ b/tests/unit/NotificationTest.php @@ -21,15 +21,15 @@ class NotificationTest extends BaseTest public function testAUserIsEmailedIfTheyCheckoutAnAssetWithEULA() { - $admin = factory(User::class)->states('superuser')->create(); - Auth::login($admin); - $cat = $this->createValidCategory('asset-laptop-category', ['require_acceptance' => true]); - $model = $this->createValidAssetModel('mbp-13-model', ['category_id' => $cat->id]); - $asset = $this->createValidAsset(['model_id' => $model->id]); - $user = factory(User::class)->create(); - Notification::fake(); - $asset->checkOut($user, 1); + $admin = factory(User::class)->states('superuser')->create(); + Auth::login($admin); + $cat = $this->createValidCategory('asset-laptop-category', ['require_acceptance' => true]); + $model = $this->createValidAssetModel('mbp-13-model', ['category_id' => $cat->id]); + $asset = $this->createValidAsset(['model_id' => $model->id]); + $user = $this->createValidUser(); - Notification::assertSentTo($user, CheckoutAssetNotification::class); + Notification::fake(); + $asset->checkOut($user, 1); + Notification::assertSentTo($user, CheckoutAssetNotification::class); } } From c74b904f141a6deb4e63d7cc2ba10bda938f1ee5 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 11:27:44 -0400 Subject: [PATCH 2/8] Fix Location parent different validation on new location creation. --- app/Http/Controllers/LocationsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index 5d76d59e67..86476867c1 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -67,6 +67,7 @@ class LocationsController extends Controller { $this->authorize('create', Location::class); $location = new Location(); + $location->id = null; // This is required to make Laravels different validation work, it errors if the parameter doesn't exist (maybe a bug)? $location->name = $request->input('name'); $location->parent_id = $request->input('parent_id', null); $location->currency = $request->input('currency', '$'); From bb50828ca00d8e843321585999eb6945240e4683 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 11:28:14 -0400 Subject: [PATCH 3/8] Remove General serial field now that the serial is part of the multi asset creation. --- resources/views/hardware/edit.blade.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/views/hardware/edit.blade.php b/resources/views/hardware/edit.blade.php index c47bb6777a..9b2077923e 100755 --- a/resources/views/hardware/edit.blade.php +++ b/resources/views/hardware/edit.blade.php @@ -77,7 +77,6 @@ @include ('partials.forms.edit.location-select', ['translated_name' => trans('admin/hardware/form.checkout_to'), 'fieldname' => 'assigned_location', 'style' => 'display:none;', 'required' => 'false']) @endif - @include ('partials.forms.edit.serial', ['fieldname'=> 'serials[1]','translated_serial' => trans('admin/hardware/form.serial')]) @include ('partials.forms.edit.name', ['translated_name' => trans('admin/hardware/form.name')]) @include ('partials.forms.edit.purchase_date') @include ('partials.forms.edit.supplier-select', ['translated_name' => trans('general.supplier'), 'fieldname' => 'supplier_id']) @@ -155,7 +154,7 @@ if(transformed_oldvals[elem.name]) { $(elem).val(transformed_oldvals[elem.name]).trigger('change'); //the trigger is for select2-based objects, if we have any } - + }); } }); From 05187eb27fa4b9563f9ad8459d716419da36c421 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 11:31:30 -0400 Subject: [PATCH 4/8] Fix Functional Tests. --- app/Http/Controllers/Assets/AssetsController.php | 2 +- tests/functional/AssetsCest.php | 11 +++++++---- tests/functional/GroupsCest.php | 4 ++-- tests/functional/UsersCest.php | 2 +- tests/unit/LocationTest.php | 1 + 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index bf8ca1c93d..6d354869d3 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -802,4 +802,4 @@ class AssetsController extends Controller return view('hardware/requested', compact('requestedItems')); } -} \ No newline at end of file +} diff --git a/tests/functional/AssetsCest.php b/tests/functional/AssetsCest.php index 5ff47e5dba..55501c7336 100644 --- a/tests/functional/AssetsCest.php +++ b/tests/functional/AssetsCest.php @@ -22,8 +22,11 @@ class AssetsCest public function failsEmptyValidation(FunctionalTester $I) { + $I->wantTo("Test Validation Fails with blank elements"); $I->amOnPage(route('hardware.create')); + // Settings factory can enable auto prefixes, which generate a random asset id. Lets clear it out for the sake of this test. + $I->fillField('#asset_tag', ''); $I->click('Save'); $I->see('The asset tag field is required.', '.alert-msg'); $I->see('The model id field is required.', '.alert-msg'); @@ -40,7 +43,7 @@ class AssetsCest ]); $userId = $I->getUserId(); $values = [ - 'asset_tag' => $asset->asset_tag, + 'asset_tags[1]' => $asset->asset_tag, 'assigned_user' => $userId, 'company_id' => $asset->company_id, 'model_id' => $asset->model_id, @@ -51,7 +54,7 @@ class AssetsCest 'purchase_date' => '2016-01-01', 'requestable' => $asset->requestable, 'rtd_location_id' => $asset->rtd_location_id, - 'serial' => $asset->serial, + 'serials[1]' => $asset->serial, 'status_id' => $asset->status_id, 'supplier_id' => $asset->supplier_id, 'warranty_months' => $asset->warranty_months, @@ -67,7 +70,7 @@ class AssetsCest 'notes' => $asset->notes, 'order_number' => $asset->order_number, 'purchase_cost' => $asset->purchase_cost, - 'purchase_date' => Carbon::parse('2016-01-01'), + 'purchase_date' => '2016-01-01', 'requestable' => $asset->requestable, 'rtd_location_id' => $asset->rtd_location_id, 'serial' => $asset->serial, @@ -80,7 +83,7 @@ class AssetsCest $I->amOnPage(route('hardware.create')); $I->submitForm('form#create-form', $values); $I->seeRecord('assets', $seenValues); - $I->dontSeeElement('.alert-danger'); // We should check for success, but we can't because of the stupid ajaxy way I did things. FIXME when the asset form is rewritten. + $I->seeResponseCodeIs(200); } public function allowsDelete(FunctionalTester $I) diff --git a/tests/functional/GroupsCest.php b/tests/functional/GroupsCest.php index b24b3c07cc..724b4dbca1 100644 --- a/tests/functional/GroupsCest.php +++ b/tests/functional/GroupsCest.php @@ -39,10 +39,10 @@ class GroupsCest $I->wantTo("Test Validation Fails with short name"); $I->amOnPage(route('groups.create')); $I->seeResponseCodeIs(200); - $I->fillField('name', 't2'); + $I->fillField('name', 't'); $I->click('Save'); $I->seeElement('.alert-danger'); - $I->see('The name must be at least 3 characters', '.alert-msg'); + $I->see('The name must be at least 2 characters', '.alert-msg'); } public function passesCorrectValidation(FunctionalTester $I) diff --git a/tests/functional/UsersCest.php b/tests/functional/UsersCest.php index 40e8d7e7ff..42ca897342 100644 --- a/tests/functional/UsersCest.php +++ b/tests/functional/UsersCest.php @@ -42,7 +42,7 @@ class UsersCest $I->fillField('password', '12345'); $I->click('Save'); $I->seeElement('.alert-danger'); - $I->see('The password must be at least 10 characters', '.alert-msg'); + $I->see('The password must be at least 8 characters', '.alert-msg'); } public function passesCorrectValidation(FunctionalTester $I) diff --git a/tests/unit/LocationTest.php b/tests/unit/LocationTest.php index 09cf2ff589..82f0a14ef1 100644 --- a/tests/unit/LocationTest.php +++ b/tests/unit/LocationTest.php @@ -13,6 +13,7 @@ class LocationTest extends BaseTest protected $tester; public function testPassesIfNotSelfParent() { + $this->createValidLocation(['id' => 10]); $a = factory(Location::class)->make([ 'name' => 'Test Location', From 9520ccae47a02680f5742678537de4406cc7735c Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 11:37:13 -0400 Subject: [PATCH 5/8] Unit test fix. Cleaner this way. --- tests/unit/BaseTest.php | 4 ++-- tests/unit/CategoryTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/BaseTest.php b/tests/unit/BaseTest.php index 4bb208a025..16b171193a 100644 --- a/tests/unit/BaseTest.php +++ b/tests/unit/BaseTest.php @@ -87,11 +87,11 @@ class BaseTest extends \Codeception\TestCase\Test ); } - protected function createValidAsset($overrides = [], $qty = 1) + protected function createValidAsset($overrides = []) { $locId = $this->createValidLocation()->id; $this->createValidAssetModel(); - return factory(\App\Models\Asset::class, $qty)->states('laptop-mbp')->create( + return factory(\App\Models\Asset::class)->states('laptop-mbp')->create( array_merge([ 'rtd_location_id' => $locId, 'location_id' => $locId, diff --git a/tests/unit/CategoryTest.php b/tests/unit/CategoryTest.php index 518cfa8134..a725010cfb 100644 --- a/tests/unit/CategoryTest.php +++ b/tests/unit/CategoryTest.php @@ -38,8 +38,8 @@ class CategoryTest extends BaseTest $this->assertCount(5, $category->models); $models->each(function($model) { - // factory(App\Models\Asset::class, 2)->create(['model_id' => $model->id]); - $this->createValidAsset(['model_id' => $model->id], 2); + $this->createValidAsset(['model_id' => $model->id]); + $this->createValidAsset(['model_id' => $model->id]); }); $this->assertEquals(10, $category->itemCount()); } From 3e7fbb02b791f5b1bad4b1e071cf33dafdac00e4 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 11:44:17 -0400 Subject: [PATCH 6/8] Notes and style cleanups --- tests/functional/AssetsCest.php | 3 +-- tests/unit/CategoryTest.php | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/AssetsCest.php b/tests/functional/AssetsCest.php index 55501c7336..078a39aa92 100644 --- a/tests/functional/AssetsCest.php +++ b/tests/functional/AssetsCest.php @@ -22,7 +22,6 @@ class AssetsCest public function failsEmptyValidation(FunctionalTester $I) { - $I->wantTo("Test Validation Fails with blank elements"); $I->amOnPage(route('hardware.create')); // Settings factory can enable auto prefixes, which generate a random asset id. Lets clear it out for the sake of this test. @@ -54,7 +53,7 @@ class AssetsCest 'purchase_date' => '2016-01-01', 'requestable' => $asset->requestable, 'rtd_location_id' => $asset->rtd_location_id, - 'serials[1]' => $asset->serial, + 'serials[1]' => $asset->serial, 'status_id' => $asset->status_id, 'supplier_id' => $asset->supplier_id, 'warranty_months' => $asset->warranty_months, diff --git a/tests/unit/CategoryTest.php b/tests/unit/CategoryTest.php index a725010cfb..69fc841ac9 100644 --- a/tests/unit/CategoryTest.php +++ b/tests/unit/CategoryTest.php @@ -38,6 +38,7 @@ class CategoryTest extends BaseTest $this->assertCount(5, $category->models); $models->each(function($model) { + // This is intentionally run twice to generate the ten imagined assets, done this way to keep it in sync with createValidAsset rather than using the factory directly. $this->createValidAsset(['model_id' => $model->id]); $this->createValidAsset(['model_id' => $model->id]); }); From 61a216a531493f88fc29254e6cc2ea228b2afd46 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 12:14:23 -0400 Subject: [PATCH 7/8] Rearrange seeder order so that users validate properly on first seed run. This hopefully fixes the actionlog seeder error. --- database/seeds/DatabaseSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index c5ea09b165..1da491a007 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -23,11 +23,11 @@ class DatabaseSeeder extends Seeder $this->call(CompanySeeder::class); $this->call(CategorySeeder::class); + $this->call(LocationSeeder::class); $this->call(UserSeeder::class); $this->call(DepreciationSeeder::class); $this->call(DepartmentSeeder::class); $this->call(ManufacturerSeeder::class); - $this->call(LocationSeeder::class); $this->call(SupplierSeeder::class); $this->call(AssetModelSeeder::class); $this->call(DepreciationSeeder::class); From c65b8f97bbd1b85f1eedf08560799636e55196f5 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Apr 2020 12:16:37 -0400 Subject: [PATCH 8/8] Laravel 6 drops support for php7.1, add php7.4 instead. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 75c7c2245c..6ea67cb9d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,9 +14,9 @@ services: # list any PHP version you want to test against php: - - 7.1.8 - 7.2 - 7.3.0 + - 7.4 # execute any number of scripts before the test run, custom env's are available as variables