001
014
015 package com.liferay.portal.security.membershippolicy.util.test;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
019 import com.liferay.portal.kernel.test.util.RandomTestUtil;
020 import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
021 import com.liferay.portal.kernel.test.util.TestPropsValues;
022 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
023 import com.liferay.portal.kernel.util.LocaleUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.model.Address;
026 import com.liferay.portal.model.Company;
027 import com.liferay.portal.model.EmailAddress;
028 import com.liferay.portal.model.Group;
029 import com.liferay.portal.model.GroupConstants;
030 import com.liferay.portal.model.ListTypeConstants;
031 import com.liferay.portal.model.Organization;
032 import com.liferay.portal.model.OrganizationConstants;
033 import com.liferay.portal.model.Phone;
034 import com.liferay.portal.model.Role;
035 import com.liferay.portal.model.User;
036 import com.liferay.portal.model.UserGroup;
037 import com.liferay.portal.model.UserGroupRole;
038 import com.liferay.portal.model.Website;
039 import com.liferay.portal.service.CompanyLocalServiceUtil;
040 import com.liferay.portal.service.GroupServiceUtil;
041 import com.liferay.portal.service.OrganizationServiceUtil;
042 import com.liferay.portal.service.RoleServiceUtil;
043 import com.liferay.portal.service.ServiceContext;
044 import com.liferay.portal.service.UserGroupServiceUtil;
045 import com.liferay.portal.service.UserServiceUtil;
046 import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
047 import com.liferay.portlet.asset.model.AssetCategory;
048 import com.liferay.portlet.asset.model.AssetTag;
049 import com.liferay.portlet.asset.model.AssetVocabulary;
050 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
051 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
052 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
053 import com.liferay.portlet.expando.model.ExpandoBridge;
054 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
055
056 import java.io.Serializable;
057
058 import java.util.ArrayList;
059 import java.util.Calendar;
060 import java.util.HashMap;
061 import java.util.List;
062 import java.util.Locale;
063 import java.util.Map;
064
065
068 public class MembershipPolicyTestUtil {
069
070
071
072
073
074 public static Group addGroup() throws Exception {
075 String name = RandomTestUtil.randomString();
076
077 Map<Locale, String> nameMap = new HashMap<>();
078
079 nameMap.put(LocaleUtil.getDefault(), name);
080
081 String friendlyURL =
082 StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
083
084 return GroupServiceUtil.addGroup(
085 GroupConstants.DEFAULT_PARENT_GROUP_ID,
086 GroupConstants.DEFAULT_LIVE_GROUP_ID, nameMap,
087 RandomTestUtil.randomLocaleStringMap(),
088 GroupConstants.TYPE_SITE_OPEN, true,
089 GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, true,
090 true, populateServiceContext(Group.class, true));
091 }
092
093 public static Organization addOrganization() throws Exception {
094 String name = RandomTestUtil.randomString();
095
096 return OrganizationServiceUtil.addOrganization(
097 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID, name,
098 OrganizationConstants.TYPE_REGULAR_ORGANIZATION, 0, 0,
099 ListTypeConstants.ORGANIZATION_STATUS_DEFAULT, StringPool.BLANK,
100 false, populateServiceContext(Organization.class, true));
101 }
102
103 public static Role addRole(int type) throws Exception {
104 String name = RandomTestUtil.randomString();
105
106 return RoleServiceUtil.addRole(
107 null, 0, name, RandomTestUtil.randomLocaleStringMap(),
108 RandomTestUtil.randomLocaleStringMap(), type,
109 RandomTestUtil.randomString(),
110 populateServiceContext(Role.class, false));
111 }
112
113 public static User addUser(
114 long[] organizationIds, long[] roleIds, long[] siteIds,
115 long[] userGroupIds)
116 throws Exception {
117
118 boolean autoPassword = true;
119 String password1 = StringPool.BLANK;
120 String password2 = StringPool.BLANK;
121 boolean autoScreenName = true;
122 String screenName = StringPool.BLANK;
123 String emailAddress =
124 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
125 long facebookId = 0;
126 String openId = StringPool.BLANK;
127 Locale locale = LocaleUtil.getDefault();
128 String firstName = "UserServiceTest";
129 String middleName = StringPool.BLANK;
130 String lastName = "UserServiceTest";
131 long prefixId = 0;
132 long suffixId = 0;
133 boolean male = true;
134 int birthdayMonth = Calendar.JANUARY;
135 int birthdayDay = 1;
136 int birthdayYear = 1970;
137 String jobTitle = StringPool.BLANK;
138 boolean sendMail = false;
139
140 ServiceContext serviceContext = new ServiceContext();
141
142 return UserServiceUtil.addUser(
143 TestPropsValues.getCompanyId(), autoPassword, password1, password2,
144 autoScreenName, screenName, emailAddress, facebookId, openId,
145 locale, firstName, middleName, lastName, prefixId, suffixId, male,
146 birthdayMonth, birthdayDay, birthdayYear, jobTitle, siteIds,
147 organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
148 }
149
150 public static UserGroup addUserGroup() throws Exception {
151 String name = RandomTestUtil.randomString();
152 String description = RandomTestUtil.randomString(50);
153
154 return UserGroupServiceUtil.addUserGroup(
155 name, description, populateServiceContext(UserGroup.class, false));
156 }
157
158 public static void updateUser(
159 User user, long[] organizationIds, long[] roleIds, long[] siteIds,
160 long[] userGroupIds, List<UserGroupRole> userGroupRoles)
161 throws Exception {
162
163 long userId = user.getUserId();
164 String oldPassword = user.getPassword();
165 String newPassword1 = RandomTestUtil.randomString();
166 String newPassword2 = newPassword1;
167 boolean passwordReset = true;
168 String reminderQueryQuestion = RandomTestUtil.randomString();
169 String reminderQueryAnswer = RandomTestUtil.randomString();
170
171 String screenName = RandomTestUtil.randomString(
172 NumericStringRandomizerBumper.INSTANCE);
173 String emailAddress =
174 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
175 long facebookId = 0;
176 String openId = StringPool.BLANK;
177 String languageId = LocaleUtil.toLanguageId(Locale.getDefault());
178 String timeZoneId = RandomTestUtil.randomString();
179 String greeting = RandomTestUtil.randomString();
180 String comments = RandomTestUtil.randomString();
181 String firstName = "UserServiceTest";
182 String middleName = StringPool.BLANK;
183 String lastName = "UserServiceTest";
184 long prefixId = 0;
185 long suffixId = 0;
186 boolean male = true;
187 int birthdayMonth = Calendar.JANUARY;
188 int birthdayDay = 1;
189 int birthdayYear = 1970;
190 String jobTitle = StringPool.BLANK;
191 String smsSn =
192 "UserServiceTestSmsSn." + RandomTestUtil.nextInt() + "@liferay.com";
193 String aimSn = RandomTestUtil.randomString();
194 String facebookSn = RandomTestUtil.randomString();
195 String icqSn = RandomTestUtil.randomString();
196 String jabberSn = RandomTestUtil.randomString();
197 String msnSn =
198 "UserServiceTestMsnSn." + RandomTestUtil.nextInt() + "@liferay.com";
199 String mySpaceSn = RandomTestUtil.randomString();
200 String skypeSn = RandomTestUtil.randomString();
201 String twitterSn = RandomTestUtil.randomString();
202 String ymSn = RandomTestUtil.randomString();
203
204 List<Address> addresses = new ArrayList<>();
205 List<EmailAddress> emailAddresses = new ArrayList<>();
206 List<Phone> phones = new ArrayList<>();
207 List<Website> websites = new ArrayList<>();
208 List<AnnouncementsDelivery> announcementsDelivers = new ArrayList<>();
209
210 ServiceContext serviceContext = new ServiceContext();
211
212 UserServiceUtil.updateUser(
213 userId, oldPassword, newPassword1, newPassword2, passwordReset,
214 reminderQueryQuestion, reminderQueryAnswer, screenName,
215 emailAddress, facebookId, openId, false, null, languageId,
216 timeZoneId, greeting, comments, firstName, middleName, lastName,
217 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
218 smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
219 skypeSn, twitterSn, ymSn, jobTitle, siteIds, organizationIds,
220 roleIds, userGroupRoles, userGroupIds, addresses, emailAddresses,
221 phones, websites, announcementsDelivers, serviceContext);
222 }
223
224 protected static Map<String, Serializable> addExpandoMap(Class<?> clazz)
225 throws PortalException {
226
227 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
228 TestPropsValues.getCompanyId(), clazz.getName());
229
230 expandoBridge.addAttribute("key1", false);
231 expandoBridge.addAttribute("key2", false);
232 expandoBridge.addAttribute("key3", false);
233 expandoBridge.addAttribute("key4", false);
234
235 Map<String, Serializable> expandoMap = new HashMap<>();
236
237 expandoMap.put("key1", "value1");
238 expandoMap.put("key2", "value2");
239 expandoMap.put("key3", "value3");
240 expandoMap.put("key4", "value4");
241
242 return expandoMap;
243 }
244
245 protected static ServiceContext populateServiceContext(
246 Class<?> clazz, boolean includeCategorization)
247 throws Exception {
248
249 ServiceContext serviceContext =
250 ServiceContextTestUtil.getServiceContext();
251
252 serviceContext.setAddGroupPermissions(true);
253 serviceContext.setAddGuestPermissions(true);
254
255 if (includeCategorization) {
256 AssetTag tag = AssetTagLocalServiceUtil.addTag(
257 TestPropsValues.getUserId(), TestPropsValues.getGroupId(),
258 RandomTestUtil.randomString(), new ServiceContext());
259
260 serviceContext.setAssetTagNames(new String[] {tag.getName()});
261
262 Company company = CompanyLocalServiceUtil.getCompany(
263 TestPropsValues.getCompanyId());
264
265 serviceContext.setScopeGroupId(company.getGroupId());
266
267 AssetVocabulary vocabulary =
268 AssetVocabularyLocalServiceUtil.addVocabulary(
269 TestPropsValues.getUserId(), company.getGroupId(),
270 RandomTestUtil.randomString(), serviceContext);
271
272 AssetCategory category = AssetCategoryLocalServiceUtil.addCategory(
273 TestPropsValues.getUserId(), company.getGroupId(),
274 RandomTestUtil.randomString(), vocabulary.getVocabularyId(),
275 serviceContext);
276
277 serviceContext.setAssetCategoryIds(
278 new long[] {category.getCategoryId()});
279 }
280
281 serviceContext.setExpandoBridgeAttributes(addExpandoMap(clazz));
282
283 return serviceContext;
284 }
285
286 }