]> BookStack Code Mirror - bookstack/blob - tests/Api/ShelvesApiTest.php
Replace node-sass with dart-sass
[bookstack] / tests / Api / ShelvesApiTest.php
1 <?php namespace Tests\Api;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use Tests\TestCase;
6
7 class ShelvesApiTest extends TestCase
8 {
9     use TestsApi;
10
11     protected $baseEndpoint = '/api/shelves';
12
13     public function test_index_endpoint_returns_expected_shelf()
14     {
15         $this->actingAsApiEditor();
16         $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
17
18         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
19         $resp->assertJson(['data' => [
20             [
21                 'id' => $firstBookshelf->id,
22                 'name' => $firstBookshelf->name,
23                 'slug' => $firstBookshelf->slug,
24             ]
25         ]]);
26     }
27
28     public function test_create_endpoint()
29     {
30         $this->actingAsApiEditor();
31         $books = Book::query()->take(2)->get();
32
33         $details = [
34             'name' => 'My API shelf',
35             'description' => 'A shelf created via the API',
36         ];
37
38         $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
39         $resp->assertStatus(200);
40         $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
41         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
42         $this->assertActivityExists('bookshelf_create', $newItem);
43         foreach ($books as $index => $book) {
44             $this->assertDatabaseHas('bookshelves_books', [
45                 'bookshelf_id' => $newItem->id,
46                 'book_id' => $book->id,
47                 'order' => $index,
48             ]);
49         }
50     }
51
52     public function test_shelf_name_needed_to_create()
53     {
54         $this->actingAsApiEditor();
55         $details = [
56             'description' => 'A shelf created via the API',
57         ];
58
59         $resp = $this->postJson($this->baseEndpoint, $details);
60         $resp->assertStatus(422);
61         $resp->assertJson([
62             "error" => [
63                 "message" => "The given data was invalid.",
64                 "validation" => [
65                     "name" => ["The name field is required."]
66                 ],
67                 "code" => 422,
68             ],
69         ]);
70     }
71
72     public function test_read_endpoint()
73     {
74         $this->actingAsApiEditor();
75         $shelf = Bookshelf::visible()->first();
76
77         $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
78
79         $resp->assertStatus(200);
80         $resp->assertJson([
81             'id' => $shelf->id,
82             'slug' => $shelf->slug,
83             'created_by' => [
84                 'name' => $shelf->createdBy->name,
85             ],
86             'updated_by' => [
87                 'name' => $shelf->createdBy->name,
88             ]
89         ]);
90     }
91
92     public function test_update_endpoint()
93     {
94         $this->actingAsApiEditor();
95         $shelf = Bookshelf::visible()->first();
96         $details = [
97             'name' => 'My updated API shelf',
98             'description' => 'A shelf created via the API',
99         ];
100
101         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
102         $shelf->refresh();
103
104         $resp->assertStatus(200);
105         $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
106         $this->assertActivityExists('bookshelf_update', $shelf);
107     }
108
109     public function test_update_only_assigns_books_if_param_provided()
110     {
111         $this->actingAsApiEditor();
112         $shelf = Bookshelf::visible()->first();
113         $this->assertTrue($shelf->books()->count() > 0);
114         $details = [
115             'name' => 'My updated API shelf',
116         ];
117
118         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
119         $resp->assertStatus(200);
120         $this->assertTrue($shelf->books()->count() > 0);
121
122         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
123         $resp->assertStatus(200);
124         $this->assertTrue($shelf->books()->count() === 0);
125     }
126
127     public function test_delete_endpoint()
128     {
129         $this->actingAsApiEditor();
130         $shelf = Bookshelf::visible()->first();
131         $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
132
133         $resp->assertStatus(204);
134         $this->assertActivityExists('bookshelf_delete');
135     }
136 }