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.util.FriendlyURLNormalizerUtil;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.model.Address;
022 import com.liferay.portal.model.Company;
023 import com.liferay.portal.model.EmailAddress;
024 import com.liferay.portal.model.Group;
025 import com.liferay.portal.model.GroupConstants;
026 import com.liferay.portal.model.ListTypeConstants;
027 import com.liferay.portal.model.Organization;
028 import com.liferay.portal.model.OrganizationConstants;
029 import com.liferay.portal.model.Phone;
030 import com.liferay.portal.model.Role;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.model.UserGroup;
033 import com.liferay.portal.model.UserGroupRole;
034 import com.liferay.portal.model.Website;
035 import com.liferay.portal.service.CompanyLocalServiceUtil;
036 import com.liferay.portal.service.GroupServiceUtil;
037 import com.liferay.portal.service.OrganizationServiceUtil;
038 import com.liferay.portal.service.RoleServiceUtil;
039 import com.liferay.portal.service.ServiceContext;
040 import com.liferay.portal.service.UserGroupServiceUtil;
041 import com.liferay.portal.service.UserServiceUtil;
042 import com.liferay.portal.util.test.RandomTestUtil;
043 import com.liferay.portal.util.test.ServiceContextTestUtil;
044 import com.liferay.portal.util.test.TestPropsValues;
045 import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
046 import com.liferay.portlet.asset.model.AssetCategory;
047 import com.liferay.portlet.asset.model.AssetTag;
048 import com.liferay.portlet.asset.model.AssetVocabulary;
049 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
050 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
051 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
052 import com.liferay.portlet.expando.model.ExpandoBridge;
053 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
054
055 import java.io.Serializable;
056
057 import java.util.ArrayList;
058 import java.util.Calendar;
059 import java.util.HashMap;
060 import java.util.List;
061 import java.util.Locale;
062 import java.util.Map;
063
064
067 public class MembershipPolicyTestUtil {
068
069
070
071
072
073 public static Group addGroup() throws Exception {
074 String name = RandomTestUtil.randomString();
075 String friendlyURL =
076 StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
077
078 return GroupServiceUtil.addGroup(
079 GroupConstants.DEFAULT_PARENT_GROUP_ID,
080 GroupConstants.DEFAULT_LIVE_GROUP_ID, name, "This is a test group",
081 GroupConstants.TYPE_SITE_OPEN, true,
082 GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, true,
083 true, populateServiceContext(Group.class, true));
084 }
085
086 public static Organization addOrganization() throws Exception {
087 String name = RandomTestUtil.randomString();
088
089 return OrganizationServiceUtil.addOrganization(
090 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID, name,
091 OrganizationConstants.TYPE_REGULAR_ORGANIZATION, 0, 0,
092 ListTypeConstants.ORGANIZATION_STATUS_DEFAULT, StringPool.BLANK,
093 false, populateServiceContext(Organization.class, true));
094 }
095
096 public static Role addRole(int type) throws Exception {
097 String name = RandomTestUtil.randomString();
098
099 return RoleServiceUtil.addRole(
100 null, 0, name, RandomTestUtil.randomLocaleStringMap(),
101 RandomTestUtil.randomLocaleStringMap(), type,
102 RandomTestUtil.randomString(),
103 populateServiceContext(Role.class, false));
104 }
105
106 public static User addUser(
107 long[] organizationIds, long[] roleIds, long[] siteIds,
108 long[] userGroupIds)
109 throws Exception {
110
111 boolean autoPassword = true;
112 String password1 = StringPool.BLANK;
113 String password2 = StringPool.BLANK;
114 boolean autoScreenName = true;
115 String screenName = StringPool.BLANK;
116 String emailAddress =
117 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
118 long facebookId = 0;
119 String openId = StringPool.BLANK;
120 Locale locale = LocaleUtil.getDefault();
121 String firstName = "UserServiceTest";
122 String middleName = StringPool.BLANK;
123 String lastName = "UserServiceTest";
124 int prefixId = 0;
125 int suffixId = 0;
126 boolean male = true;
127 int birthdayMonth = Calendar.JANUARY;
128 int birthdayDay = 1;
129 int birthdayYear = 1970;
130 String jobTitle = StringPool.BLANK;
131 boolean sendMail = false;
132
133 ServiceContext serviceContext = new ServiceContext();
134
135 return UserServiceUtil.addUser(
136 TestPropsValues.getCompanyId(), autoPassword, password1, password2,
137 autoScreenName, screenName, emailAddress, facebookId, openId,
138 locale, firstName, middleName, lastName, prefixId, suffixId, male,
139 birthdayMonth, birthdayDay, birthdayYear, jobTitle, siteIds,
140 organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
141 }
142
143 public static UserGroup addUserGroup() throws Exception {
144 String name = RandomTestUtil.randomString();
145 String description = RandomTestUtil.randomString(50);
146
147 return UserGroupServiceUtil.addUserGroup(
148 name, description, populateServiceContext(UserGroup.class, false));
149 }
150
151 public static void updateUser(
152 User user, long[] organizationIds, long[] roleIds, long[] siteIds,
153 long[] userGroupIds, List<UserGroupRole> userGroupRoles)
154 throws Exception {
155
156 long userId = user.getUserId();
157 String oldPassword = user.getPassword();
158 String newPassword1 = RandomTestUtil.randomString();
159 String newPassword2 = newPassword1;
160 boolean passwordReset = true;
161 String reminderQueryQuestion = RandomTestUtil.randomString();
162 String reminderQueryAnswer = RandomTestUtil.randomString();
163
164 String screenName = RandomTestUtil.randomString();
165 String emailAddress =
166 "UserServiceTest." + RandomTestUtil.nextLong() + "@liferay.com";
167 long facebookId = 0;
168 String openId = StringPool.BLANK;
169 String languageId = LocaleUtil.toLanguageId(Locale.getDefault());
170 String timeZoneId = RandomTestUtil.randomString();
171 String greeting = RandomTestUtil.randomString();
172 String comments = RandomTestUtil.randomString();
173 String firstName = "UserServiceTest";
174 String middleName = StringPool.BLANK;
175 String lastName = "UserServiceTest";
176 int prefixId = 0;
177 int suffixId = 0;
178 boolean male = true;
179 int birthdayMonth = Calendar.JANUARY;
180 int birthdayDay = 1;
181 int birthdayYear = 1970;
182 String jobTitle = StringPool.BLANK;
183 String smsSn =
184 "UserServiceTestSmsSn." + RandomTestUtil.nextInt() +
185 "@liferay.com";
186 String aimSn = RandomTestUtil.randomString();
187 String facebookSn = RandomTestUtil.randomString();
188 String icqSn = RandomTestUtil.randomString();
189 String jabberSn = RandomTestUtil.randomString();
190 String msnSn =
191 "UserServiceTestMsnSn." + RandomTestUtil.nextInt() +
192 "@liferay.com";
193 String mySpaceSn = RandomTestUtil.randomString();
194 String skypeSn = RandomTestUtil.randomString();
195 String twitterSn = RandomTestUtil.randomString();
196 String ymSn = RandomTestUtil.randomString();
197
198 List<Address> addresses = new ArrayList<Address>();
199 List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
200 List<Phone> phones = new ArrayList<Phone>();
201 List<Website> websites = new ArrayList<Website>();
202 List<AnnouncementsDelivery> announcementsDelivers =
203 new ArrayList<AnnouncementsDelivery>();
204
205 ServiceContext serviceContext = new ServiceContext();
206
207 UserServiceUtil.updateUser(
208 userId, oldPassword, newPassword1, newPassword2, passwordReset,
209 reminderQueryQuestion, reminderQueryAnswer, screenName,
210 emailAddress, facebookId, openId, false, null, languageId,
211 timeZoneId, greeting, comments, firstName, middleName, lastName,
212 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
213 smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
214 skypeSn, twitterSn, ymSn, jobTitle, siteIds, organizationIds,
215 roleIds, userGroupRoles, userGroupIds, addresses, emailAddresses,
216 phones, websites, announcementsDelivers, serviceContext);
217 }
218
219 protected static Map<String, Serializable> addExpandoMap(Class<?> clazz)
220 throws PortalException {
221
222 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
223 TestPropsValues.getCompanyId(), clazz.getName());
224
225 expandoBridge.addAttribute("key1", false);
226 expandoBridge.addAttribute("key2", false);
227 expandoBridge.addAttribute("key3", false);
228 expandoBridge.addAttribute("key4", false);
229
230 Map<String, Serializable> expandoMap =
231 new HashMap<String, Serializable>();
232
233 expandoMap.put("key1", "value1");
234 expandoMap.put("key2", "value2");
235 expandoMap.put("key3", "value3");
236 expandoMap.put("key4", "value4");
237
238 return expandoMap;
239 }
240
241 protected static ServiceContext populateServiceContext(
242 Class<?> clazz, boolean includeCategorization)
243 throws Exception {
244
245 ServiceContext serviceContext =
246 ServiceContextTestUtil.getServiceContext();
247
248 serviceContext.setAddGroupPermissions(true);
249 serviceContext.setAddGuestPermissions(true);
250
251 if (includeCategorization) {
252 AssetTag tag = AssetTagLocalServiceUtil.addTag(
253 TestPropsValues.getUserId(), RandomTestUtil.randomString(),
254 null, new ServiceContext());
255
256 serviceContext.setAssetTagNames(new String[] {tag.getName()});
257
258 Company company = CompanyLocalServiceUtil.getCompany(
259 TestPropsValues.getCompanyId());
260
261 serviceContext.setScopeGroupId(company.getGroupId());
262
263 AssetVocabulary vocabulary =
264 AssetVocabularyLocalServiceUtil.addVocabulary(
265 TestPropsValues.getUserId(), RandomTestUtil.randomString(),
266 serviceContext);
267
268 AssetCategory category = AssetCategoryLocalServiceUtil.addCategory(
269 TestPropsValues.getUserId(), RandomTestUtil.randomString(),
270 vocabulary.getVocabularyId(), serviceContext);
271
272 serviceContext.setAssetCategoryIds(
273 new long[] {category.getCategoryId()});
274 }
275
276 serviceContext.setExpandoBridgeAttributes(addExpandoMap(clazz));
277
278 return serviceContext;
279 }
280
281 }