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.blogs.util.test;
016    
017    import com.liferay.portal.kernel.editor.EditorConstants;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.blogs.model.BlogsEntry;
027    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Calendar;
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    import org.junit.Assert;
036    
037    /**
038     * @author Zsolt Berentey
039     */
040    public class BlogsTestUtil {
041    
042            public static BlogsEntry addEntryWithWorkflow(
043                            long userId, String title, boolean approved,
044                            ServiceContext serviceContext)
045                    throws Exception {
046    
047                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
048    
049                    try {
050                            WorkflowThreadLocal.setEnabled(true);
051    
052                            Calendar displayCalendar = CalendarFactoryUtil.getCalendar(
053                                    2012, 1, 1);
054    
055                            serviceContext = (ServiceContext)serviceContext.clone();
056    
057                            serviceContext.setWorkflowAction(
058                                    WorkflowConstants.ACTION_SAVE_DRAFT);
059    
060                            BlogsEntry entry = BlogsEntryLocalServiceUtil.addEntry(
061                                    userId, title, RandomTestUtil.randomString(),
062                                    RandomTestUtil.randomString(), RandomTestUtil.randomString(),
063                                    displayCalendar.getTime(), true, true, new String[0],
064                                    StringPool.BLANK, null, null, serviceContext);
065    
066                            if (approved) {
067                                    return updateStatus(entry, serviceContext);
068                            }
069    
070                            return entry;
071                    }
072                    finally {
073                            WorkflowThreadLocal.setEnabled(workflowEnabled);
074                    }
075            }
076    
077            public static void assertEquals(
078                    BlogsEntry expectedEntry, BlogsEntry actualEntry) {
079    
080                    Assert.assertEquals(expectedEntry.getUserId(), actualEntry.getUserId());
081                    Assert.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
082                    Assert.assertEquals(
083                            expectedEntry.getDescription(), actualEntry.getDescription());
084                    Assert.assertEquals(
085                            expectedEntry.getContent(), actualEntry.getContent());
086                    Assert.assertEquals(
087                            expectedEntry.getDisplayDate(), actualEntry.getDisplayDate());
088                    Assert.assertEquals(
089                            expectedEntry.isAllowPingbacks(), actualEntry.isAllowPingbacks());
090                    Assert.assertEquals(
091                            expectedEntry.isAllowTrackbacks(), actualEntry.isAllowTrackbacks());
092                    Assert.assertEquals(
093                            expectedEntry.isSmallImage(), actualEntry.isSmallImage());
094                    Assert.assertEquals(
095                            expectedEntry.getCoverImageFileEntryId(),
096                            actualEntry.getCoverImageFileEntryId());
097            }
098    
099            public static String getTempBlogsEntryAttachmentFileEntryImgTag(
100                    long dataImageId, String url) {
101    
102                    StringBundler sb = new StringBundler(7);
103    
104                    sb.append("<img ");
105                    sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
106                    sb.append("=\"");
107                    sb.append(dataImageId);
108                    sb.append("\" src=\"");
109                    sb.append(url);
110                    sb.append("\"/>");
111    
112                    return sb.toString();
113            }
114    
115            public static void populateNotificationsServiceContext(
116                            ServiceContext serviceContext, String command)
117                    throws Exception {
118    
119                    serviceContext.setAttribute("entryURL", "http://localhost");
120    
121                    if (Validator.isNotNull(command)) {
122                            serviceContext.setCommand(command);
123                    }
124    
125                    serviceContext.setLayoutFullURL("http://localhost");
126            }
127    
128            protected static BlogsEntry updateStatus(
129                            BlogsEntry entry, ServiceContext serviceContext)
130                    throws Exception {
131    
132                    Map<String, Serializable> workflowContext = new HashMap<>();
133    
134                    workflowContext.put(WorkflowConstants.CONTEXT_URL, "http://localhost");
135                    workflowContext.put(
136                            WorkflowConstants.CONTEXT_USER_PORTRAIT_URL, "http://localhost");
137                    workflowContext.put(
138                            WorkflowConstants.CONTEXT_USER_URL, "http://localhost");
139    
140                    return BlogsEntryLocalServiceUtil.updateStatus(
141                            entry.getUserId(), entry.getEntryId(),
142                            WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
143            }
144    
145    }