]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Updated 'Spanish Argentina' translation.
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5
6 class BookShelfTest extends TestCase
7 {
8
9     public function test_shelves_shows_in_header_if_have_view_permissions()
10     {
11         $viewer = $this->getViewer();
12         $resp = $this->actingAs($viewer)->get('/');
13         $resp->assertElementContains('header', 'Shelves');
14
15         $viewer->roles()->delete();
16         $this->giveUserPermissions($viewer);
17         $resp = $this->actingAs($viewer)->get('/');
18         $resp->assertElementNotContains('header', 'Shelves');
19
20         $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
21         $resp = $this->actingAs($viewer)->get('/');
22         $resp->assertElementContains('header', 'Shelves');
23
24         $viewer->roles()->delete();
25         $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
26         $resp = $this->actingAs($viewer)->get('/');
27         $resp->assertElementContains('header', 'Shelves');
28     }
29
30     public function test_shelves_page_contains_create_link()
31     {
32         $resp = $this->asEditor()->get('/shelves');
33         $resp->assertElementContains('a', 'Create New Shelf');
34     }
35
36     public function test_shelves_create()
37     {
38         $booksToInclude = Book::take(2)->get();
39         $shelfInfo = [
40             'name' => 'My test book' . str_random(4),
41             'description' => 'Test book description ' . str_random(10)
42         ];
43         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
44             'books' => $booksToInclude->implode('id', ','),
45             'tags' => [
46                 [
47                     'name' => 'Test Category',
48                     'value' => 'Test Tag Value',
49                 ]
50             ],
51         ]));
52         $resp->assertRedirect();
53         $editorId = $this->getEditor()->id;
54         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
55
56         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
57         $shelfPage = $this->get($shelf->getUrl());
58         $shelfPage->assertSee($shelfInfo['name']);
59         $shelfPage->assertSee($shelfInfo['description']);
60         $shelfPage->assertElementContains('.tag-item', 'Test Category');
61         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
62
63         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
64         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
65     }
66
67     public function test_shelf_view()
68     {
69         $shelf = Bookshelf::first();
70         $resp = $this->asEditor()->get($shelf->getUrl());
71         $resp->assertStatus(200);
72         $resp->assertSeeText($shelf->name);
73         $resp->assertSeeText($shelf->description);
74
75         foreach ($shelf->books as $book) {
76             $resp->assertSee($book->name);
77         }
78     }
79
80     public function test_shelf_view_shows_action_buttons()
81     {
82         $shelf = Bookshelf::first();
83         $resp = $this->asAdmin()->get($shelf->getUrl());
84         $resp->assertSee($shelf->getUrl('/edit'));
85         $resp->assertSee($shelf->getUrl('/permissions'));
86         $resp->assertSee($shelf->getUrl('/delete'));
87         $resp->assertElementContains('a', 'Edit');
88         $resp->assertElementContains('a', 'Permissions');
89         $resp->assertElementContains('a', 'Delete');
90
91         $resp = $this->asEditor()->get($shelf->getUrl());
92         $resp->assertDontSee($shelf->getUrl('/permissions'));
93     }
94
95     public function test_shelf_edit()
96     {
97         $shelf = Bookshelf::first();
98         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
99         $resp->assertSeeText('Edit Bookshelf');
100
101         $booksToInclude = Book::take(2)->get();
102         $shelfInfo = [
103             'name' => 'My test book' . str_random(4),
104             'description' => 'Test book description ' . str_random(10)
105         ];
106
107         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
108             'books' => $booksToInclude->implode('id', ','),
109             'tags' => [
110                 [
111                     'name' => 'Test Category',
112                     'value' => 'Test Tag Value',
113                 ]
114             ],
115         ]));
116         $shelf = Bookshelf::find($shelf->id);
117         $resp->assertRedirect($shelf->getUrl());
118         $this->assertSessionHas('success');
119
120         $editorId = $this->getEditor()->id;
121         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
122
123         $shelfPage = $this->get($shelf->getUrl());
124         $shelfPage->assertSee($shelfInfo['name']);
125         $shelfPage->assertSee($shelfInfo['description']);
126         $shelfPage->assertElementContains('.tag-item', 'Test Category');
127         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
128
129         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
130         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
131     }
132
133     public function test_shelf_delete()
134     {
135         $shelf = Bookshelf::first();
136         $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
137         $resp->assertSeeText('Delete Bookshelf');
138         $resp->assertSee("action=\"{$shelf->getUrl()}\"");
139
140         $resp = $this->delete($shelf->getUrl());
141         $resp->assertRedirect('/shelves');
142         $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
143         $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
144         $this->assertSessionHas('success');
145     }
146
147     public function test_shelf_copy_permissions()
148     {
149         $shelf = Bookshelf::first();
150         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
151         $resp->assertSeeText('Copy Permissions');
152         $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
153
154         $child = $shelf->books()->first();
155         $editorRole = $this->getEditor()->roles()->first();
156         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
157         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
158
159         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
160         $resp = $this->post($shelf->getUrl('/copy-permissions'));
161         $child = $shelf->books()->first();
162
163         $resp->assertRedirect($shelf->getUrl());
164         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
165         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
166         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
167         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
168     }
169
170 }