11<?php
22
3- namespace Tests \ Entity ;
3+ namespace Sorting ;
44
5- use BookStack \Entities \Models \Book ;
65use BookStack \Entities \Models \Chapter ;
76use BookStack \Entities \Models \Page ;
87use BookStack \Entities \Repos \PageRepo ;
8+ use BookStack \Sorting \SortSet ;
99use Tests \TestCase ;
1010
11- class SortTest extends TestCase
11+ class BookSortTest extends TestCase
1212{
13+ public function test_book_sort_page_shows ()
14+ {
15+ $ bookToSort = $ this ->entities ->book ();
16+
17+ $ resp = $ this ->asAdmin ()->get ($ bookToSort ->getUrl ());
18+ $ this ->withHtml ($ resp )->assertElementExists ('a[href=" ' . $ bookToSort ->getUrl ('/sort ' ) . '"] ' );
19+
20+ $ resp = $ this ->get ($ bookToSort ->getUrl ('/sort ' ));
21+ $ resp ->assertStatus (200 );
22+ $ resp ->assertSee ($ bookToSort ->name );
23+ }
24+
1325 public function test_drafts_do_not_show_up ()
1426 {
1527 $ this ->asAdmin ();
@@ -20,232 +32,10 @@ public function test_drafts_do_not_show_up()
2032 $ resp = $ this ->get ($ book ->getUrl ());
2133 $ resp ->assertSee ($ draft ->name );
2234
23- $ resp = $ this ->get ($ book ->getUrl () . '/sort ' );
35+ $ resp = $ this ->get ($ book ->getUrl ('/sort ' ) );
2436 $ resp ->assertDontSee ($ draft ->name );
2537 }
2638
27- public function test_page_move_into_book ()
28- {
29- $ page = $ this ->entities ->page ();
30- $ currentBook = $ page ->book ;
31- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
32-
33- $ resp = $ this ->asEditor ()->get ($ page ->getUrl ('/move ' ));
34- $ resp ->assertSee ('Move Page ' );
35-
36- $ movePageResp = $ this ->put ($ page ->getUrl ('/move ' ), [
37- 'entity_selection ' => 'book: ' . $ newBook ->id ,
38- ]);
39- $ page ->refresh ();
40-
41- $ movePageResp ->assertRedirect ($ page ->getUrl ());
42- $ this ->assertTrue ($ page ->book ->id == $ newBook ->id , 'Page book is now the new book ' );
43-
44- $ newBookResp = $ this ->get ($ newBook ->getUrl ());
45- $ newBookResp ->assertSee ('moved page ' );
46- $ newBookResp ->assertSee ($ page ->name );
47- }
48-
49- public function test_page_move_into_chapter ()
50- {
51- $ page = $ this ->entities ->page ();
52- $ currentBook = $ page ->book ;
53- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
54- $ newChapter = $ newBook ->chapters ()->first ();
55-
56- $ movePageResp = $ this ->actingAs ($ this ->users ->editor ())->put ($ page ->getUrl ('/move ' ), [
57- 'entity_selection ' => 'chapter: ' . $ newChapter ->id ,
58- ]);
59- $ page ->refresh ();
60-
61- $ movePageResp ->assertRedirect ($ page ->getUrl ());
62- $ this ->assertTrue ($ page ->book ->id == $ newBook ->id , 'Page parent is now the new chapter ' );
63-
64- $ newChapterResp = $ this ->get ($ newChapter ->getUrl ());
65- $ newChapterResp ->assertSee ($ page ->name );
66- }
67-
68- public function test_page_move_from_chapter_to_book ()
69- {
70- $ oldChapter = Chapter::query ()->first ();
71- $ page = $ oldChapter ->pages ()->first ();
72- $ newBook = Book::query ()->where ('id ' , '!= ' , $ oldChapter ->book_id )->first ();
73-
74- $ movePageResp = $ this ->actingAs ($ this ->users ->editor ())->put ($ page ->getUrl ('/move ' ), [
75- 'entity_selection ' => 'book: ' . $ newBook ->id ,
76- ]);
77- $ page ->refresh ();
78-
79- $ movePageResp ->assertRedirect ($ page ->getUrl ());
80- $ this ->assertTrue ($ page ->book ->id == $ newBook ->id , 'Page parent is now the new book ' );
81- $ this ->assertTrue ($ page ->chapter === null , 'Page has no parent chapter ' );
82-
83- $ newBookResp = $ this ->get ($ newBook ->getUrl ());
84- $ newBookResp ->assertSee ($ page ->name );
85- }
86-
87- public function test_page_move_requires_create_permissions_on_parent ()
88- {
89- $ page = $ this ->entities ->page ();
90- $ currentBook = $ page ->book ;
91- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
92- $ editor = $ this ->users ->editor ();
93-
94- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'delete ' ], $ editor ->roles ->all ());
95-
96- $ movePageResp = $ this ->actingAs ($ editor )->put ($ page ->getUrl ('/move ' ), [
97- 'entity_selection ' => 'book: ' . $ newBook ->id ,
98- ]);
99- $ this ->assertPermissionError ($ movePageResp );
100-
101- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'delete ' , 'create ' ], $ editor ->roles ->all ());
102- $ movePageResp = $ this ->put ($ page ->getUrl ('/move ' ), [
103- 'entity_selection ' => 'book: ' . $ newBook ->id ,
104- ]);
105-
106- $ page ->refresh ();
107- $ movePageResp ->assertRedirect ($ page ->getUrl ());
108-
109- $ this ->assertTrue ($ page ->book ->id == $ newBook ->id , 'Page book is now the new book ' );
110- }
111-
112- public function test_page_move_requires_delete_permissions ()
113- {
114- $ page = $ this ->entities ->page ();
115- $ currentBook = $ page ->book ;
116- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
117- $ editor = $ this ->users ->editor ();
118-
119- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'create ' , 'delete ' ], $ editor ->roles ->all ());
120- $ this ->permissions ->setEntityPermissions ($ page , ['view ' , 'update ' , 'create ' ], $ editor ->roles ->all ());
121-
122- $ movePageResp = $ this ->actingAs ($ editor )->put ($ page ->getUrl ('/move ' ), [
123- 'entity_selection ' => 'book: ' . $ newBook ->id ,
124- ]);
125- $ this ->assertPermissionError ($ movePageResp );
126- $ pageView = $ this ->get ($ page ->getUrl ());
127- $ pageView ->assertDontSee ($ page ->getUrl ('/move ' ));
128-
129- $ this ->permissions ->setEntityPermissions ($ page , ['view ' , 'update ' , 'create ' , 'delete ' ], $ editor ->roles ->all ());
130- $ movePageResp = $ this ->put ($ page ->getUrl ('/move ' ), [
131- 'entity_selection ' => 'book: ' . $ newBook ->id ,
132- ]);
133-
134- $ page ->refresh ();
135- $ movePageResp ->assertRedirect ($ page ->getUrl ());
136- $ this ->assertTrue ($ page ->book ->id == $ newBook ->id , 'Page book is now the new book ' );
137- }
138-
139- public function test_chapter_move ()
140- {
141- $ chapter = $ this ->entities ->chapter ();
142- $ currentBook = $ chapter ->book ;
143- $ pageToCheck = $ chapter ->pages ->first ();
144- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
145-
146- $ chapterMoveResp = $ this ->asEditor ()->get ($ chapter ->getUrl ('/move ' ));
147- $ chapterMoveResp ->assertSee ('Move Chapter ' );
148-
149- $ moveChapterResp = $ this ->put ($ chapter ->getUrl ('/move ' ), [
150- 'entity_selection ' => 'book: ' . $ newBook ->id ,
151- ]);
152-
153- $ chapter = Chapter::query ()->find ($ chapter ->id );
154- $ moveChapterResp ->assertRedirect ($ chapter ->getUrl ());
155- $ this ->assertTrue ($ chapter ->book ->id === $ newBook ->id , 'Chapter Book is now the new book ' );
156-
157- $ newBookResp = $ this ->get ($ newBook ->getUrl ());
158- $ newBookResp ->assertSee ('moved chapter ' );
159- $ newBookResp ->assertSee ($ chapter ->name );
160-
161- $ pageToCheck = Page::query ()->find ($ pageToCheck ->id );
162- $ this ->assertTrue ($ pageToCheck ->book_id === $ newBook ->id , 'Chapter child page \'s book id has changed to the new book ' );
163- $ pageCheckResp = $ this ->get ($ pageToCheck ->getUrl ());
164- $ pageCheckResp ->assertSee ($ newBook ->name );
165- }
166-
167- public function test_chapter_move_requires_delete_permissions ()
168- {
169- $ chapter = $ this ->entities ->chapter ();
170- $ currentBook = $ chapter ->book ;
171- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
172- $ editor = $ this ->users ->editor ();
173-
174- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'create ' , 'delete ' ], $ editor ->roles ->all ());
175- $ this ->permissions ->setEntityPermissions ($ chapter , ['view ' , 'update ' , 'create ' ], $ editor ->roles ->all ());
176-
177- $ moveChapterResp = $ this ->actingAs ($ editor )->put ($ chapter ->getUrl ('/move ' ), [
178- 'entity_selection ' => 'book: ' . $ newBook ->id ,
179- ]);
180- $ this ->assertPermissionError ($ moveChapterResp );
181- $ pageView = $ this ->get ($ chapter ->getUrl ());
182- $ pageView ->assertDontSee ($ chapter ->getUrl ('/move ' ));
183-
184- $ this ->permissions ->setEntityPermissions ($ chapter , ['view ' , 'update ' , 'create ' , 'delete ' ], $ editor ->roles ->all ());
185- $ moveChapterResp = $ this ->put ($ chapter ->getUrl ('/move ' ), [
186- 'entity_selection ' => 'book: ' . $ newBook ->id ,
187- ]);
188-
189- $ chapter = Chapter::query ()->find ($ chapter ->id );
190- $ moveChapterResp ->assertRedirect ($ chapter ->getUrl ());
191- $ this ->assertTrue ($ chapter ->book ->id == $ newBook ->id , 'Page book is now the new book ' );
192- }
193-
194- public function test_chapter_move_requires_create_permissions_in_new_book ()
195- {
196- $ chapter = $ this ->entities ->chapter ();
197- $ currentBook = $ chapter ->book ;
198- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
199- $ editor = $ this ->users ->editor ();
200-
201- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'delete ' ], [$ editor ->roles ->first ()]);
202- $ this ->permissions ->setEntityPermissions ($ chapter , ['view ' , 'update ' , 'create ' , 'delete ' ], [$ editor ->roles ->first ()]);
203-
204- $ moveChapterResp = $ this ->actingAs ($ editor )->put ($ chapter ->getUrl ('/move ' ), [
205- 'entity_selection ' => 'book: ' . $ newBook ->id ,
206- ]);
207- $ this ->assertPermissionError ($ moveChapterResp );
208-
209- $ this ->permissions ->setEntityPermissions ($ newBook , ['view ' , 'update ' , 'create ' , 'delete ' ], [$ editor ->roles ->first ()]);
210- $ moveChapterResp = $ this ->put ($ chapter ->getUrl ('/move ' ), [
211- 'entity_selection ' => 'book: ' . $ newBook ->id ,
212- ]);
213-
214- $ chapter = Chapter::query ()->find ($ chapter ->id );
215- $ moveChapterResp ->assertRedirect ($ chapter ->getUrl ());
216- $ this ->assertTrue ($ chapter ->book ->id == $ newBook ->id , 'Page book is now the new book ' );
217- }
218-
219- public function test_chapter_move_changes_book_for_deleted_pages_within ()
220- {
221- /** @var Chapter $chapter */
222- $ chapter = Chapter::query ()->whereHas ('pages ' )->first ();
223- $ currentBook = $ chapter ->book ;
224- $ pageToCheck = $ chapter ->pages ->first ();
225- $ newBook = Book::query ()->where ('id ' , '!= ' , $ currentBook ->id )->first ();
226-
227- $ pageToCheck ->delete ();
228-
229- $ this ->asEditor ()->put ($ chapter ->getUrl ('/move ' ), [
230- 'entity_selection ' => 'book: ' . $ newBook ->id ,
231- ]);
232-
233- $ pageToCheck ->refresh ();
234- $ this ->assertEquals ($ newBook ->id , $ pageToCheck ->book_id );
235- }
236-
237- public function test_book_sort_page_shows ()
238- {
239- $ bookToSort = $ this ->entities ->book ();
240-
241- $ resp = $ this ->asAdmin ()->get ($ bookToSort ->getUrl ());
242- $ this ->withHtml ($ resp )->assertElementExists ('a[href=" ' . $ bookToSort ->getUrl ('/sort ' ) . '"] ' );
243-
244- $ resp = $ this ->get ($ bookToSort ->getUrl ('/sort ' ));
245- $ resp ->assertStatus (200 );
246- $ resp ->assertSee ($ bookToSort ->name );
247- }
248-
24939 public function test_book_sort ()
25040 {
25141 $ oldBook = $ this ->entities ->book ();
@@ -423,14 +213,61 @@ public function test_book_sort_item_returns_book_content()
423213 $ firstPage = $ bookToSort ->pages [0 ];
424214 $ firstChapter = $ bookToSort ->chapters [0 ];
425215
426- $ resp = $ this ->asAdmin ()->get ($ bookToSort ->getUrl () . '/sort-item ' );
216+ $ resp = $ this ->asAdmin ()->get ($ bookToSort ->getUrl ('/sort-item ' ) );
427217
428218 // Ensure book details are returned
429219 $ resp ->assertSee ($ bookToSort ->name );
430220 $ resp ->assertSee ($ firstPage ->name );
431221 $ resp ->assertSee ($ firstChapter ->name );
432222 }
433223
224+ public function test_book_sort_item_shows_auto_sort_status ()
225+ {
226+ $ sort = SortSet::factory ()->create (['name ' => 'My sort ' ]);
227+ $ book = $ this ->entities ->book ();
228+
229+ $ resp = $ this ->asAdmin ()->get ($ book ->getUrl ('/sort-item ' ));
230+ $ this ->withHtml ($ resp )->assertElementNotExists ("span[title='Auto Sort Active: My sort'] " );
231+
232+ $ book ->sort_set_id = $ sort ->id ;
233+ $ book ->save ();
234+
235+ $ resp = $ this ->asAdmin ()->get ($ book ->getUrl ('/sort-item ' ));
236+ $ this ->withHtml ($ resp )->assertElementExists ("span[title='Auto Sort Active: My sort'] " );
237+ }
238+
239+ public function test_auto_sort_options_shown_on_sort_page ()
240+ {
241+ $ sort = SortSet::factory ()->create ();
242+ $ book = $ this ->entities ->book ();
243+ $ resp = $ this ->asAdmin ()->get ($ book ->getUrl ('/sort ' ));
244+
245+ $ this ->withHtml ($ resp )->assertElementExists ('select[name="auto-sort"] option[value=" ' . $ sort ->id . '"] ' );
246+ }
247+
248+ public function test_auto_sort_option_submit_saves_to_book ()
249+ {
250+ $ sort = SortSet::factory ()->create ();
251+ $ book = $ this ->entities ->book ();
252+ $ bookPage = $ book ->pages ()->first ();
253+ $ bookPage ->priority = 10000 ;
254+ $ bookPage ->save ();
255+
256+ $ resp = $ this ->asAdmin ()->put ($ book ->getUrl ('/sort ' ), [
257+ 'auto-sort ' => $ sort ->id ,
258+ ]);
259+
260+ $ resp ->assertRedirect ($ book ->getUrl ());
261+ $ book ->refresh ();
262+ $ bookPage ->refresh ();
263+
264+ $ this ->assertEquals ($ sort ->id , $ book ->sort_set_id );
265+ $ this ->assertNotEquals (10000 , $ bookPage ->priority );
266+
267+ $ resp = $ this ->get ($ book ->getUrl ('/sort ' ));
268+ $ this ->withHtml ($ resp )->assertElementExists ('select[name="auto-sort"] option[value=" ' . $ sort ->id . '"][selected] ' );
269+ }
270+
434271 public function test_pages_in_book_show_sorted_by_priority ()
435272 {
436273 $ book = $ this ->entities ->bookHasChaptersAndPages ();
0 commit comments