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() + "@liferay.com";
166
167 return UserServiceUtil.addUser(
168 TestPropsValues.getCompanyId(), autoPassword, password1,
169 password2, autoScreenName, screenName, emailAddress, facebookId,
170 openId, locale, firstName, middleName, lastName, prefixId,
171 suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
172 jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
173 sendMail, serviceContext);
174 }
175 else {
176 String emailAddress =
177 "UserServiceTest." + RandomTestUtil.nextLong() + "@test.com";
178
179 return UserLocalServiceUtil.addUser(
180 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
181 autoPassword, password1, password2, autoScreenName, screenName,
182 emailAddress, facebookId, openId, locale, firstName, middleName,
183 lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
184 birthdayYear, jobTitle, groupIds, organizationIds, roleIds,
185 userGroupIds, sendMail, serviceContext);
186 }
187 }
188
189 public static User addUser(long groupId, Locale locale) throws Exception {
190 return addUser(
191 RandomTestUtil.randomString(), false, locale,
192 RandomTestUtil.randomString(), RandomTestUtil.randomString(),
193 new long[] {groupId});
194 }
195
196 public static User addUser(
197 long companyId, long userId, String screenName,
198 boolean autoScreenName, Locale locale, String firstName,
199 String lastName, long[] groupIds, ServiceContext serviceContext)
200 throws Exception {
201
202 User user = UserLocalServiceUtil.fetchUserByScreenName(
203 companyId, screenName);
204
205 if (user != null) {
206 return user;
207 }
208
209 boolean autoPassword = true;
210 String password1 = StringPool.BLANK;
211 String password2 = StringPool.BLANK;
212 String emailAddress =
213 RandomTestUtil.randomString() + RandomTestUtil.nextLong() +
214 "@liferay.com";
215 long facebookId = 0;
216 String openId = StringPool.BLANK;
217 String middleName = StringPool.BLANK;
218 int prefixId = 0;
219 int suffixId = 0;
220 boolean male = true;
221 int birthdayMonth = Calendar.JANUARY;
222 int birthdayDay = 1;
223 int birthdayYear = 1970;
224 String jobTitle = StringPool.BLANK;
225 long[] organizationIds = null;
226 long[] roleIds = null;
227 long[] userGroupIds = null;
228 boolean sendMail = false;
229
230 return UserLocalServiceUtil.addUser(
231 userId, companyId, autoPassword, password1, password2,
232 autoScreenName, screenName, emailAddress, facebookId, openId,
233 locale, firstName, middleName, lastName, prefixId, suffixId, male,
234 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
235 organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
236 }
237
238 public static User addUser(
239 String screenName, boolean autoScreenName, Locale locale,
240 String firstName, String lastName, long[] groupIds)
241 throws Exception {
242
243 return addUser(
244 TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
245 screenName, autoScreenName, locale, firstName, lastName, groupIds,
246 ServiceContextTestUtil.getServiceContext());
247 }
248
249 public static User addUser(
250 String screenName, boolean autoScreenName, long[] groupIds)
251 throws Exception {
252
253 return addUser(
254 screenName, autoScreenName, "ServiceTestSuite", "ServiceTestSuite",
255 groupIds);
256 }
257
258 public static User addUser(
259 String screenName, boolean autoScreenName, String firstName,
260 String lastName, long[] groupIds)
261 throws Exception {
262
263 return addUser(
264 screenName, autoScreenName, LocaleUtil.getDefault(), firstName,
265 lastName, groupIds);
266 }
267
268 public static User addUser(String screenName, long groupId)
269 throws Exception {
270
271 if (Validator.isNull(screenName)) {
272 return addUser(null, true, new long[] {groupId});
273 }
274 else {
275 return addUser(screenName, false, new long[] {groupId});
276 }
277 }
278
279 public static User getAdminUser(long companyId) throws PortalException {
280 Role role = RoleLocalServiceUtil.getRole(
281 companyId, RoleConstants.ADMINISTRATOR);
282
283 List<User> users = UserLocalServiceUtil.getRoleUsers(
284 role.getRoleId(), 0, 1);
285
286 if (!users.isEmpty()) {
287 return users.get(0);
288 }
289
290 return null;
291 }
292
293 public static User updateUser(User user) throws Exception {
294 String oldPassword = StringPool.BLANK;
295 String newPassword1 = StringPool.BLANK;
296 String newPassword2 = StringPool.BLANK;
297 Boolean passwordReset = false;
298 String reminderQueryQuestion = StringPool.BLANK;
299 String reminderQueryAnswer = StringPool.BLANK;
300 String screenName = "TestUser" + RandomTestUtil.nextLong();
301 String emailAddress =
302 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
303 long facebookId = 0;
304 String openId = StringPool.BLANK;
305 String languageId = StringPool.BLANK;
306 String timeZoneId = StringPool.BLANK;
307 String greeting = StringPool.BLANK;
308 String comments = StringPool.BLANK;
309 String firstName = "UserServiceTest";
310 String middleName = StringPool.BLANK;
311 String lastName = "UserServiceTest";
312 int prefixId = 0;
313 int suffixId = 0;
314 boolean male = true;
315 int birthdayMonth = Calendar.JANUARY;
316 int birthdayDay = 1;
317 int birthdayYear = 1970;
318 String smsSn = StringPool.BLANK;
319 String aimSn = StringPool.BLANK;
320 String facebookSn = StringPool.BLANK;
321 String icqSn = StringPool.BLANK;
322 String jabberSn = StringPool.BLANK;
323 String msnSn = StringPool.BLANK;
324 String mySpaceSn = StringPool.BLANK;
325 String skypeSn = StringPool.BLANK;
326 String twitterSn = StringPool.BLANK;
327 String ymSn = StringPool.BLANK;
328 String jobTitle = StringPool.BLANK;
329 long[] groupIds = null;
330 long[] organizationIds = null;
331 long[] roleIds = null;
332 List<UserGroupRole> userGroupRoles = null;
333 long[] userGroupIds = null;
334
335 ServiceContext serviceContext = new ServiceContext();
336
337 return UserServiceUtil.updateUser(
338 user.getUserId(), oldPassword, newPassword1, newPassword2,
339 passwordReset, reminderQueryQuestion, reminderQueryAnswer,
340 screenName, emailAddress, facebookId, openId, languageId,
341 timeZoneId, greeting, comments, firstName, middleName, lastName,
342 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
343 smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
344 skypeSn, twitterSn, ymSn, jobTitle, groupIds, organizationIds,
345 roleIds, userGroupRoles, userGroupIds, serviceContext);
346 }
347
348 }