@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);
  }
  @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 testGetPage() throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

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

    Assert.assertEquals(retrievedPage.getPageId(), page.getPageId());
  }
  @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 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());
    }
  }
  @Test
  public void testDeleteTrashedPageWithRestoredRedirectPage() throws Exception {

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

    WikiPage page = pages[0];
    WikiPage redirectPage = pages[1];

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

    WikiPageLocalServiceUtil.deletePage(page);

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

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

      Assert.assertNull(redirectPage.fetchRedirectPage());
      Assert.assertEquals(WorkflowConstants.STATUS_APPROVED, redirectPage.getStatus());
    }
  }
  public static boolean contains(
      PermissionChecker permissionChecker, WikiPage page, String actionId) {

    Boolean hasPermission =
        StagingPermissionUtil.hasPermission(
            permissionChecker,
            page.getGroupId(),
            WikiPage.class.getName(),
            page.getPageId(),
            PortletKeys.WIKI,
            actionId);

    if (hasPermission != null) {
      return hasPermission.booleanValue();
    }

    if (page.isDraft()) {
      if (actionId.equals(ActionKeys.VIEW)
          && !contains(permissionChecker, page, ActionKeys.UPDATE)) {

        return false;
      }

      if (actionId.equals(ActionKeys.DELETE)
          && (page.getStatusByUserId() == permissionChecker.getUserId())) {

        return true;
      }
    } else if (page.isPending()) {
      hasPermission =
          WorkflowPermissionUtil.hasPermission(
              permissionChecker,
              page.getGroupId(),
              WikiPage.class.getName(),
              page.getResourcePrimKey(),
              actionId);

      if ((hasPermission != null) && hasPermission.booleanValue()) {
        return true;
      }
    } else if (page.isScheduled()) {
      if (actionId.equals(ActionKeys.VIEW)
          && !contains(permissionChecker, page, ActionKeys.UPDATE)) {

        return false;
      }
    }

    if (actionId.equals(ActionKeys.VIEW)) {
      WikiPage redirectPage = page.fetchRedirectPage();

      if (redirectPage != null) {
        page = redirectPage;
      }

      if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
        WikiNode node = page.getNode();

        if (!WikiNodePermission.contains(permissionChecker, node, actionId)) {

          return false;
        }

        while (page != null) {
          if (!_hasPermission(permissionChecker, page, actionId)) {
            return false;
          }

          page = page.fetchParentPage();
        }

        return true;
      }
    }

    return _hasPermission(permissionChecker, page, actionId);
  }