]> BookStack Code Mirror - bookstack/blob - tests/Commands/AssignSortSetCommandTest.php
Sorting: Renamed sort set to sort rule
[bookstack] / tests / Commands / AssignSortSetCommandTest.php
1 <?php
2
3 namespace Commands;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Sorting\SortRule;
7 use Tests\TestCase;
8
9 class AssignSortSetCommandTest extends TestCase
10 {
11     public function test_no_given_sort_set_lists_options()
12     {
13         $sortSets = SortRule::factory()->createMany(10);
14
15         $commandRun = $this->artisan('bookstack:assign-sort-set')
16             ->expectsOutputToContain('Sort set ID required!')
17             ->assertExitCode(1);
18
19         foreach ($sortSets as $sortSet) {
20             $commandRun->expectsOutputToContain("{$sortSet->id}: {$sortSet->name}");
21         }
22     }
23
24     public function test_run_without_options_advises_help()
25     {
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.")
28             ->assertExitCode(1);
29     }
30
31     public function test_run_without_valid_sort_advises_help()
32     {
33         $this->artisan("bookstack:assign-sort-set 100342 --all-books")
34             ->expectsOutput("Sort set of provided id 100342 not found!")
35             ->assertExitCode(1);
36     }
37
38     public function test_confirmation_required()
39     {
40         $sortSet = SortRule::factory()->create();
41
42         $this->artisan("bookstack:assign-sort-set {$sortSet->id} --all-books")
43             ->expectsConfirmation('Are you sure you want to continue?', 'no')
44             ->assertExitCode(1);
45
46         $booksWithSort = Book::query()->whereNotNull('sort_set_id')->count();
47         $this->assertEquals(0, $booksWithSort);
48     }
49
50     public function test_assign_to_all_books()
51     {
52         $sortSet = SortRule::factory()->create();
53         $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
54         $this->assertGreaterThan(0, $booksWithoutSort);
55
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)")
60             ->assertExitCode(0);
61
62         $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
63         $this->assertEquals(0, $booksWithoutSort);
64     }
65
66     public function test_assign_to_all_books_without_sort()
67     {
68         $totalBooks = Book::query()->count();
69         $book = $this->entities->book();
70         $sortSetA = SortRule::factory()->create();
71         $sortSetB = SortRule::factory()->create();
72         $book->sort_rule_id = $sortSetA->id;
73         $book->save();
74
75         $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
76         $this->assertEquals($totalBooks, $booksWithoutSort + 1);
77
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)")
81             ->assertExitCode(0);
82
83         $booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
84         $this->assertEquals(0, $booksWithoutSort);
85         $this->assertEquals($totalBooks, $sortSetB->books()->count() + 1);
86     }
87
88     public function test_assign_to_all_books_with_sort()
89     {
90         $book = $this->entities->book();
91         $sortSetA = SortRule::factory()->create();
92         $sortSetB = SortRule::factory()->create();
93         $book->sort_rule_id = $sortSetA->id;
94         $book->save();
95
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)")
99             ->assertExitCode(0);
100
101         $book->refresh();
102         $this->assertEquals($sortSetB->id, $book->sort_rule_id);
103         $this->assertEquals(1, $sortSetB->books()->count());
104     }
105
106     public function test_assign_to_all_books_with_sort_id_is_validated()
107     {
108         $this->artisan("bookstack:assign-sort-set 50 --books-with-sort=beans")
109             ->expectsOutputToContain("Provided --books-with-sort option value is invalid")
110             ->assertExitCode(1);
111     }
112 }