]> BookStack Code Mirror - bookstack/blob - tests/Commands/AssignSortRuleCommandTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Commands / AssignSortRuleCommandTest.php
1 <?php
2
3 namespace Tests\Commands;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Sorting\SortRule;
7 use Tests\TestCase;
8
9 class AssignSortRuleCommandTest extends TestCase
10 {
11     public function test_no_given_sort_rule_lists_options()
12     {
13         $sortRules = SortRule::factory()->createMany(10);
14
15         $commandRun = $this->artisan('bookstack:assign-sort-rule')
16             ->expectsOutputToContain('Sort rule ID required!')
17             ->assertExitCode(1);
18
19         foreach ($sortRules as $sortRule) {
20             $commandRun->expectsOutputToContain("{$sortRule->id}: {$sortRule->name}");
21         }
22     }
23
24     public function test_run_without_options_advises_help()
25     {
26         $this->artisan("bookstack:assign-sort-rule 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-rule 100342 --all-books")
34             ->expectsOutput("Sort rule of provided id 100342 not found!")
35             ->assertExitCode(1);
36     }
37
38     public function test_confirmation_required()
39     {
40         $sortRule = SortRule::factory()->create();
41
42         $this->artisan("bookstack:assign-sort-rule {$sortRule->id} --all-books")
43             ->expectsConfirmation('Are you sure you want to continue?', 'no')
44             ->assertExitCode(1);
45
46         $booksWithSort = Book::query()->whereNotNull('sort_rule_id')->count();
47         $this->assertEquals(0, $booksWithSort);
48     }
49
50     public function test_assign_to_all_books()
51     {
52         $sortRule = SortRule::factory()->create();
53         $booksWithoutSort = Book::query()->whereNull('sort_rule_id')->count();
54         $this->assertGreaterThan(0, $booksWithoutSort);
55
56         $this->artisan("bookstack:assign-sort-rule {$sortRule->id} --all-books")
57             ->expectsOutputToContain("This will apply sort rule [{$sortRule->id}: {$sortRule->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_rule_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         $sortRuleA = SortRule::factory()->create();
71         $sortRuleB = SortRule::factory()->create();
72         $book->sort_rule_id = $sortRuleA->id;
73         $book->save();
74
75         $booksWithoutSort = Book::query()->whereNull('sort_rule_id')->count();
76         $this->assertEquals($totalBooks, $booksWithoutSort + 1);
77
78         $this->artisan("bookstack:assign-sort-rule {$sortRuleB->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_rule_id')->count();
84         $this->assertEquals(0, $booksWithoutSort);
85         $this->assertEquals($totalBooks, $sortRuleB->books()->count() + 1);
86     }
87
88     public function test_assign_to_all_books_with_sort()
89     {
90         $book = $this->entities->book();
91         $sortRuleA = SortRule::factory()->create();
92         $sortRuleB = SortRule::factory()->create();
93         $book->sort_rule_id = $sortRuleA->id;
94         $book->save();
95
96         $this->artisan("bookstack:assign-sort-rule {$sortRuleB->id} --books-with-sort={$sortRuleA->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($sortRuleB->id, $book->sort_rule_id);
103         $this->assertEquals(1, $sortRuleB->books()->count());
104     }
105
106     public function test_assign_to_all_books_with_sort_id_is_validated()
107     {
108         $this->artisan("bookstack:assign-sort-rule 50 --books-with-sort=beans")
109             ->expectsOutputToContain("Provided --books-with-sort option value is invalid")
110             ->assertExitCode(1);
111     }
112 }