5 use BookStack\Entities\Models\Book;
6 use BookStack\Sorting\SortRule;
9 class AssignSortRuleCommandTest extends TestCase
11 public function test_no_given_sort_rule_lists_options()
13 $sortRules = SortRule::factory()->createMany(10);
15 $commandRun = $this->artisan('bookstack:assign-sort-rule')
16 ->expectsOutputToContain('Sort rule ID required!')
19 foreach ($sortRules as $sortRule) {
20 $commandRun->expectsOutputToContain("{$sortRule->id}: {$sortRule->name}");
24 public function test_run_without_options_advises_help()
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.")
31 public function test_run_without_valid_sort_advises_help()
33 $this->artisan("bookstack:assign-sort-rule 100342 --all-books")
34 ->expectsOutput("Sort rule of provided id 100342 not found!")
38 public function test_confirmation_required()
40 $sortRule = SortRule::factory()->create();
42 $this->artisan("bookstack:assign-sort-rule {$sortRule->id} --all-books")
43 ->expectsConfirmation('Are you sure you want to continue?', 'no')
46 $booksWithSort = Book::query()->whereNotNull('sort_rule_id')->count();
47 $this->assertEquals(0, $booksWithSort);
50 public function test_assign_to_all_books()
52 $sortRule = SortRule::factory()->create();
53 $booksWithoutSort = Book::query()->whereNull('sort_rule_id')->count();
54 $this->assertGreaterThan(0, $booksWithoutSort);
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)")
62 $booksWithoutSort = Book::query()->whereNull('sort_rule_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 $sortRuleA = SortRule::factory()->create();
71 $sortRuleB = SortRule::factory()->create();
72 $book->sort_rule_id = $sortRuleA->id;
75 $booksWithoutSort = Book::query()->whereNull('sort_rule_id')->count();
76 $this->assertEquals($totalBooks, $booksWithoutSort + 1);
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)")
83 $booksWithoutSort = Book::query()->whereNull('sort_rule_id')->count();
84 $this->assertEquals(0, $booksWithoutSort);
85 $this->assertEquals($totalBooks, $sortRuleB->books()->count() + 1);
88 public function test_assign_to_all_books_with_sort()
90 $book = $this->entities->book();
91 $sortRuleA = SortRule::factory()->create();
92 $sortRuleB = SortRule::factory()->create();
93 $book->sort_rule_id = $sortRuleA->id;
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)")
102 $this->assertEquals($sortRuleB->id, $book->sort_rule_id);
103 $this->assertEquals(1, $sortRuleB->books()->count());
106 public function test_assign_to_all_books_with_sort_id_is_validated()
108 $this->artisan("bookstack:assign-sort-rule 50 --books-with-sort=beans")
109 ->expectsOutputToContain("Provided --books-with-sort option value is invalid")