Fix pagination in gallery
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 22 Jul 2017 15:55:09 +0000 (17:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 22 Jul 2017 15:55:20 +0000 (17:55 +0200)
src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt

index 2f165fc..5e8701b 100644 (file)
@@ -33,8 +33,10 @@ class ImageBrowserPage(template: Template, webInterface: WebInterface):
                                        .filterNot(Album::isEmpty)
                                        .sortedBy(Album::getTitle)
                                        .also { albums ->
-                                               templateContext["albums"] = albums
-                                               templateContext["albumPagination"] = Pagination(albums, 12).apply { page = request.parameters["page"]?.toIntOrNull() ?: 0 }
+                                               Pagination(albums, webInterface.core.preferences.imagesPerPage).apply { page = request.parameters["page"]?.toIntOrNull() ?: 0 }.also { pagination ->
+                                                       templateContext["albumPagination"] = pagination
+                                                       templateContext["albums"] = pagination.items
+                                               }
                                        }
                } else {
                        templateContext["soneRequested"] = true
index 2c581ac..4d0b2c7 100644 (file)
@@ -85,6 +85,25 @@ class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
                }
        }
 
+       @Test
+       fun `get request for gallery can show second page`() {
+               core.preferences.imagesPerPage = 2
+               val firstSone = createSone("first album", "second album")
+               addSone("sone1", firstSone)
+               val secondSone = createSone("third album", "fourth album")
+               addSone("sone2", secondSone)
+               addHttpRequestParameter("mode", "gallery")
+               addHttpRequestParameter("page", "1")
+               verifyNoRedirect {
+                       assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
+                       @Suppress("UNCHECKED_CAST")
+                       assertThat(templateContext["albums"] as Iterable<Album>, contains(
+                                       firstSone.rootAlbum.albums[1],
+                                       secondSone.rootAlbum.albums[0]
+                       ))
+               }
+       }
+
        private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
                return mock<Sone>().apply {
                        val rootAlbum = mock<Album>()