001
014
015 package com.liferay.portal.kernel.test.util;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.PropsKeys;
021 import com.liferay.portal.kernel.util.PropsUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.Company;
025 import com.liferay.portal.model.Group;
026 import com.liferay.portal.model.Organization;
027 import com.liferay.portal.model.Role;
028 import com.liferay.portal.model.RoleConstants;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.model.UserGroupRole;
031 import com.liferay.portal.service.CompanyLocalServiceUtil;
032 import com.liferay.portal.service.RoleLocalServiceUtil;
033 import com.liferay.portal.service.ServiceContext;
034 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
035 import com.liferay.portal.service.UserLocalServiceUtil;
036 import com.liferay.portal.service.UserServiceUtil;
037
038 import java.util.Calendar;
039 import java.util.List;
040 import java.util.Locale;
041
042
047 public class UserTestUtil {
048
049 public static User addCompanyAdminUser(Company company) throws Exception {
050 User user = addUser();
051
052 user.setCompanyId(company.getCompanyId());
053
054 UserLocalServiceUtil.updateUser(user);
055
056 Role role = RoleLocalServiceUtil.getRole(
057 company.getCompanyId(), RoleConstants.ADMINISTRATOR);
058
059 UserLocalServiceUtil.addRoleUser(role.getRoleId(), user);
060
061 return user;
062 }
063
064 public static User addGroupAdminUser(Group group) throws Exception {
065 return UserTestUtil.addGroupUser(
066 group, RoleConstants.SITE_ADMINISTRATOR);
067 }
068
069 public static User addGroupOwnerUser(Group group) throws Exception {
070 return UserTestUtil.addGroupUser(group, RoleConstants.SITE_OWNER);
071 }
072
073 public static User addGroupUser(Group group, String roleName)
074 throws Exception {
075
076 User groupUser = addUser(group.getGroupId());
077
078 Role role = RoleLocalServiceUtil.getRole(
079 TestPropsValues.getCompanyId(), roleName);
080
081 long[] userIds = {groupUser.getUserId()};
082
083 UserGroupRoleLocalServiceUtil.addUserGroupRoles(
084 userIds, group.getGroupId(), role.getRoleId());
085
086 return groupUser;
087 }
088
089 public static User addOmniAdminUser() throws Exception {
090 Company company = CompanyLocalServiceUtil.getCompanyByMx(
091 PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
092
093 return addCompanyAdminUser(company);
094 }
095
096 public static User addOrganizationAdminUser(Organization organization)
097 throws Exception {
098
099 return UserTestUtil.addOrganizationUser(
100 organization, RoleConstants.ORGANIZATION_ADMINISTRATOR);
101 }
102
103 public static User addOrganizationOwnerUser(Organization organization)
104 throws Exception {
105
106 return UserTestUtil.addOrganizationUser(
107 organization, RoleConstants.ORGANIZATION_OWNER);
108 }
109
110 public static User addOrganizationUser(
111 Organization organization, String roleName)
112 throws Exception {
113
114 User organizationUser = addUser(organization.getGroupId());
115
116 UserLocalServiceUtil.addOrganizationUser(
117 organization.getOrganizationId(), organizationUser.getUserId());
118
119 addUserGroupRole(
120 organizationUser.getUserId(), organization.getGroupId(), roleName);
121
122 return organizationUser;
123 }
124
125 public static User addUser() throws Exception {
126 return addUser(
127 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
128 RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
129 LocaleUtil.getDefault(), RandomTestUtil.randomString(),
130 RandomTestUtil.randomString(),
131 new long[] {TestPropsValues.getGroupId()},
132 ServiceContextTestUtil.getServiceContext());
133 }
134
135 public static User addUser(boolean secure) throws Exception {
136 boolean autoPassword = true;
137 String password1 = StringPool.BLANK;
138 String password2 = StringPool.BLANK;
139 boolean autoScreenName = true;
140 String screenName = StringPool.BLANK;
141 long facebookId = 0;
142 String openId = StringPool.BLANK;
143 Locale locale = LocaleUtil.getDefault();
144 String firstName = "UserServiceTest";
145 String middleName = StringPool.BLANK;
146 String lastName = "UserServiceTest";
147 long prefixId = 0;
148 long suffixId = 0;
149 boolean male = true;
150 int birthdayMonth = Calendar.JANUARY;
151 int birthdayDay = 1;
152 int birthdayYear = 1970;
153 String jobTitle = StringPool.BLANK;
154 long[] groupIds = null;
155 long[] organizationIds = null;
156 long[] roleIds = null;
157 long[] userGroupIds = null;
158 boolean sendMail = false;
159
160 ServiceContext serviceContext = new ServiceContext();
161
162 if (secure) {
163 String emailAddress =
164 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
165
166 return UserServiceUtil.addUser(
167 TestPropsValues.getCompanyId(), autoPassword, password1,
168 password2, autoScreenName, screenName, emailAddress, facebookId,
169 openId, locale, firstName, middleName, lastName, prefixId,
170 suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
171 jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
172 sendMail, serviceContext);
173 }
174 else {
175 String emailAddress =
176 "UserServiceTest." + RandomTestUtil.nextLong() + "@test.com";
177
178 return UserLocalServiceUtil.addUser(
179 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
180 autoPassword, password1, password2, autoScreenName, screenName,
181 emailAddress, facebookId, openId, locale, firstName, middleName,
182 lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
183 birthdayYear, jobTitle, groupIds, organizationIds, roleIds,
184 userGroupIds, sendMail, serviceContext);
185 }
186 }
187
188 public static User addUser(long... groupIds) throws Exception {
189 return addUser(
190 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
191 RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
192 LocaleUtil.getDefault(), RandomTestUtil.randomString(),
193 RandomTestUtil.randomString(), groupIds,
194 ServiceContextTestUtil.getServiceContext());
195 }
196
197 public static User addUser(long groupId, Locale locale) throws Exception {
198 return addUser(
199 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
200 RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
201 locale, RandomTestUtil.randomString(),
202 RandomTestUtil.randomString(), new long[] {groupId},
203 ServiceContextTestUtil.getServiceContext());
204 }
205
206 public static User addUser(
207 long companyId, long userId, String screenName, Locale locale,
208 String firstName, String lastName, long[] groupIds,
209 ServiceContext serviceContext)
210 throws Exception {
211
212 User user = UserLocalServiceUtil.fetchUserByScreenName(
213 companyId, screenName);
214
215 if (user != null) {
216 return user;
217 }
218
219 boolean autoPassword = true;
220 String password1 = StringPool.BLANK;
221 String password2 = StringPool.BLANK;
222 String emailAddress =
223 RandomTestUtil.randomString() + RandomTestUtil.nextLong() +
224 "@liferay.com";
225 long facebookId = 0;
226 String openId = StringPool.BLANK;
227 String middleName = StringPool.BLANK;
228 long prefixId = 0;
229 long suffixId = 0;
230 boolean male = true;
231 int birthdayMonth = Calendar.JANUARY;
232 int birthdayDay = 1;
233 int birthdayYear = 1970;
234 String jobTitle = StringPool.BLANK;
235 long[] organizationIds = null;
236 long[] roleIds = null;
237 long[] userGroupIds = null;
238 boolean sendMail = false;
239
240 return UserLocalServiceUtil.addUser(
241 userId, companyId, autoPassword, password1, password2,
242 Validator.isNull(screenName), screenName, emailAddress, facebookId,
243 openId, locale, firstName, middleName, lastName, prefixId, suffixId,
244 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
245 organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
246 }
247
248 public static User addUser(
249 String screenName, Locale locale, String firstName, String lastName,
250 long[] groupIds)
251 throws Exception {
252
253 return addUser(
254 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
255 screenName, locale, firstName, lastName, groupIds,
256 ServiceContextTestUtil.getServiceContext());
257 }
258
259 public static User addUser(String screenName, long... groupIds)
260 throws Exception {
261
262 return addUser(
263 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
264 screenName, LocaleUtil.getDefault(), RandomTestUtil.randomString(),
265 RandomTestUtil.randomString(), groupIds,
266 ServiceContextTestUtil.getServiceContext());
267 }
268
269 public static void addUserGroupRole(
270 long userId, long groupId, String roleName)
271 throws Exception {
272
273 Role role = RoleLocalServiceUtil.getRole(
274 TestPropsValues.getCompanyId(), roleName);
275
276 UserGroupRoleLocalServiceUtil.addUserGroupRoles(
277 new long[] {userId}, groupId, role.getRoleId());
278 }
279
280 public static User getAdminUser(long companyId) throws PortalException {
281 Role role = RoleLocalServiceUtil.getRole(
282 companyId, RoleConstants.ADMINISTRATOR);
283
284 List<User> users = UserLocalServiceUtil.getRoleUsers(
285 role.getRoleId(), 0, 1);
286
287 if (!users.isEmpty()) {
288 return users.get(0);
289 }
290
291 return null;
292 }
293
294 public static User updateUser(User user) throws Exception {
295 ServiceContext serviceContext = new ServiceContext();
296
297 return updateUser(user, serviceContext);
298 }
299
300 public static User updateUser(User user, ServiceContext serviceContext)
301 throws Exception {
302
303 String oldPassword = StringPool.BLANK;
304 String newPassword1 = StringPool.BLANK;
305 String newPassword2 = StringPool.BLANK;
306 Boolean passwordReset = false;
307 String reminderQueryQuestion = StringPool.BLANK;
308 String reminderQueryAnswer = StringPool.BLANK;
309 String screenName = "TestUser" + RandomTestUtil.nextLong();
310 String emailAddress =
311 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
312 long facebookId = 0;
313 String openId = StringPool.BLANK;
314 String languageId = StringPool.BLANK;
315 String timeZoneId = StringPool.BLANK;
316 String greeting = StringPool.BLANK;
317 String comments = StringPool.BLANK;
318 String firstName = "UserServiceTest";
319 String middleName = StringPool.BLANK;
320 String lastName = "UserServiceTest";
321 long prefixId = 0;
322 long suffixId = 0;
323 boolean male = true;
324 int birthdayMonth = Calendar.JANUARY;
325 int birthdayDay = 1;
326 int birthdayYear = 1970;
327 String smsSn = StringPool.BLANK;
328 String aimSn = StringPool.BLANK;
329 String facebookSn = StringPool.BLANK;
330 String icqSn = StringPool.BLANK;
331 String jabberSn = StringPool.BLANK;
332 String mySpaceSn = StringPool.BLANK;
333 String skypeSn = StringPool.BLANK;
334 String twitterSn = StringPool.BLANK;
335 String ymSn = StringPool.BLANK;
336 String jobTitle = StringPool.BLANK;
337 long[] groupIds = null;
338 long[] organizationIds = null;
339 long[] roleIds = null;
340 List<UserGroupRole> userGroupRoles = null;
341 long[] userGroupIds = null;
342
343 return UserServiceUtil.updateUser(
344 user.getUserId(), oldPassword, newPassword1, newPassword2,
345 passwordReset, reminderQueryQuestion, reminderQueryAnswer,
346 screenName, emailAddress, facebookId, openId, languageId,
347 timeZoneId, greeting, comments, firstName, middleName, lastName,
348 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
349 smsSn, aimSn, facebookSn, icqSn, jabberSn, mySpaceSn, skypeSn,
350 twitterSn, ymSn, jobTitle, groupIds, organizationIds, roleIds,
351 userGroupRoles, userGroupIds, serviceContext);
352 }
353
354 }