]> BookStack Code Mirror - bookstack/blob - tests/Api/ShelvesApiTest.php
Added testing to favourite system
[bookstack] / tests / Api / ShelvesApiTest.php
1 <?php namespace Tests\Api;
2
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\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             'owned_by' => [
90                 'name' => $shelf->ownedBy->name
91             ],
92         ]);
93     }
94
95     public function test_update_endpoint()
96     {
97         $this->actingAsApiEditor();
98         $shelf = Bookshelf::visible()->first();
99         $details = [
100             'name' => 'My updated API shelf',
101             'description' => 'A shelf created via the API',
102         ];
103
104         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
105         $shelf->refresh();
106
107         $resp->assertStatus(200);
108         $resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
109         $this->assertActivityExists('bookshelf_update', $shelf);
110     }
111
112     public function test_update_only_assigns_books_if_param_provided()
113     {
114         $this->actingAsApiEditor();
115         $shelf = Bookshelf::visible()->first();
116         $this->assertTrue($shelf->books()->count() > 0);
117         $details = [
118             'name' => 'My updated API shelf',
119         ];
120
121         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
122         $resp->assertStatus(200);
123         $this->assertTrue($shelf->books()->count() > 0);
124
125         $resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", ['books' => []]);
126         $resp->assertStatus(200);
127         $this->assertTrue($shelf->books()->count() === 0);
128     }
129
130     public function test_delete_endpoint()
131     {
132         $this->actingAsApiEditor();
133         $shelf = Bookshelf::visible()->first();
134         $resp = $this->deleteJson($this->baseEndpoint . "/{$shelf->id}");
135
136         $resp->assertStatus(204);
137         $this->assertActivityExists('bookshelf_delete');
138     }
139 }