1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.test.Matchers.isPostWithId
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
12 import java.util.Arrays.*
15 * Unit test for [NewPage].
17 class NewPageTest : WebPageTest() {
19 private val newElements = mock<NewElements>()
20 override val page: SoneTemplatePage
21 get() = NewPage(webInterface, loaders, templateRenderer, newElements)
24 fun setupNumberOfPostsPerPage() {
25 webInterface.core.preferences.newPostsPerPage = 5
29 fun `page returns correct path`() {
30 assertThat(page.path, equalTo("new.html"))
34 fun `page does not require login`() {
35 assertThat(page.requiresLogin(), equalTo(false))
39 fun `page returns correct title`() {
40 addTranslation("Page.New.Title", "new page title")
41 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
45 fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
47 val extraPost = mock<Post>().withTime(2000)
48 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
49 val postReplies = asList(mock<PostReply>(), mock())
50 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
51 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
52 whenever(newElements.newPosts()).thenReturn(posts)
53 whenever(newElements.newReplies()).thenReturn(postReplies)
56 val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
57 assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
61 private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
64 @Suppress("UNCHECKED_CAST")
65 fun `posts are paginated properly`() {
67 webInterface.core.preferences.newPostsPerPage = 2
68 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
69 whenever(newElements.newPosts()).thenReturn(posts)
71 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
76 @Suppress("UNCHECKED_CAST")
77 fun `posts are paginated properly on second page`() {
79 webInterface.core.preferences.newPostsPerPage = 2
80 addHttpRequestParameter("page", "1")
81 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
82 whenever(newElements.newPosts()).thenReturn(posts)
84 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
89 fun `posts are unfiltered if no current Sone is set`() {
91 val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
92 whenever(newElements.newPosts()).thenReturn(posts)
94 assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
99 fun `posts are filtered if a current Sone is set`() {
100 val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
101 whenever(newElements.newPosts(currentSone)).thenReturn(posts.last().asList())
103 assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
108 fun `replies are unfiltered if no current Sone is set`() {
110 val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
111 whenever(newElements.newReplies()).thenReturn(replies)
113 assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
118 fun `replies are filtered if a current Sone is set`() {
119 val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
120 whenever(newElements.newReplies(currentSone)).thenReturn(replies.last().asList())
122 assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
127 fun `page can be created by dependency injection`() {
128 assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
132 fun `page is annotated with the correct menuname`() {
133 assertThat(page.menuName, equalTo("New"))
137 fun `page is annotated with correct template path`() {
138 assertThat(page.templatePath, equalTo("/templates/new.html"))