001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
043     * @author Alberto Chaparro
044     * @author Manuel de la Pe??a
045     * @author Sampsa Sohlman
046     */
047    public class UserTestUtil {
048    
049            public static User addCompanyAdminUser(Company company) throws Exception {
050                    User user = addUser(company);
051    
052                    Role role = RoleLocalServiceUtil.getRole(
053                            company.getCompanyId(), RoleConstants.ADMINISTRATOR);
054    
055                    UserLocalServiceUtil.addRoleUser(role.getRoleId(), user);
056    
057                    return user;
058            }
059    
060            public static User addGroupAdminUser(Group group) throws Exception {
061                    return UserTestUtil.addGroupUser(
062                            group, RoleConstants.SITE_ADMINISTRATOR);
063            }
064    
065            public static User addGroupOwnerUser(Group group) throws Exception {
066                    return UserTestUtil.addGroupUser(group, RoleConstants.SITE_OWNER);
067            }
068    
069            public static User addGroupUser(Group group, String roleName)
070                    throws Exception {
071    
072                    User groupUser = addUser(group.getGroupId());
073    
074                    Role role = RoleLocalServiceUtil.getRole(
075                            TestPropsValues.getCompanyId(), roleName);
076    
077                    long[] userIds = {groupUser.getUserId()};
078    
079                    UserGroupRoleLocalServiceUtil.addUserGroupRoles(
080                            userIds, group.getGroupId(), role.getRoleId());
081    
082                    return groupUser;
083            }
084    
085            public static User addOmniAdminUser() throws Exception {
086                    Company company = CompanyLocalServiceUtil.getCompanyByMx(
087                            PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
088    
089                    return addCompanyAdminUser(company);
090            }
091    
092            public static User addOrganizationAdminUser(Organization organization)
093                    throws Exception {
094    
095                    return UserTestUtil.addOrganizationUser(
096                            organization, RoleConstants.ORGANIZATION_ADMINISTRATOR);
097            }
098    
099            public static User addOrganizationOwnerUser(Organization organization)
100                    throws Exception {
101    
102                    return UserTestUtil.addOrganizationUser(
103                            organization, RoleConstants.ORGANIZATION_OWNER);
104            }
105    
106            public static User addOrganizationUser(
107                            Organization organization, String roleName)
108                    throws Exception {
109    
110                    User organizationUser = addUser(organization.getGroupId());
111    
112                    UserLocalServiceUtil.addOrganizationUser(
113                            organization.getOrganizationId(), organizationUser.getUserId());
114    
115                    addUserGroupRole(
116                            organizationUser.getUserId(), organization.getGroupId(), roleName);
117    
118                    return organizationUser;
119            }
120    
121            public static User addUser() throws Exception {
122                    return addUser(
123                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
124                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
125                            LocaleUtil.getDefault(), RandomTestUtil.randomString(),
126                            RandomTestUtil.randomString(),
127                            new long[] {TestPropsValues.getGroupId()},
128                            ServiceContextTestUtil.getServiceContext());
129            }
130    
131            public static User addUser(boolean secure) throws Exception {
132                    boolean autoPassword = true;
133                    String password1 = StringPool.BLANK;
134                    String password2 = StringPool.BLANK;
135                    boolean autoScreenName = true;
136                    String screenName = StringPool.BLANK;
137                    long facebookId = 0;
138                    String openId = StringPool.BLANK;
139                    Locale locale = LocaleUtil.getDefault();
140                    String firstName = "UserServiceTest";
141                    String middleName = StringPool.BLANK;
142                    String lastName = "UserServiceTest";
143                    long prefixId = 0;
144                    long suffixId = 0;
145                    boolean male = true;
146                    int birthdayMonth = Calendar.JANUARY;
147                    int birthdayDay = 1;
148                    int birthdayYear = 1970;
149                    String jobTitle = StringPool.BLANK;
150                    long[] groupIds = null;
151                    long[] organizationIds = null;
152                    long[] roleIds = null;
153                    long[] userGroupIds = null;
154                    boolean sendMail = false;
155    
156                    ServiceContext serviceContext = new ServiceContext();
157    
158                    if (secure) {
159                            String emailAddress =
160                                    "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
161    
162                            return UserServiceUtil.addUser(
163                                    TestPropsValues.getCompanyId(), autoPassword, password1,
164                                    password2, autoScreenName, screenName, emailAddress, facebookId,
165                                    openId, locale, firstName, middleName, lastName, prefixId,
166                                    suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
167                                    jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
168                                    sendMail, serviceContext);
169                    }
170                    else {
171                            String emailAddress =
172                                    "UserServiceTest." + RandomTestUtil.nextLong() + "@test.com";
173    
174                            return UserLocalServiceUtil.addUser(
175                                    TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
176                                    autoPassword, password1, password2, autoScreenName, screenName,
177                                    emailAddress, facebookId, openId, locale, firstName, middleName,
178                                    lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
179                                    birthdayYear, jobTitle, groupIds, organizationIds, roleIds,
180                                    userGroupIds, sendMail, serviceContext);
181                    }
182            }
183    
184            public static User addUser(Company company) throws Exception {
185                    return addUser(
186                            company.getCompanyId(), TestPropsValues.getUserId(),
187                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
188                            LocaleUtil.getDefault(), RandomTestUtil.randomString(),
189                            RandomTestUtil.randomString(),
190                            new long[] {TestPropsValues.getGroupId()},
191                            ServiceContextTestUtil.getServiceContext());
192            }
193    
194            public static User addUser(long... groupIds) throws Exception {
195                    return addUser(
196                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
197                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
198                            LocaleUtil.getDefault(), RandomTestUtil.randomString(),
199                            RandomTestUtil.randomString(), groupIds,
200                            ServiceContextTestUtil.getServiceContext());
201            }
202    
203            public static User addUser(long groupId, Locale locale) throws Exception {
204                    return addUser(
205                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
206                            RandomTestUtil.randomString(NumericStringRandomizerBumper.INSTANCE),
207                            locale, RandomTestUtil.randomString(),
208                            RandomTestUtil.randomString(), new long[] {groupId},
209                            ServiceContextTestUtil.getServiceContext());
210            }
211    
212            public static User addUser(
213                            long companyId, long userId, String screenName, Locale locale,
214                            String firstName, String lastName, long[] groupIds,
215                            ServiceContext serviceContext)
216                    throws Exception {
217    
218                    User user = UserLocalServiceUtil.fetchUserByScreenName(
219                            companyId, screenName);
220    
221                    if (user != null) {
222                            return user;
223                    }
224    
225                    boolean autoPassword = true;
226                    String password1 = StringPool.BLANK;
227                    String password2 = StringPool.BLANK;
228                    String emailAddress =
229                            RandomTestUtil.randomString() + RandomTestUtil.nextLong() +
230                                    "@liferay.com";
231                    long facebookId = 0;
232                    String openId = StringPool.BLANK;
233                    String middleName = StringPool.BLANK;
234                    long prefixId = 0;
235                    long suffixId = 0;
236                    boolean male = true;
237                    int birthdayMonth = Calendar.JANUARY;
238                    int birthdayDay = 1;
239                    int birthdayYear = 1970;
240                    String jobTitle = StringPool.BLANK;
241                    long[] organizationIds = null;
242                    long[] roleIds = null;
243                    long[] userGroupIds = null;
244                    boolean sendMail = false;
245    
246                    return UserLocalServiceUtil.addUser(
247                            userId, companyId, autoPassword, password1, password2,
248                            Validator.isNull(screenName), screenName, emailAddress, facebookId,
249                            openId, locale, firstName, middleName, lastName, prefixId, suffixId,
250                            male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
251                            organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
252            }
253    
254            public static User addUser(
255                            String screenName, Locale locale, String firstName, String lastName,
256                            long[] groupIds)
257                    throws Exception {
258    
259                    return addUser(
260                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
261                            screenName, locale, firstName, lastName, groupIds,
262                            ServiceContextTestUtil.getServiceContext());
263            }
264    
265            public static User addUser(String screenName, long... groupIds)
266                    throws Exception {
267    
268                    return addUser(
269                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
270                            screenName, LocaleUtil.getDefault(), RandomTestUtil.randomString(),
271                            RandomTestUtil.randomString(), groupIds,
272                            ServiceContextTestUtil.getServiceContext());
273            }
274    
275            public static void addUserGroupRole(
276                            long userId, long groupId, String roleName)
277                    throws Exception {
278    
279                    Role role = RoleLocalServiceUtil.getRole(
280                            TestPropsValues.getCompanyId(), roleName);
281    
282                    UserGroupRoleLocalServiceUtil.addUserGroupRoles(
283                            new long[] {userId}, groupId, role.getRoleId());
284            }
285    
286            public static User getAdminUser(long companyId) throws PortalException {
287                    Role role = RoleLocalServiceUtil.getRole(
288                            companyId, RoleConstants.ADMINISTRATOR);
289    
290                    List<User> users = UserLocalServiceUtil.getRoleUsers(
291                            role.getRoleId(), 0, 1);
292    
293                    if (!users.isEmpty()) {
294                            return users.get(0);
295                    }
296    
297                    return null;
298            }
299    
300            public static User updateUser(User user) throws Exception {
301                    ServiceContext serviceContext = new ServiceContext();
302    
303                    return updateUser(user, serviceContext);
304            }
305    
306            public static User updateUser(User user, ServiceContext serviceContext)
307                    throws Exception {
308    
309                    String oldPassword = StringPool.BLANK;
310                    String newPassword1 = StringPool.BLANK;
311                    String newPassword2 = StringPool.BLANK;
312                    Boolean passwordReset = false;
313                    String reminderQueryQuestion = StringPool.BLANK;
314                    String reminderQueryAnswer = StringPool.BLANK;
315                    String screenName = "TestUser" + RandomTestUtil.nextLong();
316                    String emailAddress =
317                            "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
318                    long facebookId = 0;
319                    String openId = StringPool.BLANK;
320                    String languageId = StringPool.BLANK;
321                    String timeZoneId = StringPool.BLANK;
322                    String greeting = StringPool.BLANK;
323                    String comments = StringPool.BLANK;
324                    String firstName = "UserServiceTest";
325                    String middleName = StringPool.BLANK;
326                    String lastName = "UserServiceTest";
327                    long prefixId = 0;
328                    long suffixId = 0;
329                    boolean male = true;
330                    int birthdayMonth = Calendar.JANUARY;
331                    int birthdayDay = 1;
332                    int birthdayYear = 1970;
333                    String smsSn = StringPool.BLANK;
334                    String facebookSn = StringPool.BLANK;
335                    String jabberSn = StringPool.BLANK;
336                    String skypeSn = StringPool.BLANK;
337                    String twitterSn = StringPool.BLANK;
338                    String jobTitle = StringPool.BLANK;
339                    long[] groupIds = null;
340                    long[] organizationIds = null;
341                    long[] roleIds = null;
342                    List<UserGroupRole> userGroupRoles = null;
343                    long[] userGroupIds = null;
344    
345                    return UserServiceUtil.updateUser(
346                            user.getUserId(), oldPassword, newPassword1, newPassword2,
347                            passwordReset, reminderQueryQuestion, reminderQueryAnswer,
348                            screenName, emailAddress, facebookId, openId, languageId,
349                            timeZoneId, greeting, comments, firstName, middleName, lastName,
350                            prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
351                            smsSn, facebookSn, jabberSn, skypeSn, twitterSn, jobTitle, groupIds,
352                            organizationIds, roleIds, userGroupRoles, userGroupIds,
353                            serviceContext);
354            }
355    
356    }