@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 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());
    }
  }
  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 testUpdateAssetWhenUpdatingFileEntry() throws Throwable {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    FileEntry fileEntry =
        DLAppLocalServiceUtil.addFileEntry(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString(),
            ContentTypes.TEXT_PLAIN,
            "Old Title",
            RandomTestUtil.randomString(),
            null,
            RandomTestUtil.randomBytes(),
            serviceContext);

    DLAppLocalServiceUtil.updateFileEntry(
        TestPropsValues.getUserId(),
        fileEntry.getFileEntryId(),
        RandomTestUtil.randomString(),
        ContentTypes.TEXT_PLAIN,
        "New Title",
        RandomTestUtil.randomString(),
        null,
        true,
        RandomTestUtil.randomBytes(),
        serviceContext);

    AssetEntry assetEntry =
        AssetEntryLocalServiceUtil.getEntry(
            DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());

    Assert.assertEquals("New Title", assetEntry.getTitle());
  }
  @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 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 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);
  }
  @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());
  }
  @Override
  protected void addAttachment(ClassedModel classedModel) throws Exception {
    MBMessage message = (MBMessage) classedModel;

    List<FileEntry> fileEntries = message.getAttachmentsFileEntries();

    List<String> existingFiles = new ArrayList<>();

    for (FileEntry fileEntry : fileEntries) {
      existingFiles.add(fileEntry.getTitle());
    }

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

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
        MBTestUtil.getInputStreamOVPs("OSX_Test.docx", getClass(), getSearchKeywords());

    MBMessageLocalServiceUtil.updateMessage(
        TestPropsValues.getUserId(),
        message.getMessageId(),
        getSearchKeywords(),
        getSearchKeywords(),
        inputStreamOVPs,
        existingFiles,
        0,
        false,
        serviceContext);
  }
  protected void addComment(ClassedModel classedModel, String body, ServiceContext serviceContext)
      throws Exception {

    User user = TestPropsValues.getUser();

    List<MBMessage> messages =
        MBMessageLocalServiceUtil.getMessages(
            getBaseModelClassName(),
            getBaseModelClassPK(classedModel),
            WorkflowConstants.STATUS_ANY);

    MBMessage message = messages.get(0);

    MBMessageLocalServiceUtil.addDiscussionMessage(
        user.getUserId(),
        user.getFullName(),
        serviceContext.getScopeGroupId(),
        getBaseModelClassName(),
        getBaseModelClassPK(classedModel),
        message.getThreadId(),
        message.getMessageId(),
        message.getSubject(),
        body,
        serviceContext);
  }
  @Test
  public void testLocalizedSiteAddFileEntryType() throws Exception {
    Group group = GroupTestUtil.addGroup();

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

    Locale locale = LocaleThreadLocal.getSiteDefaultLocale();

    try {
      LocaleThreadLocal.setSiteDefaultLocale(LocaleUtil.SPAIN);

      String name = RandomTestUtil.randomString();
      String description = RandomTestUtil.randomString();
      DDMStructure ddmStructure =
          DDMStructureTestUtil.addStructure(
              DLFileEntry.class.getName(), new Locale[] {LocaleUtil.SPAIN}, LocaleUtil.SPAIN);

      DLFileEntryType dlFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              TestPropsValues.getUserId(),
              group.getGroupId(),
              name,
              description,
              new long[] {ddmStructure.getStructureId()},
              serviceContext);

      Assert.assertEquals(name, dlFileEntryType.getName(LocaleUtil.US, true));
      Assert.assertEquals(description, dlFileEntryType.getDescription(LocaleUtil.US, true));
    } finally {
      LocaleThreadLocal.setSiteDefaultLocale(locale);
    }
  }
  @Before
  public void setUp() throws Exception {
    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    _group = GroupTestUtil.addGroup();

    _folder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Folder A",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _subfolder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            _folder.getFolderId(),
            "SubFolder AA",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _basicDocumentDLFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);

    _dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(_group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : _dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryType = dlFileEntryType;
      } else if (name.equals(DLFileEntryTypeConstants.NAME_MARKETING_BANNER)) {

        _marketingBannerDLFileEntryType = dlFileEntryType;
      }
    }
  }
  @After
  @Override
  public void tearDown() throws Exception {
    super.tearDown();

    ExpandoTableLocalServiceUtil.deleteTables(
        TestPropsValues.getCompanyId(), Group.class.getName());
  }
  @Override
  protected StagedModel addStagedModel(
      Group group, Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    List<StagedModel> dependentStagedModels =
        dependentStagedModelsMap.get(WikiNode.class.getSimpleName());

    WikiNode node = (WikiNode) dependentStagedModels.get(0);

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

    WikiPage page =
        WikiTestUtil.addPage(
            TestPropsValues.getUserId(),
            node.getNodeId(),
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString(),
            true,
            serviceContext);

    WikiTestUtil.addWikiAttachment(
        TestPropsValues.getUserId(), node.getNodeId(), page.getTitle(), WikiAttachmentsTest.class);

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

    FileEntry fileEntry = attachmentsFileEntries.get(0);

    Folder folder = fileEntry.getFolder();

    while (folder != null) {
      addDependentStagedModel(dependentStagedModelsMap, DLFolder.class, folder);

      folder = folder.getParentFolder();
    }

    addDependentStagedModel(
        dependentStagedModelsMap, DLFileEntry.class, attachmentsFileEntries.get(0));

    Repository repository = RepositoryUtil.fetchByPrimaryKey(fileEntry.getRepositoryId());

    addDependentStagedModel(dependentStagedModelsMap, Repository.class, repository);

    return page;
  }
  @Test
  public void testAddFileEntryType() throws Exception {
    ServiceContext serviceContext = new ServiceContext();

    byte[] testFileBytes = FileUtil.getBytes(getClass(), _TEST_DDM_STRUCTURE);

    serviceContext.setAttribute("xsd", new String(testFileBytes));

    User user = TestPropsValues.getUser();

    serviceContext.setLanguageId(LocaleUtil.toLanguageId(user.getLocale()));

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.addFileEntryType(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            "Test Structure",
            StringPool.BLANK,
            new long[0],
            serviceContext);

    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();

    Assert.assertEquals(1, ddmStructures.size());

    DDMStructure ddmStructure = ddmStructures.get(0);

    Locale[] availableLocales = LocaleUtil.fromLanguageIds(ddmStructure.getAvailableLanguageIds());

    boolean hasDefaultLocale = ArrayUtil.contains(availableLocales, LocaleUtil.getSiteDefault());

    Assert.assertTrue(hasDefaultLocale);

    boolean hasHungarianLocale = ArrayUtil.contains(availableLocales, LocaleUtil.HUNGARY);

    Assert.assertTrue(hasHungarianLocale);

    boolean hasUserLocale = ArrayUtil.contains(availableLocales, user.getLocale());

    Assert.assertTrue(hasUserLocale);

    DLFileEntryTypeLocalServiceUtil.deleteDLFileEntryType(dlFileEntryType);
  }
  @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 ThemeDisplay getThemeDisplay(Group group) throws Exception {
    ThemeDisplay themeDisplay = new ThemeDisplay();

    Company company = CompanyLocalServiceUtil.getCompany(TestPropsValues.getCompanyId());

    themeDisplay.setCompany(company);

    themeDisplay.setLayoutSet(group.getPublicLayoutSet());
    themeDisplay.setServerPort(80);
    themeDisplay.setSiteGroupId(group.getGroupId());

    return themeDisplay;
  }
  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();

    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    List<DLFileEntryType> dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryTypeId = dlFileEntryType.getFileEntryTypeId();
      }
    }

    ExpandoTable expandoTable =
        ExpandoTableLocalServiceUtil.addDefaultTable(
            PortalUtil.getDefaultCompanyId(), DLFileEntry.class.getName());

    ExpandoColumnLocalServiceUtil.addColumn(
        expandoTable.getTableId(),
        _EXPANDO_ATTRIBUTE_NAME,
        ExpandoColumnConstants.STRING,
        StringPool.BLANK);

    _serviceContext = getServiceContext();

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            group.getGroupId(),
            parentFolder.getFolderId(),
            _SOURCE_FILE_NAME,
            ContentTypes.APPLICATION_OCTET_STREAM,
            _TITLE,
            StringPool.BLANK,
            StringPool.BLANK,
            _DATA_VERSION_1,
            _serviceContext);

    _fileVersion =
        DLFileVersionLocalServiceUtil.getFileVersion(
            fileEntry.getFileEntryId(), DLFileEntryConstants.VERSION_DEFAULT);
  }
  protected void searchRecentEntries() throws Exception {
    long initialSearchGroupEntriesCount = searchGroupEntriesCount(group.getGroupId(), 0);

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

    BaseModel<?> parentBaseModel1 = getParentBaseModel(group, serviceContext);
    BaseModel<?> parentBaseModel2 = getParentBaseModel(group, serviceContext);

    String name = PrincipalThreadLocal.getName();

    try {
      User user1 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user1.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);

      User user2 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user2.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);
    } finally {
      PrincipalThreadLocal.setName(name);
    }

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));

    moveParentBaseModelToTrash((Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 3, searchGroupEntriesCount(group.getGroupId(), 0));

    TrashHandler parentTrashHandler =
        TrashHandlerRegistryUtil.getTrashHandler(getParentBaseModelClassName());

    parentTrashHandler.restoreTrashEntry(
        TestPropsValues.getUserId(), (Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));
  }
  @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);
  }
  @Override
  protected BaseModel<?> getParentBaseModel(Group group, ServiceContext serviceContext)
      throws Exception {

    serviceContext = (ServiceContext) serviceContext.clone();

    serviceContext.setWorkflowAction(WorkflowConstants.STATUS_APPROVED);

    return WikiNodeLocalServiceUtil.addNode(
        TestPropsValues.getUserId(),
        RandomTestUtil.randomString(_NODE_NAME_MAX_LENGTH),
        RandomTestUtil.randomString(),
        serviceContext);
  }
  @Before
  public void setUp() throws Exception {
    _group = GroupTestUtil.addGroup();

    Map<Locale, String> nameMap = new HashMap<>();

    nameMap.put(LocaleUtil.GERMANY, "Zuhause1");
    nameMap.put(LocaleUtil.SPAIN, "Casa1");
    nameMap.put(LocaleUtil.US, "Home1");

    Map<Locale, String> friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.GERMANY, "/zuhause1");
    friendlyURLMap.put(LocaleUtil.SPAIN, "/casa1");
    friendlyURLMap.put(LocaleUtil.US, "/home1");

    _layout1 = LayoutTestUtil.addLayout(_group.getGroupId(), false, nameMap, friendlyURLMap);

    nameMap = new HashMap<>();

    nameMap.put(LocaleUtil.GERMANY, "Zuhause2");
    nameMap.put(LocaleUtil.SPAIN, "Casa2");
    nameMap.put(LocaleUtil.US, "Home2");

    friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.GERMANY, "/zuhause2");
    friendlyURLMap.put(LocaleUtil.SPAIN, "/casa2");
    friendlyURLMap.put(LocaleUtil.US, "/home2");

    _layout2 = LayoutTestUtil.addLayout(_group.getGroupId(), false, nameMap, friendlyURLMap);

    if (_defaultGroup == null) {
      _defaultGroup =
          GroupLocalServiceUtil.getGroup(
              TestPropsValues.getCompanyId(), PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);

      _defaultGrouplayout1 =
          LayoutLocalServiceUtil.fetchFirstLayout(
              _defaultGroup.getGroupId(), false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

      if (_defaultGrouplayout1 == null) {
        _defaultGrouplayout1 = LayoutTestUtil.addLayout(_defaultGroup);
      }

      _defaultGrouplayout2 = LayoutTestUtil.addLayout(_defaultGroup.getGroupId());
    }
  }
  @Test
  public void testJournalFolderTreePathWithJournalFolderInTrash() throws Exception {

    JournalFolder parentFolder =
        JournalTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString());

    JournalFolder folder =
        JournalTestUtil.addFolder(
            _group.getGroupId(), parentFolder.getFolderId(), RandomTestUtil.randomString());

    JournalFolderLocalServiceUtil.moveFolderToTrash(
        TestPropsValues.getUserId(), folder.getFolderId());

    JournalFolderLocalServiceUtil.deleteFolder(parentFolder.getFolderId(), false);

    doVerify();
  }
  @Test
  public void testJournalArticleTreePathWithJournalArticleInTrash() throws Exception {

    JournalFolder parentFolder =
        JournalTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString());

    JournalArticle article =
        JournalTestUtil.addArticle(
            _group.getGroupId(), parentFolder.getFolderId(), "title", "content");

    JournalArticleLocalServiceUtil.moveArticleToTrash(
        TestPropsValues.getUserId(), _group.getGroupId(), article.getArticleId());

    JournalFolderLocalServiceUtil.deleteFolder(parentFolder.getFolderId(), false);

    doVerify();
  }
  @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);
  }