]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Fixed bad test class name
[bookstack] / tests / Api / BooksApiTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4
5 class BooksApiTest extends TestCase
6 {
7     use TestsApi;
8
9     protected $baseEndpoint = '/api/books';
10
11     public function test_index_endpoint_returns_expected_book()
12     {
13         $this->actingAsApiEditor();
14         $firstBook = Book::query()->orderBy('id', 'asc')->first();
15
16         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
17         $resp->assertJson(['data' => [
18             [
19                 'id' => $firstBook->id,
20                 'name' => $firstBook->name,
21                 'slug' => $firstBook->slug,
22             ]
23         ]]);
24     }
25
26     public function test_create_endpoint()
27     {
28         $this->actingAsApiEditor();
29         $details = [
30             'name' => 'My API book',
31             'description' => 'A book created via the API',
32         ];
33
34         $resp = $this->postJson($this->baseEndpoint, $details);
35         $resp->assertStatus(200);
36         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
37         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
38         $this->assertActivityExists('book_create', $newItem);
39     }
40
41     public function test_read_endpoint()
42     {
43         $this->actingAsApiEditor();
44         $book = Book::visible()->first();
45
46         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
47
48         $resp->assertStatus(200);
49         $resp->assertJson([
50             'id' => $book->id,
51             'slug' => $book->slug,
52             'created_by' => [
53                 'name' => $book->createdBy->name,
54             ],
55             'updated_by' => [
56                 'name' => $book->createdBy->name,
57             ]
58         ]);
59     }
60
61     public function test_update_endpoint()
62     {
63         $this->actingAsApiEditor();
64         $book = Book::visible()->first();
65         $details = [
66             'name' => 'My updated API book',
67             'description' => 'A book created via the API',
68         ];
69
70         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
71         $book->refresh();
72
73         $resp->assertStatus(200);
74         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
75         $this->assertActivityExists('book_update', $book);
76     }
77
78     public function test_delete_endpoint()
79     {
80         $this->actingAsApiEditor();
81         $book = Book::visible()->first();
82         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
83
84         $resp->assertStatus(204);
85         $this->assertActivityExists('book_delete');
86     }
87 }