@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 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 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 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 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 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 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());
    }
  }
  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);
  }
  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 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());
  }
  @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 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);
  }
  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);
  }
  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;
  }
  @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 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());
  }
  @Override
  protected long addBaseModel(long containerModelId) throws Exception {
    WikiPage page = WikiTestUtil.addPage(group.getGroupId(), containerModelId, true);

    return page.getResourcePrimKey();
  }