PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/test/java/com/adaptavist/confluence/naturalchildren/NaturalChildrenActionTestCase.java

https://bitbucket.org/atlassian/confluence-pagetree-plugin
Java | 329 lines | 267 code | 54 blank | 8 comment | 2 complexity | 75bc4a2579bc63803a7cb32fede5ce3c MD5 | raw file
  1. package com.adaptavist.confluence.naturalchildren;
  2. import com.atlassian.confluence.content.render.xhtml.ConversionContext;
  3. import com.atlassian.confluence.content.render.xhtml.XhtmlException;
  4. import com.atlassian.confluence.internal.ContentPermissionManagerInternal;
  5. import com.atlassian.confluence.pages.Page;
  6. import com.atlassian.confluence.pages.PageManager;
  7. import com.atlassian.confluence.security.Permission;
  8. import com.atlassian.confluence.security.PermissionCheckExemptions;
  9. import com.atlassian.confluence.security.PermissionManager;
  10. import com.atlassian.confluence.spaces.Space;
  11. import com.atlassian.confluence.spaces.SpaceManager;
  12. import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
  13. import com.atlassian.confluence.util.ExcerptHelper;
  14. import com.atlassian.confluence.xhtml.api.XhtmlContent;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.junit.Before;
  17. import org.junit.Rule;
  18. import org.junit.Test;
  19. import org.mockito.Mock;
  20. import org.mockito.junit.MockitoJUnit;
  21. import org.mockito.junit.MockitoRule;
  22. import org.mockito.stubbing.Answer;
  23. import javax.xml.stream.XMLStreamException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import static java.util.Collections.singletonList;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertFalse;
  29. import static org.junit.Assert.assertTrue;
  30. import static org.mockito.Matchers.any;
  31. import static org.mockito.Matchers.anyString;
  32. import static org.mockito.Matchers.eq;
  33. import static org.mockito.Mockito.never;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.when;
  36. public class NaturalChildrenActionTestCase {
  37. @Rule
  38. public MockitoRule mockitoRule = MockitoJUnit.rule();
  39. @Mock
  40. private XhtmlContent xhtmlContent;
  41. @Mock
  42. private PageManager pageManager;
  43. @Mock
  44. private SpaceManager spaceManager;
  45. @Mock
  46. private PermissionManager permissionManager;
  47. @Mock
  48. private ContentPermissionManagerInternal contentPermissionManager;
  49. @Mock
  50. private PermissionCheckExemptions permissionCheckExemptions;
  51. @Mock
  52. private ExcerptHelper excerptHelper;
  53. private String spaceKey = "tst";
  54. private Space space;
  55. private Page page;
  56. private NaturalChildrenAction naturalChildrenAction;
  57. @Before
  58. public void setUp() {
  59. space = new Space(spaceKey);
  60. page = new Page();
  61. naturalChildrenAction = new NaturalChildrenAction();
  62. setNaturalChildrenActionDependencies(naturalChildrenAction);
  63. }
  64. @Test
  65. public void testPageExcerptHtml() throws XhtmlException, XMLStreamException {
  66. String excerpt = "true";
  67. String excerptAfterConvert = "This is after convert excerpt";
  68. when(excerptHelper.getExcerpt(page)).thenReturn(excerpt);
  69. when(xhtmlContent.convertStorageToView(any(String.class), any(ConversionContext.class))).thenReturn(excerptAfterConvert);
  70. when(pageManager.getPage(eq(page.getId()))).thenReturn(page);
  71. assertEquals(excerptAfterConvert, naturalChildrenAction.getPageExcerptHtml(page));
  72. }
  73. @Test
  74. public void testPageExcerptHtmlWithException() throws XhtmlException, XMLStreamException {
  75. String excerpt = "<b>test</b>";
  76. String excerptWithException = "<span class=\"error\">&lt;b&gt;test&lt;/b&gt;</span>";
  77. when(excerptHelper.getExcerpt(page)).thenReturn(excerpt);
  78. when(xhtmlContent.convertStorageToView(any(String.class), any(ConversionContext.class))).thenThrow(new XMLStreamException("xmlStreamException"));
  79. when(pageManager.getPage(eq(page.getId()))).thenReturn(page);
  80. assertEquals(excerptWithException, naturalChildrenAction.getPageExcerptHtml(page));
  81. }
  82. @Test
  83. public void testPageExcerptHtmlWithNullExcerpt() {
  84. assertEquals("", naturalChildrenAction.getPageExcerptHtml(page));
  85. }
  86. @Test
  87. public void testGetAllStartPagesWithoutSort() {
  88. List<Page> expectedStartPages = new ArrayList<Page>();
  89. Space testSpace = new Space();
  90. Page homePage = new Page();
  91. homePage.setTitle("Home Page");
  92. testSpace.setHomePage(homePage);
  93. Page pageOne = new Page();
  94. pageOne.setTitle("One");
  95. Page pageTwo = new Page();
  96. pageTwo.setTitle("Two");
  97. expectedStartPages.add(pageOne);
  98. expectedStartPages.add(pageTwo);
  99. expectedStartPages.add(homePage);
  100. when(spaceManager.getSpace(spaceKey)).thenReturn(testSpace);
  101. when(pageManager.getTopLevelPages(testSpace)).thenReturn(expectedStartPages);
  102. when(permissionManager.getPermittedEntities(eq(AuthenticatedUserThreadLocal.get()), eq(Permission.VIEW), eq(expectedStartPages)))
  103. .thenReturn(expectedStartPages);
  104. when(contentPermissionManager.getPermittedPagesIgnoreInheritedPermissions(
  105. any(), any(), any())).thenReturn(expectedStartPages);
  106. naturalChildrenAction.setSort("");
  107. naturalChildrenAction.setReverse(false);
  108. assertEquals(expectedStartPages, naturalChildrenAction.getAllPermittedStartPages(spaceKey));
  109. }
  110. /**
  111. * Checks that the list of pages is limited and "Show all" button is rendered
  112. * when it is rendered on the sidebar
  113. */
  114. @Test
  115. public void testGetPermittedChildrenWithShowMoreButtonOnSidebar() {
  116. naturalChildrenAction.setPlacement("sidebar");
  117. when(contentPermissionManager.getPermittedPagesIgnoreInheritedPermissions(any(), any(), any())).
  118. thenAnswer((Answer<List<Page>>) invocation -> {
  119. Object[] args = invocation.getArguments();
  120. return (List<Page>) args[0];
  121. });
  122. Page parentPage = new Page();
  123. parentPage.setTitle("Page Level 1");
  124. for (int i = 0; i < NaturalChildrenAction.BATCH_SIZE + 5; i++) {
  125. Page childPage = new Page();
  126. childPage.setTitle("Child page " + i);
  127. childPage.setParentPage(parentPage);
  128. parentPage.addChild(childPage);
  129. }
  130. PageList pageList = naturalChildrenAction.getLimitedPermittedChildren(parentPage);
  131. assertEquals(NaturalChildrenAction.BATCH_SIZE + 1, pageList.getPageList().size());
  132. assertFalse(pageList.isHasMoreBefore());
  133. assertTrue(pageList.isHasMoreAfter());
  134. }
  135. /**
  136. * Checks that the whole list of pages is displayed and "Show all" buttons are not rendered
  137. * when it is rendered in the page tree macro (placement is not "sidebar)
  138. */
  139. @Test
  140. public void testGetPermittedChildrenWithoutShowMoreButtonInPageTreeMacro() {
  141. naturalChildrenAction.setPlacement(null);
  142. when(contentPermissionManager.getPermittedPagesIgnoreInheritedPermissions(any(), any(), any())).
  143. thenAnswer((Answer<List<Page>>) invocation -> {
  144. Object[] args = invocation.getArguments();
  145. return (List<Page>) args[0];
  146. });
  147. Page parentPage = new Page();
  148. parentPage.setTitle("Page Level 1");
  149. final int NUMBER_OF_PAGES = NaturalChildrenAction.BATCH_SIZE + 5;
  150. for (int i = 0; i < NUMBER_OF_PAGES; i++) {
  151. Page childPage = new Page();
  152. childPage.setTitle("Child page " + i);
  153. childPage.setParentPage(parentPage);
  154. parentPage.addChild(childPage);
  155. }
  156. PageList pageList = naturalChildrenAction.getLimitedPermittedChildren(parentPage);
  157. assertEquals(NUMBER_OF_PAGES, pageList.getPageList().size());
  158. assertFalse(pageList.isHasMoreBefore());
  159. assertFalse(pageList.isHasMoreAfter());
  160. }
  161. @Test
  162. public void testGetPermittedChildrenPageByReverseSort() {
  163. final List<Page> permittedChildren = new ArrayList<Page>();
  164. List<Object> expectedPermittedPage = new ArrayList<Object>();
  165. Page parentPage = new Page();
  166. parentPage.setTitle("Page Level 1");
  167. Page childPage = new Page();
  168. childPage.setTitle("Childpage");
  169. childPage.setParentPage(parentPage);
  170. Page childPageTwo = new Page();
  171. childPageTwo.setTitle("Childpage Two");
  172. childPageTwo.setParentPage(parentPage);
  173. permittedChildren.add(childPage);
  174. permittedChildren.add(childPageTwo);
  175. expectedPermittedPage.add(childPageTwo);
  176. expectedPermittedPage.add(childPage);
  177. naturalChildrenAction.setSort("");
  178. naturalChildrenAction.setReverse(true);
  179. naturalChildrenAction.setPage(parentPage);
  180. when(permissionManager.hasPermission(eq(AuthenticatedUserThreadLocal.get()),
  181. eq(Permission.VIEW), eq(parentPage))).thenReturn(true);
  182. when(permissionCheckExemptions.isExempt(eq(AuthenticatedUserThreadLocal.get()))).thenReturn(false);
  183. when(contentPermissionManager.getPermittedChildren(eq(parentPage), eq(AuthenticatedUserThreadLocal.get()))).thenReturn(permittedChildren);
  184. when(contentPermissionManager.getPermittedPagesIgnoreInheritedPermissions(
  185. any(), any(), any())).thenReturn(permittedChildren);
  186. assertEquals(expectedPermittedPage, naturalChildrenAction.getPermittedChildren(parentPage));
  187. }
  188. @Test
  189. public void testCurrentPageIsAncestorPage() {
  190. String[] ancestors = {"100"};
  191. Page testPage = new Page() {
  192. @Override
  193. public String getIdAsString() {
  194. return "100";
  195. }
  196. };
  197. naturalChildrenAction.setAncestors(ancestors);
  198. assertTrue(naturalChildrenAction.isAncestorPage(testPage));
  199. }
  200. @Test
  201. public void testCurrentPageIsNotAncestorPage() {
  202. String[] ancestors = {"200"};
  203. Page testPage = new Page() {
  204. @Override
  205. public String getIdAsString() {
  206. return "100";
  207. }
  208. };
  209. naturalChildrenAction.setAncestors(ancestors);
  210. assertFalse(naturalChildrenAction.isAncestorPage(testPage));
  211. }
  212. @Test
  213. public void testCurrentPageNotSetToExpand() throws Exception {
  214. long id = 100;
  215. when(permissionManager.hasPermission(eq(AuthenticatedUserThreadLocal.get()),
  216. eq(Permission.VIEW), eq(space))).thenReturn(true);
  217. naturalChildrenAction.setSpace(space);
  218. naturalChildrenAction.setExpandCurrent(false);
  219. naturalChildrenAction.setTreePageId(id);
  220. naturalChildrenAction.doDefault();
  221. assertFalse(naturalChildrenAction.getIdsToExpand().contains(id));
  222. }
  223. @Test
  224. public void testCurrentPageSetToExpand() throws Exception {
  225. long id = 100;
  226. when(permissionManager.hasPermission(eq(AuthenticatedUserThreadLocal.get()),
  227. eq(Permission.VIEW), eq(space))).thenReturn(true);
  228. naturalChildrenAction.setSpace(space);
  229. naturalChildrenAction.setExpandCurrent(true);
  230. naturalChildrenAction.setTreePageId(id);
  231. naturalChildrenAction.doDefault();
  232. assertTrue(naturalChildrenAction.getIdsToExpand().contains(id));
  233. }
  234. @Test
  235. public void testGetAllStartPagesReturnsTopLevelPagesAndNotOrphanedPages() {
  236. Space aSpace = new Space(spaceKey);
  237. Page theOnlyTopLevelPage = new Page();
  238. theOnlyTopLevelPage.setSpace(aSpace);
  239. theOnlyTopLevelPage.setTitle("Top level page 1");
  240. when(spaceManager.getSpace(spaceKey)).thenReturn(aSpace);
  241. when(pageManager.getTopLevelPages(aSpace)).thenReturn(singletonList(theOnlyTopLevelPage));
  242. when(permissionManager.getPermittedEntities(eq(AuthenticatedUserThreadLocal.get()), eq(Permission.VIEW), eq(singletonList(theOnlyTopLevelPage))))
  243. .thenReturn(singletonList(theOnlyTopLevelPage));
  244. when(contentPermissionManager.getPermittedPagesIgnoreInheritedPermissions(
  245. any(), any(), any())).thenReturn(singletonList(theOnlyTopLevelPage));
  246. List allStartPages = naturalChildrenAction.getAllPermittedStartPages(spaceKey);
  247. assertEquals(singletonList(theOnlyTopLevelPage), allStartPages);
  248. verify(pageManager, never()).getOrphanedPages(anyString());
  249. }
  250. @Test
  251. public void testExcerptHtmlEmptyIfContentExcerptIsEmptyString() {
  252. when(excerptHelper.getExcerpt(page)).thenReturn("");
  253. when(pageManager.getPage(eq(page.getId()))).thenReturn(page);
  254. assertTrue(StringUtils.isBlank(naturalChildrenAction.getPageExcerptHtml(page)));
  255. }
  256. private void setNaturalChildrenActionDependencies(NaturalChildrenAction naturalChildrenAction) {
  257. naturalChildrenAction.setExcerptHelper(excerptHelper);
  258. naturalChildrenAction.setContentPermissionManager(contentPermissionManager);
  259. naturalChildrenAction.setPermissionCheckExemptions(permissionCheckExemptions);
  260. naturalChildrenAction.setXhtmlContent(xhtmlContent);
  261. naturalChildrenAction.setPageManager(pageManager);
  262. naturalChildrenAction.setSpaceManager(spaceManager);
  263. naturalChildrenAction.setPermissionManager(permissionManager);
  264. naturalChildrenAction.setSpaceKey(spaceKey);
  265. naturalChildrenAction.setExcerpt(false);
  266. naturalChildrenAction.setSort("");
  267. naturalChildrenAction.setReverse(false);
  268. naturalChildrenAction.setTreeId("10");
  269. naturalChildrenAction.setStartDepth(0);
  270. naturalChildrenAction.setHasRoot(false);
  271. naturalChildrenAction.setDisableLinks(false);
  272. naturalChildrenAction.setExpandCurrent(false);
  273. naturalChildrenAction.setTreePageId(null);
  274. }
  275. }