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