From 823acde49d00870e1ee55805b46cbb294b4f808b Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Wed, 8 Mar 2023 09:41:57 +0100 Subject: [PATCH] Fix the TwoFAccount Export feature & add related tests --- app/Services/TwoFAccountService.php | 2 ++ .../Services/TwoFAccountServiceTest.php | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/Services/TwoFAccountService.php b/app/Services/TwoFAccountService.php index acdd0fea..8747dac8 100644 --- a/app/Services/TwoFAccountService.php +++ b/app/Services/TwoFAccountService.php @@ -68,6 +68,8 @@ class TwoFAccountService public static function export($ids) : Collection { $ids = Helpers::commaSeparatedToArray($ids); + $ids = is_array($ids) ? $ids : func_get_args(); + $twofaccounts = TwoFAccount::whereIn('id', $ids)->get(); return $twofaccounts; diff --git a/tests/Feature/Services/TwoFAccountServiceTest.php b/tests/Feature/Services/TwoFAccountServiceTest.php index 77c400a5..59f14cd9 100644 --- a/tests/Feature/Services/TwoFAccountServiceTest.php +++ b/tests/Feature/Services/TwoFAccountServiceTest.php @@ -230,6 +230,41 @@ class TwoFAccountServiceTest extends FeatureTestCase $twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA); } + /** + * @test + */ + public function test_export_single_id_returns_collection() + { + $twofaccounts = TwoFAccounts::export($this->customTotpTwofaccount->id); + + $this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts); + $this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first()); + } + + /** + * @test + */ + public function test_export_comma_separated_ids_returns_collection() + { + $twofaccounts = TwoFAccounts::export($this->customTotpTwofaccount->id . ',' . $this->customHotpTwofaccount->id); + + $this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts); + $this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first()); + $this->assertObjectEquals($this->customHotpTwofaccount, $twofaccounts->last()); + } + + /** + * @test + */ + public function test_export_array_of_ids_returns_collection() + { + $twofaccounts = TwoFAccounts::export([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]); + + $this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts); + $this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first()); + $this->assertObjectEquals($this->customHotpTwofaccount, $twofaccounts->last()); + } + /** * @test */