001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.util.test;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.MimeTypesUtil;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.test.RandomTestUtil;
025    import com.liferay.portal.util.test.ServiceContextTestUtil;
026    import com.liferay.portal.util.test.TestPropsValues;
027    import com.liferay.portlet.wiki.model.WikiNode;
028    import com.liferay.portlet.wiki.model.WikiPage;
029    import com.liferay.portlet.wiki.model.WikiPageConstants;
030    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
031    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
032    
033    import java.io.File;
034    import java.io.Serializable;
035    
036    import java.util.HashMap;
037    import java.util.Map;
038    
039    /**
040     * @author Julio Camarero
041     * @author Roberto D??az
042     */
043    public class WikiTestUtil {
044    
045            public static WikiNode addNode(long groupId) throws Exception {
046                    return addNode(
047                            TestPropsValues.getUserId(), groupId, RandomTestUtil.randomString(),
048                            RandomTestUtil.randomString(50));
049            }
050    
051            public static WikiNode addNode(
052                            long userId, long groupId, String name, String description)
053                    throws Exception {
054    
055                    WorkflowThreadLocal.setEnabled(true);
056    
057                    ServiceContext serviceContext =
058                            ServiceContextTestUtil.getServiceContext(groupId);
059    
060                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
061    
062                    serviceContext = (ServiceContext)serviceContext.clone();
063    
064                    WikiNode node = WikiNodeLocalServiceUtil.addNode(
065                            userId, name, description, serviceContext);
066    
067                    return node;
068            }
069    
070            public static WikiPage addPage(long groupId, long nodeId, boolean approved)
071                    throws Exception {
072    
073                    return addPage(
074                            TestPropsValues.getUserId(), groupId, nodeId,
075                            RandomTestUtil.randomString(), approved);
076            }
077    
078            public static WikiPage addPage(
079                            long userId, long groupId, long nodeId, String title,
080                            boolean approved)
081                    throws Exception {
082    
083                    ServiceContext serviceContext =
084                            ServiceContextTestUtil.getServiceContext(groupId);
085    
086                    serviceContext.setCommand(Constants.ADD);
087                    serviceContext.setLayoutFullURL("http://localhost");
088    
089                    return addPage(
090                            userId, nodeId, title, "content", approved, serviceContext);
091            }
092    
093            public static WikiPage addPage(
094                            long userId, long nodeId, String title, String content,
095                            boolean approved, ServiceContext serviceContext)
096                    throws Exception {
097    
098                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
099    
100                    try {
101                            WorkflowThreadLocal.setEnabled(true);
102    
103                            serviceContext = (ServiceContext)serviceContext.clone();
104    
105                            serviceContext.setWorkflowAction(
106                                    WorkflowConstants.ACTION_SAVE_DRAFT);
107    
108                            WikiPage page = WikiPageLocalServiceUtil.addPage(
109                                    userId, nodeId, title, content, "Summary", false,
110                                    serviceContext);
111    
112                            if (approved) {
113                                    page = updateStatus(page, serviceContext);
114                            }
115    
116                            return page;
117                    }
118                    finally {
119                            WorkflowThreadLocal.setEnabled(workflowEnabled);
120                    }
121            }
122    
123            public static WikiPage addPage(
124                            long userId, long nodeId, String title, String content,
125                            String parentTitle, boolean approved, ServiceContext serviceContext)
126                    throws Exception {
127    
128                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
129    
130                    try {
131                            WorkflowThreadLocal.setEnabled(true);
132    
133                            serviceContext = (ServiceContext)serviceContext.clone();
134    
135                            serviceContext.setCommand(Constants.ADD);
136    
137                            serviceContext.setWorkflowAction(
138                                    WorkflowConstants.ACTION_SAVE_DRAFT);
139    
140                            WikiPage page = WikiPageLocalServiceUtil.addPage(
141                                    userId, nodeId, title, WikiPageConstants.VERSION_DEFAULT,
142                                    content, "Summary", true, WikiPageConstants.DEFAULT_FORMAT,
143                                    false, parentTitle, null, serviceContext);
144    
145                            if (approved) {
146                                    page = updateStatus(page, serviceContext);
147                            }
148    
149                            return page;
150                    }
151                    finally {
152                            WorkflowThreadLocal.setEnabled(workflowEnabled);
153                    }
154            }
155    
156            public static WikiPage[] addPageWithChangedParentPage(
157                            long groupId, long nodeId)
158                    throws Exception {
159    
160                    WikiPage initialParentPage = WikiTestUtil.addPage(
161                            TestPropsValues.getUserId(), groupId, nodeId,
162                            RandomTestUtil.randomString(), true);
163    
164                    ServiceContext serviceContext =
165                            ServiceContextTestUtil.getServiceContext(groupId);
166    
167                    WikiPage childPage = WikiTestUtil.addPage(
168                            TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
169                            RandomTestUtil.randomString(), initialParentPage.getTitle(), true,
170                            serviceContext);
171    
172                    WikiPage finalParentPage =  WikiTestUtil.addPage(
173                            TestPropsValues.getUserId(), groupId, nodeId,
174                            RandomTestUtil.randomString(), true);
175    
176                    WikiPageLocalServiceUtil.changeParent(
177                            TestPropsValues.getUserId(), nodeId, childPage.getTitle(),
178                            finalParentPage.getTitle(), serviceContext);
179    
180                    childPage = WikiPageLocalServiceUtil.getPage(
181                            nodeId, childPage.getTitle());
182                    initialParentPage =  WikiPageLocalServiceUtil.getPageByPageId(
183                            initialParentPage.getPageId());
184                    finalParentPage =  WikiPageLocalServiceUtil.getPageByPageId(
185                            finalParentPage.getPageId());
186    
187                    return new WikiPage[] {childPage, finalParentPage, initialParentPage};
188            }
189    
190            public static WikiPage[] addPageWithChildPageAndRedirectPage(
191                            long groupId, long nodeId)
192                    throws Exception {
193    
194                    WikiTestUtil.addPage(
195                            TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
196    
197                    ServiceContext serviceContext =
198                            ServiceContextTestUtil.getServiceContext(groupId);
199    
200                    WikiTestUtil.addPage(
201                            TestPropsValues.getUserId(), nodeId, "TestChildPage",
202                            RandomTestUtil.randomString(), "TestPage", true, serviceContext);
203    
204                    WikiPageLocalServiceUtil.renamePage(
205                            TestPropsValues.getUserId(), nodeId, "TestPage", "B",
206                            serviceContext);
207    
208                    WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
209                    WikiPage childPage = WikiPageLocalServiceUtil.getPage(
210                            nodeId, "TestChildPage");
211                    WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(
212                            nodeId, "TestPage");
213    
214                    return new WikiPage[]{page, childPage, redirectPage};
215            }
216    
217            public static WikiPage[] addRenamedParentPageWithChildPageAndGrandchildPage(
218                            long groupId, long nodeId)
219                    throws Exception {
220    
221                    ServiceContext serviceContext =
222                            ServiceContextTestUtil.getServiceContext(groupId);
223    
224                    WikiTestUtil.addPage(
225                            TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
226    
227                    WikiPage childPage = WikiTestUtil.addPage(
228                            TestPropsValues.getUserId(), nodeId, "TestChildPage",
229                            RandomTestUtil.randomString(), "TestPage", true, serviceContext);
230    
231                    WikiPage grandchildPage = WikiTestUtil.addPage(
232                            TestPropsValues.getUserId(), nodeId, "TestGrandchildPage",
233                            RandomTestUtil.randomString(), "TestChildPage", true,
234                            serviceContext);
235    
236                    WikiPageLocalServiceUtil.renamePage(
237                            TestPropsValues.getUserId(), nodeId, "TestPage", "B",
238                            serviceContext);
239    
240                    WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
241                    WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(
242                            nodeId, "TestPage");
243                    childPage = WikiPageLocalServiceUtil.getPageByPageId(
244                            childPage.getPageId());
245                    grandchildPage = WikiPageLocalServiceUtil.getPageByPageId(
246                            grandchildPage.getPageId());
247    
248                    return new WikiPage[] {
249                            page, redirectPage, childPage, grandchildPage};
250            }
251    
252            public static WikiPage[] addRenamedTrashedPage(
253                            long groupId, long nodeId, boolean explicitlyRemoveRedirectPage)
254                    throws Exception {
255    
256                    WikiTestUtil.addPage(
257                            TestPropsValues.getUserId(), groupId, nodeId, "A", true);
258    
259                    ServiceContext serviceContext =
260                            ServiceContextTestUtil.getServiceContext(groupId);
261    
262                    WikiPageLocalServiceUtil.renamePage(
263                            TestPropsValues.getUserId(), nodeId, "A", "B", serviceContext);
264    
265                    WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
266                    WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(nodeId, "A");
267    
268                    if (explicitlyRemoveRedirectPage) {
269                            WikiPageLocalServiceUtil.movePageToTrash(
270                                    TestPropsValues.getUserId(), nodeId, "A");
271                    }
272    
273                    WikiPageLocalServiceUtil.movePageToTrash(
274                            TestPropsValues.getUserId(), nodeId, "B");
275    
276                    page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
277                    redirectPage = WikiPageLocalServiceUtil.getPageByPageId(
278                            redirectPage.getPageId());
279    
280                    return new WikiPage[] {page, redirectPage};
281            }
282    
283            public static WikiPage[] addRenamedTrashedParentPage(
284                            long groupId, long nodeId, boolean explicitlyRemoveChildPage,
285                            boolean explicitlyRemoveRedirectPage)
286                    throws Exception {
287    
288                    ServiceContext serviceContext =
289                            ServiceContextTestUtil.getServiceContext(groupId);
290    
291                    WikiTestUtil.addPage(
292                            TestPropsValues.getUserId(), groupId, nodeId, "A", true);
293    
294                    WikiPageLocalServiceUtil.renamePage(
295                            TestPropsValues.getUserId(), nodeId, "A", "B", serviceContext);
296    
297                    WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
298                    WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(nodeId, "A");
299    
300                    WikiPage childPage = WikiTestUtil.addPage(
301                            TestPropsValues.getUserId(), nodeId, "TestChildPage",
302                            RandomTestUtil.randomString(), "B", true, serviceContext);
303    
304                    if (explicitlyRemoveChildPage) {
305                            WikiPageLocalServiceUtil.movePageToTrash(
306                                    TestPropsValues.getUserId(), nodeId, "TestChildPage");
307                    }
308    
309                    if (explicitlyRemoveRedirectPage) {
310                            WikiPageLocalServiceUtil.movePageToTrash(
311                                    TestPropsValues.getUserId(), nodeId, "A");
312                    }
313    
314                    WikiPageLocalServiceUtil.movePageToTrash(
315                            TestPropsValues.getUserId(), nodeId, "B");
316    
317                    page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
318                    childPage = WikiPageLocalServiceUtil.getPageByPageId(
319                            childPage.getPageId());
320                    redirectPage = WikiPageLocalServiceUtil.getPageByPageId(
321                            redirectPage.getPageId());
322    
323                    return new WikiPage[] {page, childPage, redirectPage};
324            }
325    
326            public static WikiPage[] addTrashedPageWithChildPage(
327                            long groupId, long nodeId, boolean explicitlyRemoveChildPage)
328                    throws Exception {
329    
330                    WikiPage page = WikiTestUtil.addPage(
331                            TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
332    
333                    ServiceContext serviceContext =
334                            ServiceContextTestUtil.getServiceContext(groupId);
335    
336                    WikiPage childPage = WikiTestUtil.addPage(
337                            TestPropsValues.getUserId(), nodeId, "TestChildPage",
338                            RandomTestUtil.randomString(), "TestPage", true, serviceContext);
339    
340                    if (explicitlyRemoveChildPage) {
341                            WikiPageLocalServiceUtil.movePageToTrash(
342                                    TestPropsValues.getUserId(), childPage);
343                    }
344    
345                    WikiPageLocalServiceUtil.movePageToTrash(
346                            TestPropsValues.getUserId(), page);
347    
348                    page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
349                    childPage = WikiPageLocalServiceUtil.getPageByPageId(
350                            childPage.getPageId());
351    
352                    return new WikiPage[] {page, childPage};
353            }
354    
355            public static WikiPage[] addTrashedParentPageWithChildPageAndGrandchildPage(
356                            long groupId, long nodeId, boolean explicitMoveChildToTrash,
357                            boolean explicitMoveParentToTrash)
358                    throws Exception {
359    
360                    WikiPage parentPage = WikiTestUtil.addPage(
361                            TestPropsValues.getUserId(), groupId, nodeId,
362                            RandomTestUtil.randomString(), true);
363    
364                    ServiceContext serviceContext =
365                            ServiceContextTestUtil.getServiceContext(groupId);
366    
367                    WikiPage childPage = WikiTestUtil.addPage(
368                            TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
369                            RandomTestUtil.randomString(), parentPage.getTitle(), true,
370                            serviceContext);
371    
372                    WikiPage grandchildPage = WikiTestUtil.addPage(
373                            TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
374                            RandomTestUtil.randomString(), childPage.getTitle(), true,
375                            serviceContext);
376    
377                    if (explicitMoveChildToTrash) {
378                            WikiPageLocalServiceUtil.movePageToTrash(
379                                    TestPropsValues.getUserId(), childPage);
380                    }
381    
382                    if (explicitMoveParentToTrash) {
383                            WikiPageLocalServiceUtil.movePageToTrash(
384                                    TestPropsValues.getUserId(), parentPage);
385                    }
386    
387                    parentPage = WikiPageLocalServiceUtil.getPageByPageId(
388                            parentPage.getPageId());
389                    childPage = WikiPageLocalServiceUtil.getPageByPageId(
390                            childPage.getPageId());
391                    grandchildPage = WikiPageLocalServiceUtil.getPageByPageId(
392                            grandchildPage.getPageId());
393    
394                    return new WikiPage[] {parentPage, childPage, grandchildPage};
395            }
396    
397            public static File addWikiAttachment(
398                            long userId, long nodeId, String title, Class<?> clazz)
399                    throws Exception {
400    
401                    String fileName = RandomTestUtil.randomString() + ".docx";
402    
403                    return addWikiAttachment(userId, nodeId, title, fileName, clazz);
404            }
405    
406            public static File addWikiAttachment(
407                            long userId, long nodeId, String title, String fileName,
408                            Class<?> clazz)
409                    throws Exception {
410    
411                    byte[] fileBytes = FileUtil.getBytes(
412                            clazz, "dependencies/OSX_Test.docx");
413    
414                    File file = null;
415    
416                    if (ArrayUtil.isNotEmpty(fileBytes)) {
417                            file = FileUtil.createTempFile(fileBytes);
418                    }
419    
420                    String mimeType = MimeTypesUtil.getExtensionContentType("docx");
421    
422                    WikiPageLocalServiceUtil.addPageAttachment(
423                            userId, nodeId, title, fileName, file, mimeType);
424    
425                    return file;
426            }
427    
428            public static WikiPage copyPage(
429                            WikiPage page, boolean approved, ServiceContext serviceContext)
430                    throws Exception {
431    
432                    WikiPage copyPage = addPage(
433                            page.getUserId(), page.getNodeId(), RandomTestUtil.randomString(),
434                            page.getContent(), approved, serviceContext);
435    
436                    WikiPageLocalServiceUtil.copyPageAttachments(
437                            page.getUserId(), page.getNodeId(), page.getTitle(),
438                            copyPage.getNodeId(), copyPage.getTitle());
439    
440                    return copyPage;
441            }
442    
443            public static WikiPage updatePage(WikiPage page) throws Exception {
444                    ServiceContext serviceContext =
445                            ServiceContextTestUtil.getServiceContext(page.getGroupId());
446    
447                    serviceContext.setCommand(Constants.UPDATE);
448                    serviceContext.setLayoutFullURL("http://localhost");
449    
450                    return updatePage(
451                            page, page.getUserId(), page.getTitle(),
452                            RandomTestUtil.randomString(50), true, serviceContext);
453            }
454    
455            public static WikiPage updatePage(
456                            WikiPage page, long userId, String content,
457                            ServiceContext serviceContext)
458                    throws Exception {
459    
460                    return updatePage(
461                            page, userId, page.getTitle(), content, true, serviceContext);
462            }
463    
464            public static WikiPage updatePage(
465                            WikiPage page, long userId, String title, String content,
466                            boolean approved, ServiceContext serviceContext)
467                    throws Exception {
468    
469                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
470    
471                    try {
472                            WorkflowThreadLocal.setEnabled(true);
473    
474                            serviceContext = (ServiceContext)serviceContext.clone();
475    
476                            serviceContext.setWorkflowAction(
477                                    WorkflowConstants.ACTION_SAVE_DRAFT);
478    
479                            page = WikiPageLocalServiceUtil.updatePage(
480                                    userId, page.getNodeId(), title, page.getVersion(), content,
481                                    page.getSummary(), false, page.getFormat(),
482                                    page.getParentTitle(), page.getRedirectTitle(), serviceContext);
483    
484                            if (approved) {
485                                    page = updateStatus(page, serviceContext);
486                            }
487    
488                            return page;
489                    }
490                    finally {
491                            WorkflowThreadLocal.setEnabled(workflowEnabled);
492                    }
493            }
494    
495            protected static WikiPage updateStatus(
496                            WikiPage page, ServiceContext serviceContext)
497                    throws Exception {
498    
499                    Map<String, Serializable> workflowContext =
500                            new HashMap<String, Serializable>();
501    
502                    workflowContext.put(WorkflowConstants.CONTEXT_URL, "http://localhost");
503    
504                    page = WikiPageLocalServiceUtil.updateStatus(
505                            page.getUserId(), page, WorkflowConstants.STATUS_APPROVED,
506                            serviceContext, workflowContext);
507    
508                    return page;
509            }
510    
511    }