@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());
  }
  @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 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
  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());
    }
  }
  protected void testRevertPage(boolean hasExpandoValues) throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    String originalContent = RandomTestUtil.randomString();

    WikiPage originalPage =
        WikiTestUtil.addPage(
            TestPropsValues.getUserId(),
            _node.getNodeId(),
            RandomTestUtil.randomString(),
            originalContent,
            true,
            serviceContext);

    if (hasExpandoValues) {
      addExpandoValueToPage(originalPage);
    }

    WikiPage updatedPage1 =
        WikiTestUtil.updatePage(
            originalPage,
            TestPropsValues.getUserId(),
            originalContent + "\nAdded second line.",
            serviceContext);

    Assert.assertNotEquals(originalContent, updatedPage1.getContent());

    WikiPage updatedPage2 =
        WikiTestUtil.updatePage(
            updatedPage1,
            TestPropsValues.getUserId(),
            updatedPage1.getContent() + "\nAdded third line.",
            serviceContext);

    Assert.assertNotEquals(originalContent, updatedPage2.getContent());

    WikiPage revertedPage =
        WikiPageLocalServiceUtil.revertPage(
            TestPropsValues.getUserId(),
            _node.getNodeId(),
            updatedPage2.getTitle(),
            originalPage.getVersion(),
            serviceContext);

    Assert.assertEquals(revertedPage.getContent(), originalContent);

    checkPopulatedServiceContext(serviceContext, revertedPage, hasExpandoValues);
  }
  @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 testRestoreExplicitlyTrashedParentPageWithChildPageAndgrandchildPageFromTrash()
      throws Exception {

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

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

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

    restoreTrashEntry(parentPage);

    parentPage = (WikiPage) getBaseModel(parentPage.getPrimaryKey());
    childPage = (WikiPage) getBaseModel(childPage.getPrimaryKey());
    grandchildPage = (WikiPage) getBaseModel(grandchildPage.getPrimaryKey());

    Assert.assertFalse(parentPage.isInTrash());
    Assert.assertFalse(childPage.isInTrash());
    Assert.assertEquals(parentPage.getTitle(), childPage.getParentTitle());
    Assert.assertFalse(grandchildPage.isInTrash());
    Assert.assertEquals(childPage.getTitle(), grandchildPage.getParentTitle());
    Assert.assertEquals(initialBaseModelsCount + 3, getNotInTrashBaseModelsCount(_node));
    Assert.assertEquals(initialTrashEntriesCount, getTrashEntriesCount(group.getGroupId()));
  }
  @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 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 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());
  }
  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 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());
  }
  @Test
  public void testTrashVersionCreationWhenMovingToTrash() throws Exception {
    int initialTrashVersionsCount = TrashVersionLocalServiceUtil.getTrashVersionsCount();

    WikiTestUtil.addRenamedTrashedParentPage(group.getGroupId(), _node.getNodeId(), false, false);

    Assert.assertEquals(
        initialTrashVersionsCount + 3, TrashVersionLocalServiceUtil.getTrashVersionsCount());
  }
  @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 testChangePageNodeWithChildPageNameDuplication() 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(),
        "DuplicatedTitlePage",
        RandomTestUtil.randomString(),
        "ParentPage",
        true,
        serviceContext);

    WikiTestUtil.addPage(
        TestPropsValues.getUserId(),
        _group.getGroupId(),
        destinationNode.getNodeId(),
        "DuplicatedTitlePage",
        true);

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

      Assert.fail();
    } catch (NodeChangeException nce) {
      Assert.assertEquals("DuplicatedTitlePage", nce.getPageTitle());
      Assert.assertEquals(destinationNode.getName(), nce.getNodeName());
      Assert.assertEquals(NodeChangeException.DUPLICATE_PAGE, nce.getType());
    }
  }
  @Test
  public void testMoveParentPageToTrash() throws Exception {
    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(group.getGroupId(), _node.getNodeId(), false);

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

    Assert.assertTrue(parentPage.isInTrashExplicitly());
    Assert.assertTrue(childPage.isInTrashImplicitly());
    Assert.assertEquals(childPage.getParentTitle(), parentPage.getTitle());
  }
  @Test
  public void testMoveExplicitlyPageAndRedirectPageToTrash() throws Exception {

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

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

    Assert.assertTrue(page.isInTrashExplicitly());
    Assert.assertTrue(redirectPage.isInTrashExplicitly());
    Assert.assertEquals(redirectPage.getRedirectTitle(), page.getTitle());
  }
  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);
  }
  @Test(expected = DuplicatePageException.class)
  public void testRenamePageSameName() throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

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

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        page.getTitle(),
        page.getTitle(),
        true,
        serviceContext);
  }
  @Test
  public void testAddPageWithSameTitleAsImplicitlyDeletedPageVersion() throws Exception {

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

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

    String childPageTitle = childPage.getTitle();

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

    WikiPage page =
        WikiTestUtil.addPage(
            TestPropsValues.getUserId(),
            group.getGroupId(),
            _node.getNodeId(),
            childPageTitle,
            true);

    Assert.assertNotNull(page);
  }
  @Test
  public void testCopyPage() throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    WikiTestUtil.addWikiAttachment(page.getUserId(), page.getNodeId(), page.getTitle(), getClass());

    List<FileEntry> attachmentsFileEntries = page.getAttachmentsFileEntries();

    WikiPage copyPage =
        WikiTestUtil.copyPage(
            page, true, ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    List<FileEntry> copyAttachmentsFileEntries = copyPage.getAttachmentsFileEntries();

    Assert.assertEquals(copyAttachmentsFileEntries.size(), attachmentsFileEntries.size());

    FileEntry fileEntry = attachmentsFileEntries.get(0);
    FileEntry copyFileEntry = copyAttachmentsFileEntries.get(0);

    Assert.assertEquals(copyFileEntry.getExtension(), fileEntry.getExtension());
    Assert.assertEquals(copyFileEntry.getMimeType(), fileEntry.getMimeType());
    Assert.assertEquals(copyFileEntry.getTitle(), fileEntry.getTitle());
    Assert.assertEquals(copyFileEntry.getSize(), fileEntry.getSize());
  }
  @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());
  }
  @Test
  public void testTrashVersionDeletionWhenRestoringFromTrash() throws Exception {

    int initialTrashVersionCount = TrashVersionLocalServiceUtil.getTrashVersionsCount();

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

    WikiPage parentPage = pages[0];

    restoreTrashEntry(parentPage);

    Assert.assertEquals(
        initialTrashVersionCount, TrashVersionLocalServiceUtil.getTrashVersionsCount());
  }
  @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());
    }
  }
  @Override
  protected BaseModel<?> addBaseModelWithWorkflow(
      BaseModel<?> parentBaseModel, boolean approved, ServiceContext serviceContext)
      throws Exception {

    serviceContext = (ServiceContext) serviceContext.clone();

    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);

    String title = getSearchKeywords();

    title += RandomTestUtil.randomString(_PAGE_TITLE_MAX_LENGTH - title.length());

    return WikiTestUtil.addPage(
        TestPropsValues.getUserId(),
        serviceContext.getScopeGroupId(),
        (Long) parentBaseModel.getPrimaryKeyObj(),
        title,
        approved);
  }
  @Test
  public void testMoveExplicitlyChildPageWithChildPageToTrash() throws Exception {

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

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

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

    Assert.assertFalse(parentPage.isInTrash());
    Assert.assertTrue(childPage.isInTrashExplicitly());
    Assert.assertTrue(grandchildPage.isInTrashImplicitly());
    Assert.assertEquals(childPage.getTitle(), grandchildPage.getParentTitle());
    Assert.assertEquals(initialBaseModelsCount + 1, getNotInTrashBaseModelsCount(_node));
    Assert.assertEquals(initialTrashEntriesCount + 1, getTrashEntriesCount(group.getGroupId()));
  }
  @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 List<WikiPage> createPageHierarchy(
      String parentTitle, int level, ServiceContext serviceContext) throws Exception {

    List<WikiPage> pages = new ArrayList<WikiPage>();

    for (int i = 0; i < level; i++) {
      WikiPage page =
          WikiTestUtil.addPage(
              TestPropsValues.getUserId(),
              _node.getNodeId(),
              RandomTestUtil.randomString(),
              RandomTestUtil.randomString(),
              parentTitle,
              true,
              serviceContext);

      parentTitle = page.getTitle();

      pages.add(page);
    }

    return pages;
  }
  protected void testRenamePage(boolean hasExpandoValues) throws Exception {
    WikiPage page = WikiTestUtil.addPage(_group.getGroupId(), _node.getNodeId(), true);

    if (hasExpandoValues) {
      addExpandoValueToPage(page);
    }

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

    WikiPageLocalServiceUtil.renamePage(
        TestPropsValues.getUserId(),
        _node.getNodeId(),
        page.getTitle(),
        "New Title",
        true,
        serviceContext);

    WikiPage renamedPage = WikiPageLocalServiceUtil.getPage(_node.getNodeId(), "New Title");

    Assert.assertNotNull(renamedPage);

    checkPopulatedServiceContext(serviceContext, renamedPage, hasExpandoValues);
  }