5 use BookStack\Entities\Models\Book;
6 use BookStack\Sorting\SortSet;
9 class AssignSortSetCommandTest extends TestCase
11 public function test_no_given_sort_set_lists_options()
13 $sortSets = SortSet::factory()->createMany(10);
15 $commandRun = $this->artisan('bookstack:assign-sort-set')
16 ->expectsOutputToContain('Sort set ID required!')
19 foreach ($sortSets as $sortSet) {
20 $commandRun->expectsOutputToContain("{$sortSet->id}: {$sortSet->name}");
24 public function test_run_without_options_advises_help()
26 $this->artisan("bookstack:assign-sort-set 100")
27 ->expectsOutput("No option provided to specify target. Run with the -h option to see all available options.")
31 public function test_run_without_valid_sort_advises_help()
33 $this->artisan("bookstack:assign-sort-set 100342 --all-books")
34 ->expectsOutput("Sort set of provided id 100342 not found!")
38 public function test_confirmation_required()
40 $sortSet = SortSet::factory()->create();
42 $this->artisan("bookstack:assign-sort-set {$sortSet->id} --all-books")
43 ->expectsConfirmation('Are you sure you want to continue?', 'no')
46 $booksWithSort = Book::query()->whereNotNull('sort_set_id')->count();
47 $this->assertEquals(0, $booksWithSort);
50 public function test_assign_to_all_books()
52 $sortSet = SortSet::factory()->create();
53 $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
54 $this->assertGreaterThan(0, $booksWithoutSort);
56 $this->artisan("bookstack:assign-sort-set {$sortSet->id} --all-books")
57 ->expectsOutputToContain("This will apply sort set [{$sortSet->id}: {$sortSet->name}] to {$booksWithoutSort} book(s)")
58 ->expectsConfirmation('Are you sure you want to continue?', 'yes')
59 ->expectsOutputToContain("Sort applied to {$booksWithoutSort} book(s)")
62 $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
63 $this->assertEquals(0, $booksWithoutSort);
66 public function test_assign_to_all_books_without_sort()
68 $totalBooks = Book::query()->count();
69 $book = $this->entities->book();
70 $sortSetA = SortSet::factory()->create();
71 $sortSetB = SortSet::factory()->create();
72 $book->sort_set_id = $sortSetA->id;
75 $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
76 $this->assertEquals($totalBooks, $booksWithoutSort + 1);
78 $this->artisan("bookstack:assign-sort-set {$sortSetB->id} --books-without-sort")
79 ->expectsConfirmation('Are you sure you want to continue?', 'yes')
80 ->expectsOutputToContain("Sort applied to {$booksWithoutSort} book(s)")
83 $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
84 $this->assertEquals(0, $booksWithoutSort);
85 $this->assertEquals($totalBooks, $sortSetB->books()->count() + 1);
88 public function test_assign_to_all_books_with_sort()
90 $book = $this->entities->book();
91 $sortSetA = SortSet::factory()->create();
92 $sortSetB = SortSet::factory()->create();
93 $book->sort_set_id = $sortSetA->id;
96 $this->artisan("bookstack:assign-sort-set {$sortSetB->id} --books-with-sort={$sortSetA->id}")
97 ->expectsConfirmation('Are you sure you want to continue?', 'yes')
98 ->expectsOutputToContain("Sort applied to 1 book(s)")
102 $this->assertEquals($sortSetB->id, $book->sort_set_id);
103 $this->assertEquals(1, $sortSetB->books()->count());
106 public function test_assign_to_all_books_with_sort_id_is_validated()
108 $this->artisan("bookstack:assign-sort-set 50 --books-with-sort=beans")
109 ->expectsOutputToContain("Provided --books-with-sort option value is invalid")