1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2026-01-03 23:42:28 +03:00

Covered untested commands with testing

This commit is contained in:
Dan Brown
2023-05-24 10:34:43 +01:00
parent 3b31ac75ec
commit 0704f1bd0d
6 changed files with 141 additions and 2 deletions

View File

@@ -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]);
}
}

View File

@@ -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');
});
}
}

View File

@@ -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();
}
}

View File

@@ -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;');
}
}