protected void checkPopulatedServiceContext(
      ServiceContext serviceContext, WikiPage page, boolean hasExpandoValues) throws Exception {

    long[] assetCategoryIds =
        AssetCategoryLocalServiceUtil.getCategoryIds(
            WikiPage.class.getName(), page.getResourcePrimKey());

    Assert.assertArrayEquals(serviceContext.getAssetCategoryIds(), assetCategoryIds);

    AssetEntry assetEntry =
        AssetEntryLocalServiceUtil.getEntry(WikiPage.class.getName(), page.getResourcePrimKey());

    List<AssetLink> assetLinks = AssetLinkLocalServiceUtil.getLinks(assetEntry.getEntryId());

    long[] assetLinkEntryIds = ListUtil.toLongArray(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);

    Assert.assertArrayEquals(serviceContext.getAssetLinkEntryIds(), assetLinkEntryIds);

    String[] assetTagNames =
        AssetTagLocalServiceUtil.getTagNames(WikiPage.class.getName(), page.getResourcePrimKey());

    Assert.assertArrayEquals(serviceContext.getAssetTagNames(), assetTagNames);

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

      AssertUtils.assertEquals(
          expandoBridge.getAttributes(), serviceContext.getExpandoBridgeAttributes());
    }
  }
  public static boolean contains(
      PermissionChecker permissionChecker, WikiPage page, String actionId) {

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

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

    if (page.isPending()) {
      Boolean hasPermission =
          WorkflowPermissionUtil.hasPermission(
              permissionChecker,
              page.getGroupId(),
              WikiPage.class.getName(),
              page.getResourcePrimKey(),
              actionId);

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

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

      return true;
    }

    if (permissionChecker.hasOwnerPermission(
        page.getCompanyId(),
        WikiPage.class.getName(),
        page.getResourcePrimKey(),
        page.getUserId(),
        actionId)) {

      return true;
    }

    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
      WikiPage parentPage = page.getParentPage();

      if ((parentPage != null) && !contains(permissionChecker, parentPage, ActionKeys.VIEW)) {

        return false;
      }
    }

    return permissionChecker.hasPermission(
        page.getGroupId(), WikiPage.class.getName(), page.getResourcePrimKey(), actionId);
  }
  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());
    }
  }
  @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());
    }
  }
  private static boolean _hasPermission(
      PermissionChecker permissionChecker, WikiPage page, String actionId) {

    if (permissionChecker.hasOwnerPermission(
            page.getCompanyId(),
            WikiPage.class.getName(),
            page.getResourcePrimKey(),
            page.getUserId(),
            actionId)
        || permissionChecker.hasPermission(
            page.getGroupId(), WikiPage.class.getName(), page.getResourcePrimKey(), actionId)) {

      return true;
    }

    return false;
  }
  @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(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());
    }
  }
  @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());
  }
  @Test
  public void testDeleteTrashedPageWithExplicitTrashedRedirectPage() throws Exception {

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

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

    WikiPageLocalServiceUtil.deletePage(page);

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

      Assert.fail();
    } catch (NoSuchPageResourceException nspre) {
      redirectPage = WikiPageLocalServiceUtil.getPage(redirectPage.getResourcePrimKey());

      Assert.assertNull(redirectPage.fetchRedirectPage());
    }
  }
  @Test
  public void testDeleteTrashedPageWithRestoredChildPage() throws Exception {
    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(_group.getGroupId(), _node.getNodeId(), true);

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

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

    WikiPageLocalServiceUtil.deletePage(parentPage);

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

      Assert.fail();
    } catch (NoSuchPageResourceException nspre) {
      childPage = WikiPageLocalServiceUtil.getPage(childPage.getResourcePrimKey());

      Assert.assertNull(childPage.fetchParentPage());
      Assert.assertEquals(WorkflowConstants.STATUS_APPROVED, childPage.getStatus());
    }
  }
  @Test
  public void testRestorePageWithParentPageInTrash() throws Exception {
    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(group.getGroupId(), _node.getNodeId(), false);

    WikiPage childPage = pages[1];

    WikiPage newParentPage = WikiTestUtil.addPage(group.getGroupId(), _node.getNodeId(), true);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(getBaseModelClassName());

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

    trashHandler.moveEntry(
        TestPropsValues.getUserId(),
        childPage.getResourcePrimKey(),
        newParentPage.getResourcePrimKey(),
        serviceContext);

    childPage = WikiPageLocalServiceUtil.getPage(childPage.getResourcePrimKey());

    Assert.assertTrue(childPage.isApproved());
    Assert.assertEquals(newParentPage.getTitle(), childPage.getParentTitle());
  }
  @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);
  }
  protected void testChangeParent(boolean hasExpandoValues) throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    if (hasExpandoValues) {
      addExpandoValueToPage(page);
    }

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

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

    WikiPageLocalServiceUtil.changeParent(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        page.getTitle(),
        parentPage.getTitle(),
        serviceContext);

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

    checkPopulatedServiceContext(serviceContext, retrievedPage, hasExpandoValues);
  }
  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);
  }
  @Override
  protected long getTrashEntryClassPK(ClassedModel classedModel) {
    WikiPage page = (WikiPage) classedModel;

    return page.getResourcePrimKey();
  }
  @Override
  protected long addBaseModel(long containerModelId) throws Exception {
    WikiPage page = WikiTestUtil.addPage(group.getGroupId(), containerModelId, true);

    return page.getResourcePrimKey();
  }