From 3a6bbcc615d44444419633b3e2a006d728d0943c Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Fri, 30 Dec 2016 14:44:47 -0500 Subject: [PATCH] More improvements (#3116) * Restore display of deleted items in the logs * Fix functional tests. --- app/Models/Actionlog.php | 2 +- app/Presenters/ActionlogPresenter.php | 41 +++++++++++++++++++------- app/Presenters/ConsumablePresenter.php | 8 ++++- database/factories/ModelFactory.php | 1 - tests/functional/AssetsCest.php | 5 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index a85f5fcde4..3aa0307eb8 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -119,7 +119,7 @@ class Actionlog extends SnipeModel public function target() { - return $this->morphTo('target'); + return $this->morphTo('target')->withTrashed(); } public function childlogs() diff --git a/app/Presenters/ActionlogPresenter.php b/app/Presenters/ActionlogPresenter.php index cceb683a42..1d3635c541 100644 --- a/app/Presenters/ActionlogPresenter.php +++ b/app/Presenters/ActionlogPresenter.php @@ -19,7 +19,7 @@ class ActionlogPresenter extends Presenter 'icon' => '', 'created_at' => date("M d, Y g:iA", strtotime($this->created_at)), 'action_type' => strtolower(trans('general.'.str_replace(' ', '_', $this->action_type))), - 'admin' => $this->model->user ? $this->model->user->present()->nameUrl() : '', + 'admin' => $this->admin(), 'target' => $this->target(), 'item' => $this->item(), 'item_type' => $this->itemType(), @@ -30,6 +30,15 @@ class ActionlogPresenter extends Presenter public function admin() { + if ($user = $this->model->user) { + if(empty($user->deleted_at)) { + return $user->present()->nameUrl(); + } + // The user was deleted + return ''.$user->present()->name()." (deleted)"; + } + + return ''; } public function item() @@ -38,32 +47,42 @@ class ActionlogPresenter extends Presenter if($this->action_type=='uploaded') { return (string) link_to_route('show/userfile', $this->model->filename, [$this->model->item->id, $this->model->id]); } - if ($this->model->item) { - return $this->model->item->present()->nameUrl(); + if ($item = $this->model->item) { + if (empty($item->deleted_at)) { + return $this->model->item->present()->nameUrl(); + } + // The item was deleted + return ''.$item->present()->name().' (deleted)'; } return ''; } public function target() { + $target = null; // Target is messy. // On an upload, the target is the item we are uploading to, stored as the "item" in the log. if ($this->action_type=='uploaded') { - return $this->model->item->present()->nameUrl(); - } + $target = $this->model->item; + } elseif (($this->action_type=='accepted') || ($this->action_type=='declined')) { // If we are logging an accept/reject, the target is not stored directly, // so we access it through who the item is assigned to. // FIXME: On a reject it's not assigned to anyone. - if (($this->action_type=='accepted') || ($this->action_type=='declined')) { - return $this->model->item->assignedTo->nameUrl(); + $target = $this->model->item->assignedTo; } elseif ($this->action_type=='requested') { if ($this->model->user) { - return $this->model->user->present()->nameUrl(); + $target = $this->model->user; } + } elseif ($this->model->target) { + // Otherwise, we'll just take the target of the log. + $target = $this->model->target; } - // Otherwise, we'll just take the target of the log. - if ($this->model->target) { - return $this->model->target->present()->nameUrl(); + + if($target) { + if (empty($target->deleted_at)) { + return $target->present()->nameUrl(); + } + return ''.$target->present()->name().''; } return ''; } diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php index 2346d40ace..7c2be419c9 100644 --- a/app/Presenters/ConsumablePresenter.php +++ b/app/Presenters/ConsumablePresenter.php @@ -58,9 +58,15 @@ class ConsumablePresenter extends Presenter } /** - * Link to this consumables name + * Displayable name of consumable * @return string */ + + public function name() + { + return $this->model->name; + } + public function nameUrl() { return (string)link_to_route('consumables.show', $this->name, $this->id); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index aa929d56b3..ef5d97f105 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -30,7 +30,6 @@ $factory->defineAs(App\Models\Asset::class, 'asset', function (Faker\Generator $ 'purchase_cost' => $faker->randomFloat(2), 'order_number' => $faker->numberBetween(1000000, 50000000), 'supplier_id' => $faker->numberBetween(1, 5), - 'requestable' => $faker->numberBetween(0, 1), 'company_id' => Company::inRandomOrder()->first()->id, 'requestable' => $faker->boolean() ]; diff --git a/tests/functional/AssetsCest.php b/tests/functional/AssetsCest.php index c1b22ea060..b1a99fd137 100644 --- a/tests/functional/AssetsCest.php +++ b/tests/functional/AssetsCest.php @@ -34,12 +34,13 @@ class AssetsCest public function passesCreateAndCheckout(FunctionalTester $I) { $asset = factory(App\Models\Asset::class,'asset')->make(); + $userId = $I->getUserId(); $values = [ 'company_id' => $asset->company_id, 'asset_tag' => $asset->asset_tag, 'model_id' => $asset->model_id, 'status_id' => $asset->status_id, - 'assigned_user' => $I->getUserId(), + 'assigned_user' => $userId, 'serial' => $asset->serial, 'name' => $asset->name, 'purchase_date' => '2016-01-01', @@ -57,7 +58,7 @@ class AssetsCest 'asset_tag' => $asset->asset_tag, 'model_id' => $asset->model_id, 'status_id' => $asset->status_id, - 'assigned_to' => $I->getUserId(), + 'assigned_to' => $userId, 'assigned_type' => 'App\\Models\\User', 'serial' => $asset->serial, 'name' => $asset->name,