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