A free open source IT asset/license management system
Move dynamic method to base presenter and adjust for other model types
Marcus Moore2acb38a6a51d
diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.phpindex 6dc0700b23..f6189c61b1 100644--- a/app/Presenters/AssetPresenter.php+++ b/app/Presenters/AssetPresenter.php@@ -3,7 +3,6 @@namespace App\Presenters;use App\Models\CustomField;-use App\Models\Setting;use Carbon\CarbonImmutable;use DateTime;use Illuminate\Support\Facades\Storage;@@ -673,21 +672,6 @@ class AssetPresenter extends Presenterreturn false;}- /**- * Used to take user created URL and dynamically fill in the needed values per asset- *- * @return string- */- public function dynamicUrl($dynamic_url)- {- $url = (str_replace('{LOCALE}', Setting::getSettings()->locale, $dynamic_url));- $url = (str_replace('{SERIAL}', urlencode($this->model->serial), $url));- $url = (str_replace('{MODEL_NAME}', urlencode($this->model->model->name), $url));- $url = (str_replace('{MODEL_NUMBER}', urlencode($this->model->model->model_number), $url));-- return $url;- }-/*** Url to view this item.*diff --git a/app/Presenters/Presenter.php b/app/Presenters/Presenter.phpindex f461937fd3..dc54ad6fb5 100644--- a/app/Presenters/Presenter.php+++ b/app/Presenters/Presenter.php@@ -2,6 +2,8 @@namespace App\Presenters;+use App\Models\Asset;+use App\Models\Setting;use App\Models\SnipeModel;abstract class Presenter@@ -98,6 +100,31 @@ abstract class Presenterreturn '';}+ /**+ * Used to take user created URL and dynamically fill in the needed values per item+ *+ * @return string+ */+ public function dynamicUrl($dynamic_url)+ {+ $url = (str_replace('{LOCALE}', Setting::getSettings()->locale, $dynamic_url));++ if ($this->model instanceof Asset) {+ $url = (str_replace('{SERIAL}', urlencode($this->model->serial), $url));+ $url = (str_replace('{MODEL_NAME}', urlencode($this->model->model->name), $url));+ $url = (str_replace('{MODEL_NUMBER}', urlencode($this->model->model->model_number), $url));++ return $url;+ }++ $url = (str_replace('{SERIAL}', urlencode($this->serial), $url));+ $url = (str_replace('{MODEL_NAME}', urlencode($this->model_name), $url));+ $url = (str_replace('{MODEL_NUMBER}', urlencode($this->model_number), $url));++ return $url;++ }+public function __get($property){if (method_exists($this, $property)) {diff --git a/tests/Unit/Presenters/AccessoryPresenterTest.php b/tests/Unit/Presenters/AccessoryPresenterTest.phpnew file mode 100644index 0000000000..0e410adb5e--- /dev/null+++ b/tests/Unit/Presenters/AccessoryPresenterTest.php@@ -0,0 +1,21 @@+<?php++namespace Tests\Unit\Presenters;++use App\Models\Accessory;+use Tests\TestCase;++class AccessoryPresenterTest extends TestCase+{+ public function test_dynamic_url()+ {+ $this->settings->set(['locale' => 'en-US']);++ $accessory = Accessory::factory()->create(['model_number' => 'MN-123']);++ $this->assertEquals(+ 'https://example.com/en-US/MN-123',+ $accessory->present()->dynamicUrl('https://example.com/{LOCALE}/{MODEL_NUMBER}')+ );+ }+}diff --git a/tests/Unit/Presenters/AssetPresenterTest.php b/tests/Unit/Presenters/AssetPresenterTest.phpnew file mode 100644index 0000000000..b16bc2888d--- /dev/null+++ b/tests/Unit/Presenters/AssetPresenterTest.php@@ -0,0 +1,29 @@+<?php++namespace Tests\Unit\Presenters;++use App\Models\Asset;+use App\Models\AssetModel;+use Tests\TestCase;++class AssetPresenterTest extends TestCase+{+ public function test_dynamic_url()+ {+ $this->settings->set(['locale' => 'en-US']);++ $assetModel = AssetModel::factory()->create([+ 'model_number' => 'MN-123',+ 'name' => 'Macbook',+ ]);++ $asset = Asset::factory()+ ->for($assetModel, 'model')+ ->create(['serial' => 'SN-123']);++ $this->assertEquals(+ 'https://example.com/en-US/SN-123/MN-123/Macbook',+ $asset->present()->dynamicUrl('https://example.com/{LOCALE}/{SERIAL}/{MODEL_NUMBER}/{MODEL_NAME}')+ );+ }+}diff --git a/tests/Unit/Presenters/ComponentPresenterTest.php b/tests/Unit/Presenters/ComponentPresenterTest.phpnew file mode 100644index 0000000000..1084f3f034--- /dev/null+++ b/tests/Unit/Presenters/ComponentPresenterTest.php@@ -0,0 +1,24 @@+<?php++namespace Tests\Unit\Presenters;++use App\Models\Component;+use Tests\TestCase;++class ComponentPresenterTest extends TestCase+{+ public function test_dynamic_url()+ {+ $this->settings->set(['locale' => 'en-US']);++ $component = Component::factory()->create([+ 'serial' => 'SN-123',+ 'model_number' => 'MN-123',+ ]);++ $this->assertEquals(+ 'https://example.com/en-US/SN-123/MN-123',+ $component->present()->dynamicUrl('https://example.com/{LOCALE}/{SERIAL}/{MODEL_NUMBER}')+ );+ }+}diff --git a/tests/Unit/Presenters/ConsumablePresenterTest.php b/tests/Unit/Presenters/ConsumablePresenterTest.phpnew file mode 100644index 0000000000..780341b573--- /dev/null+++ b/tests/Unit/Presenters/ConsumablePresenterTest.php@@ -0,0 +1,21 @@+<?php++namespace Tests\Unit\Presenters;++use App\Models\Consumable;+use Tests\TestCase;++class ConsumablePresenterTest extends TestCase+{+ public function test_dynamic_url()+ {+ $this->settings->set(['locale' => 'en-US']);++ $consumable = Consumable::factory()->create(['model_number' => 'MN-123']);++ $this->assertEquals(+ 'https://example.com/en-US/MN-123',+ $consumable->present()->dynamicUrl('https://example.com/{LOCALE}/{MODEL_NUMBER}')+ );+ }+}diff --git a/tests/Unit/Presenters/LicensePresenterTest.php b/tests/Unit/Presenters/LicensePresenterTest.phpnew file mode 100644index 0000000000..b731e05b53--- /dev/null+++ b/tests/Unit/Presenters/LicensePresenterTest.php@@ -0,0 +1,21 @@+<?php++namespace Tests\Unit\Presenters;++use App\Models\License;+use Tests\TestCase;++class LicensePresenterTest extends TestCase+{+ public function test_dynamic_url()+ {+ $this->settings->set(['locale' => 'en-US']);++ $license = License::factory()->create();++ $this->assertEquals(+ 'https://example.com/en-US',+ $license->present()->dynamicUrl('https://example.com/{LOCALE}')+ );+ }+}