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.portal.util.test;
016    
017    import com.liferay.portal.kernel.cache.Lifecycle;
018    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
020    import com.liferay.portal.kernel.staging.StagingUtil;
021    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.GroupConstants;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.GroupServiceUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.StagingLocalServiceUtil;
034    import com.liferay.portal.test.randomizerbumpers.NumericStringRandomizerBumper;
035    import com.liferay.portal.test.randomizerbumpers.UniqueStringRandomizerBumper;
036    
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Manuel de la Pe??a
042     */
043    public class GroupTestUtil {
044    
045            public static Group addGroup() throws Exception {
046                    return addGroup(GroupConstants.DEFAULT_PARENT_GROUP_ID);
047            }
048    
049            public static Group addGroup(long parentGroupId) throws Exception {
050                    return addGroup(
051                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
052                            parentGroupId);
053            }
054    
055            public static Group addGroup(long userId, Layout layout) throws Exception {
056                    return addGroup(userId, GroupConstants.DEFAULT_PARENT_GROUP_ID, layout);
057            }
058    
059            public static Group addGroup(long userId, long parentGroupId, Layout layout)
060                    throws Exception {
061    
062                    Group scopeGroup = layout.getScopeGroup();
063    
064                    if (scopeGroup != null) {
065                            return scopeGroup;
066                    }
067    
068                    return GroupLocalServiceUtil.addGroup(
069                            userId, parentGroupId, Layout.class.getName(), layout.getPlid(),
070                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
071                            String.valueOf(layout.getPlid()), null, 0, true,
072                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, false, true,
073                            null);
074            }
075    
076            public static Group addGroup(
077                            long companyId, long userId, long parentGroupId)
078                    throws Exception {
079    
080                    String name = RandomTestUtil.randomString(
081                            NumericStringRandomizerBumper.INSTANCE,
082                            UniqueStringRandomizerBumper.INSTANCE);
083    
084                    Group group = GroupLocalServiceUtil.fetchGroup(companyId, name);
085    
086                    if (group != null) {
087                            return group;
088                    }
089    
090                    int type = GroupConstants.TYPE_SITE_OPEN;
091                    String friendlyURL =
092                            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
093                    boolean site = true;
094                    boolean active = true;
095                    boolean manualMembership = true;
096                    int membershipRestriction =
097                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION;
098    
099                    return GroupLocalServiceUtil.addGroup(
100                            userId, parentGroupId, null, 0,
101                            GroupConstants.DEFAULT_LIVE_GROUP_ID, name,
102                            RandomTestUtil.randomString(), type, manualMembership,
103                            membershipRestriction, friendlyURL, site, active,
104                            ServiceContextTestUtil.getServiceContext());
105            }
106    
107            public static Group addGroup(
108                            long parentGroupId, ServiceContext serviceContext)
109                    throws Exception {
110    
111                    String name = RandomTestUtil.randomString(
112                            NumericStringRandomizerBumper.INSTANCE,
113                            UniqueStringRandomizerBumper.INSTANCE);
114    
115                    Group group = GroupLocalServiceUtil.fetchGroup(
116                            TestPropsValues.getCompanyId(), name);
117    
118                    if (group != null) {
119                            return group;
120                    }
121    
122                    int type = GroupConstants.TYPE_SITE_OPEN;
123                    String friendlyURL =
124                            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
125                    boolean site = true;
126                    boolean active = true;
127                    boolean manualMembership = true;
128                    int membershipRestriction =
129                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION;
130    
131                    if (serviceContext == null) {
132                            serviceContext = ServiceContextTestUtil.getServiceContext();
133                    }
134    
135                    return GroupServiceUtil.addGroup(
136                            parentGroupId, GroupConstants.DEFAULT_LIVE_GROUP_ID, name,
137                            RandomTestUtil.randomString(), type, manualMembership,
138                            membershipRestriction, friendlyURL, site, active, serviceContext);
139            }
140    
141            public static void enableLocalStaging(Group group) throws Exception {
142                    ServiceContext serviceContext =
143                            ServiceContextTestUtil.getServiceContext();
144    
145                    serviceContext.setAddGroupPermissions(true);
146                    serviceContext.setAddGuestPermissions(true);
147                    serviceContext.setScopeGroupId(group.getGroupId());
148    
149                    Map<String, String[]> parameters = StagingUtil.getStagingParameters();
150    
151                    parameters.put(
152                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
153                            new String[] {Boolean.FALSE.toString()});
154                    parameters.put(
155                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
156                            new String[] {Boolean.FALSE.toString()});
157    
158                    for (String parameterName : parameters.keySet()) {
159                            serviceContext.setAttribute(
160                                    parameterName, parameters.get(parameterName)[0]);
161                    }
162    
163                    StagingLocalServiceUtil.enableLocalStaging(
164                            TestPropsValues.getUserId(), group, false, false, serviceContext);
165            }
166    
167            public static Group updateDisplaySettings(
168                            long groupId, Locale[] availableLocales, Locale defaultLocale)
169                    throws Exception {
170    
171                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
172    
173                    boolean inheritLocales = false;
174    
175                    if ((availableLocales == null) && (defaultLocale == null)) {
176                            inheritLocales = true;
177                    }
178    
179                    typeSettingsProperties.put(
180                            "inheritLocales", String.valueOf(inheritLocales));
181    
182                    if (availableLocales != null) {
183                            typeSettingsProperties.put(
184                                    PropsKeys.LOCALES,
185                                    StringUtil.merge(LocaleUtil.toLanguageIds(availableLocales)));
186                    }
187    
188                    if (defaultLocale != null) {
189                            typeSettingsProperties.put(
190                                    "languageId", LocaleUtil.toLanguageId(defaultLocale));
191                    }
192    
193                    Group group = GroupLocalServiceUtil.updateGroup(
194                            groupId, typeSettingsProperties.toString());
195    
196                    ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
197    
198                    return group;
199            }
200    
201    }