]> BookStack Code Mirror - bookstack/commitdiff
Covered untested commands with testing
authorDan Brown <redacted>
Wed, 24 May 2023 09:34:43 +0000 (10:34 +0100)
committerDan Brown <redacted>
Wed, 24 May 2023 09:34:43 +0000 (10:34 +0100)
app/Console/Commands/CleanupImages.php
app/Uploads/ImageService.php
tests/Commands/CleanupImagesCommandTest.php [new file with mode: 0644]
tests/Commands/DeleteUsersCommandTest.php [new file with mode: 0644]
tests/Commands/RegenerateSearchCommandTest.php [new file with mode: 0644]
tests/Commands/UpgradeDatabaseEncodingCommandTest.php [new file with mode: 0644]

index 7221501979a529861e8e505ce66f9f79d7771160..2399e1cbb341460a2af5946648b9d57e344612b2 100644 (file)
@@ -49,7 +49,8 @@ class CleanupImages extends Command
         $dryRun = $this->option('force') ? false : true;
 
         if (!$dryRun) {
-            $proceed = $this->confirm("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\nAre you sure you want to proceed?");
+            $this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
+            $proceed = $this->confirm("Are you sure you want to proceed?");
             if (!$proceed) {
                 return;
             }
@@ -59,7 +60,7 @@ class CleanupImages extends Command
         $deleteCount = count($deleted);
 
         if ($dryRun) {
-            $this->comment('Dry run, No images have been deleted');
+            $this->comment('Dry run, no images have been deleted');
             $this->comment($deleteCount . ' images found that would have been deleted');
             $this->showDeletedImages($deleted);
             $this->comment('Run with -f or --force to perform deletions');
index 1d04582c094ef910cef05a33098ddda9819293a0..5458779e943906db62a58cce8f12587a248d5139 100644 (file)
@@ -462,6 +462,7 @@ class ImageService
 
         Image::query()->whereIn('type', $types)
             ->chunk(1000, function ($images) use ($checkRevisions, &$deletedPaths, $dryRun) {
+                /** @var Image $image */
                 foreach ($images as $image) {
                     $searchQuery = '%' . basename($image->path) . '%';
                     $inPage = DB::table('pages')
diff --git a/tests/Commands/CleanupImagesCommandTest.php b/tests/Commands/CleanupImagesCommandTest.php
new file mode 100644 (file)
index 0000000..a1a5ab9
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace Tests\Commands;
+
+use BookStack\Uploads\Image;
+use Tests\TestCase;
+
+class CleanupImagesCommandTest extends TestCase
+{
+    public function test_command_defaults_to_dry_run()
+    {
+        $page = $this->entities->page();
+        $image = Image::factory()->create(['uploaded_to' => $page->id]);
+
+        $this->artisan('bookstack:cleanup-images -v')
+            ->expectsOutput('Dry run, no images have been deleted')
+            ->expectsOutput('1 images found that would have been deleted')
+            ->expectsOutputToContain($image->path)
+            ->assertExitCode(0);
+
+        $this->assertDatabaseHas('images', ['id' => $image->id]);
+    }
+
+    public function test_command_force_run()
+    {
+        $page = $this->entities->page();
+        $image = Image::factory()->create(['uploaded_to' => $page->id]);
+
+        $this->artisan('bookstack:cleanup-images --force')
+            ->expectsOutputToContain('This operation is destructive and is not guaranteed to be fully accurate')
+            ->expectsConfirmation('Are you sure you want to proceed?', 'yes')
+            ->expectsOutput('1 images deleted')
+            ->assertExitCode(0);
+
+        $this->assertDatabaseMissing('images', ['id' => $image->id]);
+    }
+
+    public function test_command_force_run_negative_confirmation()
+    {
+        $page = $this->entities->page();
+        $image = Image::factory()->create(['uploaded_to' => $page->id]);
+
+        $this->artisan('bookstack:cleanup-images --force')
+            ->expectsConfirmation('Are you sure you want to proceed?', 'no')
+            ->assertExitCode(0);
+
+        $this->assertDatabaseHas('images', ['id' => $image->id]);
+    }
+}
diff --git a/tests/Commands/DeleteUsersCommandTest.php b/tests/Commands/DeleteUsersCommandTest.php
new file mode 100644 (file)
index 0000000..4d8081b
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace Tests\Commands;
+
+use BookStack\Users\Models\User;
+use Illuminate\Database\Eloquent\Collection;
+use Tests\TestCase;
+
+class DeleteUsersCommandTest extends TestCase
+{
+    public function test_command_deletes_users()
+    {
+        $userCount = User::query()->count();
+        $normalUsers = $this->getNormalUsers();
+
+        $normalUserCount = $userCount - count($normalUsers);
+        $this->artisan('bookstack:delete-users')
+            ->expectsQuestion('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)', 'yes')
+            ->expectsOutputToContain("Deleted $normalUserCount of $userCount total users.")
+            ->assertExitCode(0);
+
+        $this->assertDatabaseMissing('users', ['id' => $normalUsers->first()->id]);
+    }
+
+    public function test_command_requires_confirmation()
+    {
+        $normalUsers = $this->getNormalUsers();
+
+        $this->artisan('bookstack:delete-users')
+            ->expectsQuestion('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)', 'no')
+            ->assertExitCode(0);
+
+        $this->assertDatabaseHas('users', ['id' => $normalUsers->first()->id]);
+    }
+
+    protected function getNormalUsers(): Collection
+    {
+        return User::query()->whereNull('system_name')
+            ->get()
+            ->filter(function (User $user) {
+                return !$user->hasSystemRole('admin');
+            });
+    }
+}
diff --git a/tests/Commands/RegenerateSearchCommandTest.php b/tests/Commands/RegenerateSearchCommandTest.php
new file mode 100644 (file)
index 0000000..418327e
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace Tests\Commands;
+
+use BookStack\Search\SearchTerm;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class RegenerateSearchCommandTest extends TestCase
+{
+    public function test_command_regenerates_index()
+    {
+        DB::rollBack();
+        $page = $this->entities->page();
+        SearchTerm::truncate();
+
+        $this->assertDatabaseMissing('search_terms', ['entity_id' => $page->id]);
+
+        $this->artisan('bookstack:regenerate-search')
+            ->expectsOutput('Search index regenerated!')
+            ->assertExitCode(0);
+
+        $this->assertDatabaseHas('search_terms', [
+            'entity_type' => 'page',
+            'entity_id' => $page->id
+        ]);
+        DB::beginTransaction();
+    }
+}
diff --git a/tests/Commands/UpgradeDatabaseEncodingCommandTest.php b/tests/Commands/UpgradeDatabaseEncodingCommandTest.php
new file mode 100644 (file)
index 0000000..b7fe4eb
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+namespace Tests\Commands;
+
+use Tests\TestCase;
+
+class UpgradeDatabaseEncodingCommandTest extends TestCase
+{
+    public function test_command_outputs_sql()
+    {
+        $this->artisan('bookstack:db-utf8mb4')
+            ->expectsOutputToContain('ALTER DATABASE')
+            ->expectsOutputToContain('ALTER TABLE `users` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
+    }
+}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.