From 7b16171941e1cca7222ef31a802d3260351a181a Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 28 Aug 2024 14:14:00 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- .../QueuedJobDescriptorExtension.php | 2 +- src/Tasks/GarbageCollectionTask.php | 25 ++++++------------- src/Tasks/InvalidateAllSessionsTask.php | 24 +++++++----------- .../Tasks/InvalidateAllSessionsTaskTest.php | 9 ++++++- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/Extensions/QueuedJobDescriptorExtension.php b/src/Extensions/QueuedJobDescriptorExtension.php index 709a7c6..f2db88a 100644 --- a/src/Extensions/QueuedJobDescriptorExtension.php +++ b/src/Extensions/QueuedJobDescriptorExtension.php @@ -12,7 +12,7 @@ class QueuedJobDescriptorExtension extends Extension { /** - * Called on dev/build by DatabaseAdmin + * Called on dev/build by DevBuild */ protected function onAfterBuild(): void { diff --git a/src/Tasks/GarbageCollectionTask.php b/src/Tasks/GarbageCollectionTask.php index 8ae1248..b55a593 100644 --- a/src/Tasks/GarbageCollectionTask.php +++ b/src/Tasks/GarbageCollectionTask.php @@ -3,31 +3,22 @@ namespace SilverStripe\Tasks; use SilverStripe\Dev\BuildTask; +use SilverStripe\HybridExecution\HybridOutput; use SilverStripe\SessionManager\Services\GarbageCollectionService; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; class GarbageCollectionTask extends BuildTask { - /** - * @var string - */ - private static $segment = 'LoginSessionGarbageCollectionTask'; + protected static string $commandName = 'LoginSessionGarbageCollectionTask'; - /** - * @var string - */ - protected $title = 'Login Session Garbage Collection Task'; + protected string $title = 'Login Session Garbage Collection Task'; - /** - * @var string - */ - protected $description = 'Removes expired login sessions and “remember me” hashes from the database'; + protected static string $description = 'Removes expired login sessions and "remember me" hashes from the database'; - /** - * @param HTTPRequest $request - */ - public function run($request) + protected function execute(InputInterface $input, HybridOutput $output): int { GarbageCollectionService::singleton()->collect(); - echo "Garbage collection completed successfully\n"; + return Command::SUCCESS; } } diff --git a/src/Tasks/InvalidateAllSessionsTask.php b/src/Tasks/InvalidateAllSessionsTask.php index 6687d74..e020674 100644 --- a/src/Tasks/InvalidateAllSessionsTask.php +++ b/src/Tasks/InvalidateAllSessionsTask.php @@ -2,32 +2,26 @@ namespace SilverStripe\SessionManager\Tasks; -use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\BuildTask; +use SilverStripe\HybridExecution\HybridOutput; use SilverStripe\Security\RememberLoginHash; use SilverStripe\SessionManager\Models\LoginSession; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; class InvalidateAllSessionsTask extends BuildTask { - private static string $segment = 'InvalidateAllSessions'; + protected static string $commandName = 'InvalidateAllSessions'; - /** - * @var string - */ - protected $title = 'Invalidate All Login Sessions Task'; + protected string $title = 'Invalidate All Login Sessions Task'; - /** - * @var string - */ - protected $description = 'Removes all login sessions and "remember me" hashes (including yours) from the database'; + protected static string $description = 'Removes all login sessions and "remember me" hashes' + . ' (including yours) from the database'; - /** - * @param HTTPRequest $request - */ - public function run($request) + protected function execute(InputInterface $input, HybridOutput $output): int { LoginSession::get()->removeAll(); RememberLoginHash::get()->removeAll(); - echo "Session removal completed successfully\n"; + return Command::SUCCESS; } } diff --git a/tests/php/Tasks/InvalidateAllSessionsTaskTest.php b/tests/php/Tasks/InvalidateAllSessionsTaskTest.php index 64d905a..345963b 100644 --- a/tests/php/Tasks/InvalidateAllSessionsTaskTest.php +++ b/tests/php/Tasks/InvalidateAllSessionsTaskTest.php @@ -6,6 +6,7 @@ use SilverStripe\Control\Tests\HttpRequestMockBuilder; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\SapphireTest; +use SilverStripe\HybridExecution\HybridOutput; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\Security\AuthenticationHandler; use SilverStripe\Security\Member; @@ -15,6 +16,8 @@ use SilverStripe\SessionManager\Middleware\LoginSessionMiddleware; use SilverStripe\SessionManager\Models\LoginSession; use SilverStripe\SessionManager\Tasks\InvalidateAllSessionsTask; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\BufferedOutput; class InvalidateAllSessionsTaskTest extends SapphireTest { @@ -46,7 +49,11 @@ public function testRemoveAllSessions() // Completely invalidate all current sessions and login hashes $task = new InvalidateAllSessionsTask(); - $task->run($request); + $buffer = new BufferedOutput(); + $output = new HybridOutput(HybridOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); // Assert all sessions are removed from the database $this->assertSame(0, LoginSession::get()->count(), 'There are no LoginSessions');