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