]> BookStack Code Mirror - bookstack/blob - tests/Api/ShelvesApiTest.php
Fix Crowdin name in the language_request issue template
[bookstack] / tests / Api / ShelvesApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use Carbon\Carbon;
8 use Illuminate\Support\Facades\DB;
9 use Tests\TestCase;
10
11 class ShelvesApiTest extends TestCase
12 {
13     use TestsApi;
14
15     protected string $baseEndpoint = '/api/shelves';
16
17     public function test_index_endpoint_returns_expected_shelf()
18     {
19         $this->actingAsApiEditor();
20         $firstBookshelf = Bookshelf::query()->orderBy('id', 'asc')->first();
21
22         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
23         $resp->assertJson(['data' => [
24             [
25                 'id'   => $firstBookshelf->id,
26                 'name' => $firstBookshelf->name,
27                 'slug' => $firstBookshelf->slug,
28             ],
29         ]]);
30     }
31
32     public function test_create_endpoint()
33     {
34         $this->actingAsApiEditor();
35         $books = Book::query()->take(2)->get();
36
37         $details = [
38             'name'        => 'My API shelf',
39             'description' => 'A shelf created via the API',
40         ];
41
42         $resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
43         $resp->assertStatus(200);
44         $newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
45         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
46         $this->assertActivityExists('bookshelf_create', $newItem);
47         foreach ($books as $index => $book) {
48             $this->assertDatabaseHas('bookshelves_books', [
49                 'bookshelf_id' => $newItem->id,
50                 'book_id'      => $book->id,
51                 'order'        => $index,
52             ]);
53         }
54     }
55
56     public function test_shelf_name_needed_to_create()
57     {
58         $this->actingAsApiEditor();
59         $details = [
60             'description' => 'A shelf created via the API',
61         ];
62
63         $resp = $this->postJson($this->baseEndpoint, $details);
64         $resp->assertStatus(422);
65         $resp->assertJson([
66             'error' => [
67                 'message'    => 'The given data was invalid.',
68                 'validation' => [
69                     'name' => ['The name field is required.'],
70                 ],
71                 'code' => 422,
72             ],
73         ]);
74     }
75
76     public function test_read_endpoint()
77     {
78         $this->actingAsApiEditor();
79         $shelf = Bookshelf::visible()->first();
80
81         $resp = $this->getJson($this->baseEndpoint . "/{$shelf->id}");
82
83         $resp->assertStatus(200);
84         $resp->assertJson([
85             'id'         => $shelf->id,
86             'slug'       => $shelf->slug,
87             'created_by' => [
88                 'name' => $shelf->createdBy->name,
89             ],
90             'updated_by' => [
91                 'name' => $shelf->createdBy->name,
92             ],
93             'owned_by' => [
94                 'name' => $shelf->ownedBy->name,
95             ],
96         ]);
97     }
98
99     public function test_update_endpoint()
100     {
101         $this->actingAsApiEditor();
102         $shelf = Bookshelf::visible()->first();
103         $details = [
104             'name'        => 'My updated API shelf',
105             'description' => 'A shelf created via the API',
106         ];
107
108         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
109         $shelf->refresh();
110
111         $resp->assertStatus(200);
112         $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
113         $this->assertActivityExists('bookshelf_update', $shelf);
114     }
115
116     public function test_update_increments_updated_date_if_only_tags_are_sent()
117     {
118         $this->actingAsApiEditor();
119         $shelf = Bookshelf::visible()->first();
120         DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
121
122         $details = [
123             'tags' => [['name' => 'Category', 'value' => 'Testing']],
124         ];
125
126         $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
127         $shelf->refresh();
128         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
129     }
130
131     public function test_update_only_assigns_books_if_param_provided()
132     {
133         $this->actingAsApiEditor();
134         $shelf = Bookshelf::visible()->first();
135         $this->assertTrue($shelf->books()->count() > 0);
136         $details = [
137             'name' => 'My updated API shelf',
138         ];
139
140         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
141         $resp->assertStatus(200);
142         $this->assertTrue($shelf->books()->count() > 0);
143
144         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
145         $resp->assertStatus(200);
146         $this->assertTrue($shelf->books()->count() === 0);
147     }
148
149     public function test_delete_endpoint()
150     {
151         $this->actingAsApiEditor();
152         $shelf = Bookshelf::visible()->first();
153         $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
154
155         $resp->assertStatus(204);
156         $this->assertActivityExists('bookshelf_delete');
157     }
158 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.