Page MenuHomePhorge

D5325.1775256277.diff
No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None

D5325.1775256277.diff

diff --git a/src/app/Console/Commands/Wallet/AddControllerCommand.php b/src/app/Console/Commands/Wallet/AddControllerCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/Wallet/AddControllerCommand.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Console\Commands\Wallet;
+
+use App\Console\Command;
+
+class AddControllerCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'wallet:add-controller {wallet} {user}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Add a controller to a wallet';
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $wallet = $this->getWallet($this->argument('wallet'));
+
+ if (!$wallet) {
+ $this->error("Wallet not found.");
+ return 1;
+ }
+
+ $user = $this->getUser($this->argument('user'));
+
+ if (!$user) {
+ $this->error("User not found.");
+ return 1;
+ }
+
+ $user_wallet = $user->wallet();
+
+ if (!$user_wallet || $user_wallet->id !== $wallet->id) {
+ $this->error("User does not belong to this wallet.");
+ return 1;
+ }
+
+ $wallet->addController($user);
+ }
+}
diff --git a/src/app/Console/Commands/Wallet/RemoveControllerCommand.php b/src/app/Console/Commands/Wallet/RemoveControllerCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/Wallet/RemoveControllerCommand.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Console\Commands\Wallet;
+
+use App\Console\Command;
+
+class RemoveControllerCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'wallet:remove-controller {wallet} {user}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Remove a controller from a wallet';
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $wallet = $this->getWallet($this->argument('wallet'));
+
+ if (!$wallet) {
+ $this->error("Wallet not found.");
+ return 1;
+ }
+
+ $user = $this->getUser($this->argument('user'));
+
+ if (!$user) {
+ $this->error("User not found.");
+ return 1;
+ }
+
+ if ($wallet->owner?->id == $user->id) {
+ $this->error("User is the wallet owner.");
+ return 1;
+ }
+
+ if (!$wallet->isController($user)) {
+ $this->error("User is not the wallet controller.");
+ return 1;
+ }
+
+ $wallet->removeController($user);
+ }
+}
diff --git a/src/app/Wallet.php b/src/app/Wallet.php
--- a/src/app/Wallet.php
+++ b/src/app/Wallet.php
@@ -66,6 +66,7 @@
{
if (!$this->controllers->contains($user)) {
$this->controllers()->save($user);
+ $this->unsetRelation('controllers');
}
}
@@ -456,6 +457,7 @@
{
if ($this->controllers->contains($user)) {
$this->controllers()->detach($user);
+ $this->unsetRelation('controllers');
}
}
diff --git a/src/tests/Feature/Console/Wallet/AddControllerTest.php b/src/tests/Feature/Console/Wallet/AddControllerTest.php
new file mode 100644
--- /dev/null
+++ b/src/tests/Feature/Console/Wallet/AddControllerTest.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Tests\Feature\Console\Wallet;
+
+use App\Sku;
+use Tests\TestCase;
+
+class AddControllerTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+ $this->deleteTestUser('wallets-controller-user@kolabnow.com');
+ }
+
+ protected function tearDown(): void
+ {
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+ $this->deleteTestUser('wallets-controller-user@kolabnow.com');
+
+ parent::tearDown();
+ }
+
+ /**
+ * Test command run for a specified wallet
+ */
+ public function testHandle(): void
+ {
+ $owner = $this->getTestUser('wallets-controller@kolabnow.com');
+ $user = $this->getTestUser('wallets-controller-user@kolabnow.com');
+ $sku = Sku::withEnvTenantContext()->where('title', 'mailbox')->first();
+ $wallet = $owner->wallets()->first();
+ $user->assignSku($sku, 1, $wallet);
+
+ // Invalid wallet id
+ $code = \Artisan::call("wallet:add-controller 123 {$user->id}");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("Wallet not found.", $output);
+
+ // Invalid user id
+ $code = \Artisan::call("wallet:add-controller {$wallet->id} 123");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("User not found.", $output);
+
+ // User from another account
+ $code = \Artisan::call("wallet:add-controller {$wallet->id} jack@kolab.org");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("User does not belong to this wallet.", $output);
+
+ // Success
+ $code = \Artisan::call("wallet:add-controller {$wallet->id} {$user->id}");
+ $output = trim(\Artisan::output());
+ $this->assertSame(0, $code);
+ $this->assertSame("", $output);
+
+ $this->assertTrue($wallet->fresh()->isController($user));
+ }
+}
diff --git a/src/tests/Feature/Console/Wallet/RemoveControllerTest.php b/src/tests/Feature/Console/Wallet/RemoveControllerTest.php
new file mode 100644
--- /dev/null
+++ b/src/tests/Feature/Console/Wallet/RemoveControllerTest.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Tests\Feature\Console\Wallet;
+
+use Tests\TestCase;
+
+class RemoveControllerTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+ $this->deleteTestUser('wallets-controller-user@kolabnow.com');
+ }
+
+ protected function tearDown(): void
+ {
+ $this->deleteTestUser('wallets-controller@kolabnow.com');
+ $this->deleteTestUser('wallets-controller-user@kolabnow.com');
+
+ parent::tearDown();
+ }
+
+ /**
+ * Test command run for a specified wallet
+ */
+ public function testHandle(): void
+ {
+ $owner = $this->getTestUser('wallets-controller@kolabnow.com');
+ $user = $this->getTestUser('wallets-controller-user@kolabnow.com');
+ $wallet = $owner->wallets()->first();
+ $wallet->addController($user);
+
+ // Invalid wallet id
+ $code = \Artisan::call("wallet:remove-controller 123 {$user->id}");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("Wallet not found.", $output);
+
+ // Invalid user id
+ $code = \Artisan::call("wallet:remove-controller {$wallet->id} 123");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("User not found.", $output);
+
+ // Account owner is not a controller
+ $code = \Artisan::call("wallet:remove-controller {$wallet->id} {$owner->email}");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("User is the wallet owner.", $output);
+
+ // User not a controller
+ $code = \Artisan::call("wallet:remove-controller {$wallet->id} jack@kolab.org");
+ $output = trim(\Artisan::output());
+ $this->assertSame(1, $code);
+ $this->assertSame("User is not the wallet controller.", $output);
+
+ // Success
+ $code = \Artisan::call("wallet:remove-controller {$wallet->id} {$user->id}");
+ $output = trim(\Artisan::output());
+ $this->assertSame(0, $code);
+ $this->assertSame("", $output);
+
+ $this->assertFalse($wallet->fresh()->isController($user));
+ }
+}
diff --git a/src/tests/Feature/WalletTest.php b/src/tests/Feature/WalletTest.php
--- a/src/tests/Feature/WalletTest.php
+++ b/src/tests/Feature/WalletTest.php
@@ -428,21 +428,25 @@
$userA = $this->getTestUser('UserWallet1@UserWallet.com');
$userB = $this->getTestUser('UserWallet2@UserWallet.com');
+ $this->assertSame(0, $userB->accounts()->count());
+
$userA->wallets()->each(
- static function ($wallet) use ($userB) {
+ function ($wallet) use ($userB) {
$wallet->addController($userB);
+ $this->assertTrue($wallet->isController($userB));
}
);
- $userB->refresh();
+ $this->assertSame(1, $userB->accounts()->count());
$userB->accounts()->each(
- static function ($wallet) use ($userB) {
+ function ($wallet) use ($userB) {
$wallet->removeController($userB);
+ $this->assertFalse($wallet->isController($userB));
}
);
- $this->assertCount(0, $userB->accounts);
+ $this->assertSame(0, $userB->accounts()->count());
}
/**

File Metadata

Mime Type
text/plain
Expires
Fri, Apr 3, 10:44 PM (8 h, 38 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18824248
Default Alt Text
D5325.1775256277.diff (9 KB)

Event Timeline