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 facebookSn = RandomTestUtil.randomString();
194 String jabberSn = RandomTestUtil.randomString();
195 String skypeSn = RandomTestUtil.randomString();
196 String twitterSn = RandomTestUtil.randomString();
197
198 List<Address> addresses = new ArrayList<>();
199 List<EmailAddress> emailAddresses = new ArrayList<>();
200 List<Phone> phones = new ArrayList<>();
201 List<Website> websites = new ArrayList<>();
202 List<AnnouncementsDelivery> announcementsDelivers = new ArrayList<>();
203
204 ServiceContext serviceContext = new ServiceContext();
205
206 UserServiceUtil.updateUser(
207 userId, oldPassword, newPassword1, newPassword2, passwordReset,
208 reminderQueryQuestion, reminderQueryAnswer, screenName,
209 emailAddress, facebookId, openId, false, null, languageId,
210 timeZoneId, greeting, comments, firstName, middleName, lastName,
211 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
212 smsSn, facebookSn, jabberSn, skypeSn, twitterSn, jobTitle, siteIds,
213 organizationIds, roleIds, userGroupRoles, userGroupIds, addresses,
214 emailAddresses, phones, websites, announcementsDelivers,
215 serviceContext);
216 }
217
218 protected static Map<String, Serializable> addExpandoMap(Class<?> clazz)
219 throws PortalException {
220
221 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
222 TestPropsValues.getCompanyId(), clazz.getName());
223
224 expandoBridge.addAttribute("key1", false);
225 expandoBridge.addAttribute("key2", false);
226 expandoBridge.addAttribute("key3", false);
227 expandoBridge.addAttribute("key4", false);
228
229 Map<String, Serializable> expandoMap = new HashMap<>();
230
231 expandoMap.put("key1", "value1");
232 expandoMap.put("key2", "value2");
233 expandoMap.put("key3", "value3");
234 expandoMap.put("key4", "value4");
235
236 return expandoMap;
237 }
238
239 protected static ServiceContext populateServiceContext(
240 Class<?> clazz, boolean includeCategorization)
241 throws Exception {
242
243 ServiceContext serviceContext =
244 ServiceContextTestUtil.getServiceContext();
245
246 serviceContext.setAddGroupPermissions(true);
247 serviceContext.setAddGuestPermissions(true);
248
249 if (includeCategorization) {
250 AssetTag tag = AssetTagLocalServiceUtil.addTag(
251 TestPropsValues.getUserId(), TestPropsValues.getGroupId(),
252 RandomTestUtil.randomString(), new ServiceContext());
253
254 serviceContext.setAssetTagNames(new String[] {tag.getName()});
255
256 Company company = CompanyLocalServiceUtil.getCompany(
257 TestPropsValues.getCompanyId());
258
259 serviceContext.setScopeGroupId(company.getGroupId());
260
261 AssetVocabulary vocabulary =
262 AssetVocabularyLocalServiceUtil.addVocabulary(
263 TestPropsValues.getUserId(), company.getGroupId(),
264 RandomTestUtil.randomString(), serviceContext);
265
266 AssetCategory category = AssetCategoryLocalServiceUtil.addCategory(
267 TestPropsValues.getUserId(), company.getGroupId(),
268 RandomTestUtil.randomString(), vocabulary.getVocabularyId(),
269 serviceContext);
270
271 serviceContext.setAssetCategoryIds(
272 new long[] {category.getCategoryId()});
273 }
274
275 serviceContext.setExpandoBridgeAttributes(addExpandoMap(clazz));
276
277 return serviceContext;
278 }
279
280 }