@Test
  public void testRestoreRedirectPageWithParentPageFromTrash() throws Exception {

    int initialBaseModelsCount = getNotInTrashBaseModelsCount(_node);
    int initialTrashEntriesCount = getTrashEntriesCount(group.getGroupId());

    WikiPage[] pages =
        WikiTestUtil.addRenamedParentPageWithChildPageAndGrandchildPage(
            group.getGroupId(), _node.getNodeId());

    WikiPage parentPage = pages[0];
    WikiPage redirectPage = pages[1];
    WikiPage childPage = pages[2];
    WikiPage grandchildPage = pages[3];

    moveBaseModelToTrash(redirectPage.getPrimaryKey());

    restoreTrashEntry(redirectPage);

    parentPage = WikiPageLocalServiceUtil.getPageByPageId(parentPage.getPageId());
    redirectPage = WikiPageLocalServiceUtil.getPageByPageId(redirectPage.getPageId());
    childPage = WikiPageLocalServiceUtil.getPageByPageId(childPage.getPageId());
    grandchildPage = WikiPageLocalServiceUtil.getPageByPageId(grandchildPage.getPageId());

    Assert.assertFalse(parentPage.isInTrash());
    Assert.assertFalse(redirectPage.isInTrashExplicitly());
    Assert.assertEquals(parentPage.getTitle(), redirectPage.getRedirectTitle());
    Assert.assertFalse(childPage.isInTrash());
    Assert.assertEquals(parentPage.getTitle(), childPage.getParentTitle());
    Assert.assertFalse(grandchildPage.isInTrash());
    Assert.assertEquals(childPage.getTitle(), grandchildPage.getParentTitle());
    Assert.assertEquals(initialBaseModelsCount + 4, getNotInTrashBaseModelsCount(_node));
    Assert.assertEquals(initialTrashEntriesCount, getTrashEntriesCount(group.getGroupId()));
  }
  @Test
  public void testRestoreExplicitlyTrashedParentPageWithRedirectPageFromTrash() throws Exception {

    WikiPage[] pages =
        WikiTestUtil.addRenamedTrashedParentPage(
            group.getGroupId(), _node.getNodeId(), false, true);

    WikiPage parentPage = pages[0];
    WikiPage childPage = pages[1];
    WikiPage redirectPage = pages[2];

    restoreTrashEntry(parentPage);

    parentPage = WikiPageLocalServiceUtil.getPageByPageId(parentPage.getPageId());
    childPage = WikiPageLocalServiceUtil.getPageByPageId(childPage.getPageId());
    redirectPage = WikiPageLocalServiceUtil.getPageByPageId(redirectPage.getPageId());

    Assert.assertFalse(parentPage.isInTrash());
    Assert.assertFalse(childPage.isInTrash());
    Assert.assertEquals(childPage.getParentTitle(), parentPage.getTitle());
    Assert.assertTrue(redirectPage.isInTrashExplicitly());
    Assert.assertEquals(redirectPage.getRedirectTitle(), parentPage.getTitle());

    restoreTrashEntry(redirectPage);
  }
  @Override
  protected void moveBaseModelToTrash(long primaryKey) throws Exception {
    WikiPage page = WikiPageLocalServiceUtil.getPageByPageId(primaryKey);

    WikiPageLocalServiceUtil.movePageToTrash(
        TestPropsValues.getUserId(), page.getNodeId(), page.getTitle());
  }
  @Test
  public void testMoveInitialParentPageToTrash() throws Exception {
    int initialBaseModelsCount = getNotInTrashBaseModelsCount(_node);
    int initialTrashEntriesCount = getTrashEntriesCount(group.getGroupId());

    WikiPage[] pages =
        WikiTestUtil.addPageWithChangedParentPage(group.getGroupId(), _node.getNodeId());

    WikiPage page = pages[0];
    WikiPage finalParentPage = pages[1];
    WikiPage initialParentPage = pages[2];

    WikiPageLocalServiceUtil.movePageToTrash(TestPropsValues.getUserId(), initialParentPage);

    page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
    finalParentPage = WikiPageLocalServiceUtil.getPageByPageId(finalParentPage.getPageId());
    initialParentPage = WikiPageLocalServiceUtil.getPageByPageId(initialParentPage.getPageId());

    Assert.assertFalse(page.isInTrash());
    Assert.assertFalse(finalParentPage.isInTrash());
    Assert.assertTrue(initialParentPage.isInTrashExplicitly());
    Assert.assertEquals(finalParentPage.getTitle(), page.getParentTitle());
    Assert.assertEquals(initialBaseModelsCount + 2, getNotInTrashBaseModelsCount(_node));
    Assert.assertEquals(initialTrashEntriesCount + 1, getTrashEntriesCount(group.getGroupId()));
    Assert.assertEquals(page.getParentTitle(), finalParentPage.getTitle());
  }
  @Test
  public void testChangeRedirectPageNode() throws Exception {
    WikiNode destinationNode = WikiTestUtil.addNode(_group.getGroupId());

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(),
        _group.getGroupId(),
        _node.getNodeId(),
        "InitialTitlePage",
        true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "InitialTitlePage",
        "RenamedPage",
        serviceContext);

    try {
      WikiPageLocalServiceUtil.changeNode(
          TestPropsValues.getUserId(),
          _node.getNodeId(),
          "InitialTitlePage",
          destinationNode.getNodeId(),
          serviceContext);

      Assert.fail();
    } catch (NodeChangeException nce) {
      Assert.assertEquals("InitialTitlePage", nce.getPageTitle());
      Assert.assertEquals(_node.getName(), nce.getNodeName());
      Assert.assertEquals(NodeChangeException.REDIRECT_PAGE, nce.getType());
    }
  }
  @Test
  public void testRenameRenamedPage() throws Exception {
    WikiTestUtil.addPage(
        TestPropsValues.getUserId(), _group.getGroupId(), _node.getNodeId(), "A", true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(), _node.getNodeId(), "A", "B", true, serviceContext);

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(), _node.getNodeId(), "A", "C", true, serviceContext);

    WikiPage pageA = WikiPageLocalServiceUtil.getPage(_node.getNodeId(), "A");
    WikiPage pageB = WikiPageLocalServiceUtil.getPage(_node.getNodeId(), "B");
    WikiPage pageC = WikiPageLocalServiceUtil.getPage(_node.getNodeId(), "C");

    Assert.assertEquals("C", pageA.getRedirectTitle());
    Assert.assertEquals(StringPool.BLANK, pageB.getRedirectTitle());
    Assert.assertEquals(StringPool.BLANK, pageC.getRedirectTitle());
    Assert.assertEquals("Renamed as C", pageA.getSummary());
    Assert.assertEquals("Summary", pageB.getSummary());
    Assert.assertEquals(StringPool.BLANK, pageC.getSummary());
    Assert.assertEquals("[[C]]", pageA.getContent());
    Assert.assertEquals("[[B]]", pageC.getContent());
  }
  @Test
  public void testChangeChildPageNode() throws Exception {
    WikiNode destinationNode = WikiTestUtil.addNode(_group.getGroupId());

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(), _group.getGroupId(), _node.getNodeId(), "ParentPage", true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "ChildPage",
        RandomTestUtil.randomString(),
        "ParentPage",
        true,
        serviceContext);

    WikiPageLocalServiceUtil.changeNode(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "ChildPage",
        destinationNode.getNodeId(),
        serviceContext);

    WikiPage parentPage = WikiPageLocalServiceUtil.getPage(_node.getNodeId(), "ParentPage");
    WikiPage childPage = WikiPageLocalServiceUtil.getPage(destinationNode.getNodeId(), "ChildPage");

    Assert.assertEquals(_node.getNodeId(), parentPage.getNodeId());
    Assert.assertEquals(destinationNode.getNodeId(), childPage.getNodeId());
    Assert.assertTrue(Validator.isNull(childPage.getParentTitle()));
  }
  @Test
  public void testChangePageNodeWithRedirectPage() throws Exception {
    WikiNode destinationNode = WikiTestUtil.addNode(_group.getGroupId());

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(),
        _group.getGroupId(),
        _node.getNodeId(),
        "InitialTitlePage",
        true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "InitialTitlePage",
        "RenamedPage",
        serviceContext);

    WikiPageLocalServiceUtil.changeNode(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "RenamedPage",
        destinationNode.getNodeId(),
        serviceContext);

    WikiPage page = WikiPageLocalServiceUtil.getPage(destinationNode.getNodeId(), "RenamedPage");
    WikiPage redirectPage =
        WikiPageLocalServiceUtil.getPage(destinationNode.getNodeId(), "InitialTitlePage");

    Assert.assertEquals(destinationNode.getNodeId(), page.getNodeId());
    Assert.assertEquals(destinationNode.getNodeId(), redirectPage.getNodeId());
    Assert.assertEquals("RenamedPage", redirectPage.getRedirectTitle());
  }
  protected void testRestorePageFromTrash(boolean hasExpandoValues) throws Exception {

    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    if (hasExpandoValues) {
      addExpandoValueToPage(page);
    }

    page =
        WikiPageLocalServiceUtil.movePageToTrash(
            TestPropsValues.getUserId(), _node.getNodeId(), page.getTitle());

    WikiPageLocalServiceUtil.restorePageFromTrash(TestPropsValues.getUserId(), page);

    WikiPage restoredPage = WikiPageLocalServiceUtil.getPage(page.getResourcePrimKey());

    Assert.assertNotNull(restoredPage);

    if (hasExpandoValues) {
      ExpandoBridge expandoBridge = page.getExpandoBridge();

      ExpandoBridge restoredExpandoBridge = restoredPage.getExpandoBridge();

      AssertUtils.assertEquals(
          restoredExpandoBridge.getAttributes(), expandoBridge.getAttributes());
    }
  }
  @Override
  public void checkDuplicateTrashEntry(TrashEntry trashEntry, long containerModelId, String newName)
      throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(trashEntry.getClassPK());

    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
      containerModelId = page.getNodeId();
    }

    String restoredTitle = page.getTitle();

    if (Validator.isNotNull(newName)) {
      restoredTitle = newName;
    }

    String originalTitle = TrashUtil.stripTrashNamespace(restoredTitle);

    WikiPageResource pageResource =
        WikiPageResourceLocalServiceUtil.fetchPageResource(containerModelId, originalTitle);

    if (pageResource != null) {
      DuplicateEntryException dee = new DuplicateEntryException();

      WikiPage duplicatePage = WikiPageLocalServiceUtil.getPage(pageResource.getResourcePrimKey());

      dee.setDuplicateEntryId(duplicatePage.getPageId());
      dee.setOldName(duplicatePage.getTitle());
      dee.setTrashEntryId(trashEntry.getEntryId());

      throw dee;
    }
  }
  @Test
  public void testChangePageNodeWithChildHierarchy() throws Exception {
    WikiNode destinationNode = WikiTestUtil.addNode(_group.getGroupId());

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(), _group.getGroupId(), _node.getNodeId(), "ParentPage", true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    List<WikiPage> childPages = createPageHierarchy("ParentPage", 10, serviceContext);

    WikiPageLocalServiceUtil.changeNode(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        "ParentPage",
        destinationNode.getNodeId(),
        serviceContext);

    WikiPage page = WikiPageLocalServiceUtil.getPage(destinationNode.getNodeId(), "ParentPage");

    Assert.assertEquals(destinationNode.getNodeId(), page.getNodeId());

    for (WikiPage childPage : childPages) {
      childPage = WikiPageLocalServiceUtil.getPage(childPage.getResourcePrimKey());

      Assert.assertEquals(destinationNode.getNodeId(), childPage.getNodeId());
    }
  }
  @Test(expected = NoSuchPageResourceException.class)
  public void testDeletePage() throws Exception {
    WikiPage page =
        WikiTestUtil.addPage(
            TestPropsValues.getUserId(), _group.getGroupId(), _node.getNodeId(), "TestPage", true);

    WikiPageLocalServiceUtil.deletePage(page);

    WikiPageLocalServiceUtil.getPage(page.getResourcePrimKey());
  }
  public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
      throws PortalException, SystemException {

    for (long classPK : classPKs) {
      WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

      if (checkPermission) {
        WikiPageServiceUtil.deletePage(page.getNodeId(), page.getTitle());
      } else {
        WikiPageLocalServiceUtil.deletePage(page.getNodeId(), page.getTitle());
      }
    }
  }
  @Override
  public String getRestoreLink(PortletRequest portletRequest, long classPK)
      throws PortalException, SystemException {

    String portletId = PortletKeys.WIKI;

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    long plid = PortalUtil.getPlidFromPortletId(page.getGroupId(), PortletKeys.WIKI);

    if (plid == LayoutConstants.DEFAULT_PLID) {
      plid = PortalUtil.getControlPanelPlid(portletRequest);

      portletId = PortletKeys.WIKI_ADMIN;
    }

    PortletURL portletURL =
        PortletURLFactoryUtil.create(portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);

    WikiNode node = page.getNode();

    portletURL.setParameter("struts_action", "/wiki/view");
    portletURL.setParameter("nodeName", node.getName());
    portletURL.setParameter("title", HtmlUtil.unescape(page.getTitle()));

    return portletURL.toString();
  }
  @Override
  public WikiPage updateStatus(int status, Map<String, Serializable> workflowContext)
      throws PortalException {

    long userId =
        GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
    long classPK =
        GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));

    ServiceContext serviceContext = (ServiceContext) workflowContext.get("serviceContext");

    WikiPage page = WikiPageLocalServiceUtil.getPageByPageId(classPK);

    return WikiPageLocalServiceUtil.updateStatus(
        userId, page, status, serviceContext, workflowContext);
  }
  @Override
  public void updateTitle(long classPK, String name) throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    page.setTitle(name);

    WikiPageLocalServiceUtil.updateWikiPage(page);

    WikiPageResource pageResource =
        WikiPageResourceLocalServiceUtil.getPageResource(page.getResourcePrimKey());

    pageResource.setTitle(name);

    WikiPageResourceLocalServiceUtil.updateWikiPageResource(pageResource);
  }
  @Override
  public TrashRenderer getTrashRenderer(long classPK) throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    return new WikiPageAssetRenderer(page);
  }
  @Override
  public String getRestoreMessage(PortletRequest portletRequest, long classPK)
      throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    return page.getTitle();
  }
  @Override
  public ContainerModel getParentContainerModel(long classPK)
      throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    return page.getNode();
  }
  @Test
  public void testGetPage() throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    WikiPage retrievedPage = WikiPageLocalServiceUtil.getPage(page.getResourcePrimKey());

    Assert.assertEquals(retrievedPage.getPageId(), page.getPageId());
  }
  @Test
  public void testChangePageNode() throws Exception {
    WikiNode destinationNode = WikiTestUtil.addNode(_group.getGroupId());

    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    WikiPageLocalServiceUtil.changeNode(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        page.getTitle(),
        destinationNode.getNodeId(),
        serviceContext);

    WikiPageLocalServiceUtil.getPage(destinationNode.getNodeId(), page.getTitle());
  }
  public void spamWikiPages(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    checkWikiPagePermission(themeDisplay.getScopeGroupId());

    long[] wikiPageIds = ParamUtil.getLongValues(actionRequest, "spamWikiPageIds");

    for (long wikiPageId : wikiPageIds) {
      WikiPage wikiPage = WikiPageLocalServiceUtil.getPageByPageId(wikiPageId);

      wikiPage.setSummary(AkismetConstants.WIKI_PAGE_MARKED_AS_SPAM);

      WikiPageLocalServiceUtil.updateWikiPage(wikiPage);
    }
  }
  public static boolean contains(
      PermissionChecker permissionChecker, long resourcePrimKey, String actionId)
      throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePrimKey, (Boolean) null);

    return contains(permissionChecker, page, actionId);
  }
  @Test(expected = NoSuchPageResourceException.class)
  public void testDeleteTrashedParentPageWithImplicitTrashedChildPage() throws Exception {

    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(_group.getGroupId(), _node.getNodeId(), false);

    WikiPage parentPage = pages[0];
    WikiPage childPage = pages[1];

    WikiPageLocalServiceUtil.deletePage(parentPage);

    try {
      WikiPageLocalServiceUtil.getPage(parentPage.getResourcePrimKey());

      Assert.fail();
    } catch (NoSuchPageResourceException nspre) {
      WikiPageLocalServiceUtil.getPage(childPage.getResourcePrimKey());
    }
  }
  public boolean isInTrash(long classPK) throws PortalException, SystemException {

    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);

    if (page.isInTrash() || page.isInTrashFolder()) {
      return true;
    }

    return false;
  }
  public static boolean contains(
      PermissionChecker permissionChecker, long resourcePrimKey, String actionId)
      throws PortalException {

    try {
      WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePrimKey, (Boolean) null);

      return contains(permissionChecker, page, actionId);
    } catch (NoSuchPageResourceException nspre) {
      return false;
    }
  }
  public static boolean contains(
      PermissionChecker permissionChecker, long nodeId, String title, String actionId)
      throws PortalException {

    try {
      WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, title, null);

      return contains(permissionChecker, page, actionId);
    } catch (NoSuchPageException nspe) {
      return WikiNodePermission.contains(permissionChecker, nodeId, ActionKeys.VIEW);
    }
  }
  @Override
  protected BaseModel<?> updateBaseModel(long primaryKey, ServiceContext serviceContext)
      throws Exception {

    WikiPage page = WikiPageLocalServiceUtil.getPageByPageId(primaryKey);

    serviceContext = (ServiceContext) serviceContext.clone();

    return WikiPageLocalServiceUtil.updatePage(
        TestPropsValues.getUserId(),
        page.getNodeId(),
        page.getTitle(),
        page.getVersion(),
        RandomTestUtil.randomString(),
        RandomTestUtil.randomString(),
        false,
        page.getFormat(),
        page.getParentTitle(),
        page.getRedirectTitle(),
        serviceContext);
  }
  @Test
  public void testDeleteTrashedParentPageWithExplicitTrashedChildPage() throws Exception {

    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(_group.getGroupId(), _node.getNodeId(), true);

    WikiPage parentPage = pages[0];
    WikiPage childPage = pages[1];

    WikiPageLocalServiceUtil.deletePage(parentPage);

    try {
      WikiPageLocalServiceUtil.getPage(parentPage.getResourcePrimKey());

      Assert.fail();
    } catch (NoSuchPageResourceException nspre) {
      childPage = WikiPageLocalServiceUtil.getPageByPageId(childPage.getPageId());

      Assert.assertNull(childPage.fetchParentPage());
    }
  }
  protected void migrateWiki() throws Exception {
    int count = WikiPageLocalServiceUtil.getWikiPagesCount();
    int pages = count / Indexer.DEFAULT_INTERVAL;

    MaintenanceUtil.appendStatus("Migrating wiki page attachments in " + count + " pages");

    for (int i = 0; i <= pages; i++) {
      int start = (i * Indexer.DEFAULT_INTERVAL);
      int end = start + Indexer.DEFAULT_INTERVAL;

      List<WikiPage> wikiPages = WikiPageLocalServiceUtil.getWikiPages(start, end);

      for (WikiPage wikiPage : wikiPages) {
        if (!wikiPage.isHead()) {
          continue;
        }

        migrateFiles(
            wikiPage.getCompanyId(), wikiPage.getAttachmentsDir(), wikiPage.getAttachmentsFiles());
      }
    }
  }