EntityTest.php
8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
use Illuminate\Support\Facades\DB;
class EntityTest extends TestCase
{
public function test_entity_creation()
{
// Test Creation
$book = $this->bookCreation();
$chapter = $this->chapterCreation($book);
$page = $this->pageCreation($chapter);
// Test Updating
$book = $this->bookUpdate($book);
// Test Deletion
$this->bookDelete($book);
}
public function bookDelete(\BookStack\Book $book)
{
$this->asAdmin()
->visit($book->getUrl())
// Check link works correctly
->click('Delete')
->seePageIs($book->getUrl() . '/delete')
// Ensure the book name is show to user
->see($book->name)
->press('Confirm')
->seePageIs('/books')
->notSeeInDatabase('books', ['id' => $book->id]);
}
public function bookUpdate(\BookStack\Book $book)
{
$newName = $book->name . ' Updated';
$this->asAdmin()
// Go to edit screen
->visit($book->getUrl() . '/edit')
->see($book->name)
// Submit new name
->type($newName, '#name')
->press('Save Book')
// Check page url and text
->seePageIs($book->getUrl() . '-updated')
->see($newName);
return \BookStack\Book::find($book->id);
}
public function test_book_sort_page_shows()
{
$books = \BookStack\Book::all();
$bookToSort = $books[0];
$this->asAdmin()
->visit($bookToSort->getUrl())
->click('Sort')
->seePageIs($bookToSort->getUrl() . '/sort')
->seeStatusCode(200)
->see($bookToSort->name)
// Ensure page shows other books
->see($books[1]->name);
}
public function test_book_sort_item_returns_book_content()
{
$books = \BookStack\Book::all();
$bookToSort = $books[0];
$firstPage = $bookToSort->pages[0];
$firstChapter = $bookToSort->chapters[0];
$this->asAdmin()
->visit($bookToSort->getUrl() . '/sort-item')
// Ensure book details are returned
->see($bookToSort->name)
->see($firstPage->name)
->see($firstChapter->name);
}
public function pageCreation($chapter)
{
$page = factory(\BookStack\Page::class)->make([
'name' => 'My First Page'
]);
$this->asAdmin()
// Navigate to page create form
->visit($chapter->getUrl())
->click('New Page');
$draftPage = \BookStack\Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
$this->seePageIs($draftPage->getUrl())
// Fill out form
->type($page->name, '#name')
->type($page->html, '#html')
->press('Save Page')
// Check redirect and page
->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
->see($page->name);
$page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
return $page;
}
public function chapterCreation(\BookStack\Book $book)
{
$chapter = factory(\BookStack\Chapter::class)->make([
'name' => 'My First Chapter'
]);
$this->asAdmin()
// Navigate to chapter create page
->visit($book->getUrl())
->click('New Chapter')
->seePageIs($book->getUrl() . '/chapter/create')
// Fill out form
->type($chapter->name, '#name')
->type($chapter->description, '#description')
->press('Save Chapter')
// Check redirect and landing page
->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
->see($chapter->name)->see($chapter->description);
$chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
return $chapter;
}
public function bookCreation()
{
$book = factory(\BookStack\Book::class)->make([
'name' => 'My First Book'
]);
$this->asAdmin()
->visit('/books')
// Choose to create a book
->click('Add new book')
->seePageIs('/books/create')
// Fill out form & save
->type($book->name, '#name')
->type($book->description, '#description')
->press('Save Book')
// Check it redirects correctly
->seePageIs('/books/my-first-book')
->see($book->name)->see($book->description);
// Ensure duplicate names are given different slugs
$this->asAdmin()
->visit('/books/create')
->type($book->name, '#name')
->type($book->description, '#description')
->press('Save Book');
$expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
$this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
$book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
return $book;
}
public function test_entities_viewable_after_creator_deletion()
{
// Create required assets and revisions
$creator = $this->getEditor();
$updater = $this->getEditor();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
$this->actingAs($creator);
app('BookStack\Repos\UserRepo')->destroy($creator);
app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
$this->checkEntitiesViewable($entities);
}
public function test_entities_viewable_after_updater_deletion()
{
// Create required assets and revisions
$creator = $this->getEditor();
$updater = $this->getEditor();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
$this->actingAs($updater);
app('BookStack\Repos\UserRepo')->destroy($updater);
app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
$this->checkEntitiesViewable($entities);
}
private function checkEntitiesViewable($entities)
{
// Check pages and books are visible.
$this->asAdmin();
$this->visit($entities['book']->getUrl())->seeStatusCode(200)
->visit($entities['chapter']->getUrl())->seeStatusCode(200)
->visit($entities['page']->getUrl())->seeStatusCode(200);
// Check revision listing shows no errors.
$this->visit($entities['page']->getUrl())
->click('Revisions')->seeStatusCode(200);
}
public function test_recently_created_pages_view()
{
$user = $this->getEditor();
$content = $this->createEntityChainBelongingToUser($user);
$this->asAdmin()->visit('/pages/recently-created')
->seeInNthElement('.entity-list .page', 0, $content['page']->name);
}
public function test_recently_updated_pages_view()
{
$user = $this->getEditor();
$content = $this->createEntityChainBelongingToUser($user);
$this->asAdmin()->visit('/pages/recently-updated')
->seeInNthElement('.entity-list .page', 0, $content['page']->name);
}
public function test_old_page_slugs_redirect_to_new_pages()
{
$page = \BookStack\Page::first();
$pageUrl = $page->getUrl();
$newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
// Need to save twice since revisions are not generated in seeder.
$this->asAdmin()->visit($pageUrl)
->clickInElement('#content', 'Edit')
->type('super test', '#name')
->press('Save Page');
$page = \BookStack\Page::first();
$pageUrl = $page->getUrl();
// Second Save
$this->visit($pageUrl)
->clickInElement('#content', 'Edit')
->type('super test page', '#name')
->press('Save Page')
// Check redirect
->seePageIs($newPageUrl)
->visit($pageUrl)
->seePageIs($newPageUrl);
}
public function test_recently_updated_pages_on_home()
{
$page = \BookStack\Page::orderBy('updated_at', 'asc')->first();
$this->asAdmin()->visit('/')
->dontSeeInElement('#recently-updated-pages', $page->name);
$this->visit($page->getUrl() . '/edit')
->press('Save Page')
->visit('/')
->seeInElement('#recently-updated-pages', $page->name);
}
public function test_recently_created_pages_on_home()
{
$entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
$this->asAdmin()->visit('/')
->seeInElement('#recently-created-pages', $entityChain['page']->name);
}
}