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