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() + "@liferay.com";
185 String aimSn = RandomTestUtil.randomString();
186 String facebookSn = RandomTestUtil.randomString();
187 String icqSn = RandomTestUtil.randomString();
188 String jabberSn = RandomTestUtil.randomString();
189 String msnSn =
190 "UserServiceTestMsnSn." + RandomTestUtil.nextInt() + "@liferay.com";
191 String mySpaceSn = RandomTestUtil.randomString();
192 String skypeSn = RandomTestUtil.randomString();
193 String twitterSn = RandomTestUtil.randomString();
194 String ymSn = RandomTestUtil.randomString();
195
196 List<Address> addresses = new ArrayList<Address>();
197 List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
198 List<Phone> phones = new ArrayList<Phone>();
199 List<Website> websites = new ArrayList<Website>();
200 List<AnnouncementsDelivery> announcementsDelivers =
201 new ArrayList<AnnouncementsDelivery>();
202
203 ServiceContext serviceContext = new ServiceContext();
204
205 UserServiceUtil.updateUser(
206 userId, oldPassword, newPassword1, newPassword2, passwordReset,
207 reminderQueryQuestion, reminderQueryAnswer, screenName,
208 emailAddress, facebookId, openId, false, null, languageId,
209 timeZoneId, greeting, comments, firstName, middleName, lastName,
210 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
211 smsSn, aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn,
212 skypeSn, twitterSn, ymSn, jobTitle, siteIds, organizationIds,
213 roleIds, userGroupRoles, userGroupIds, addresses, emailAddresses,
214 phones, websites, announcementsDelivers, serviceContext);
215 }
216
217 protected static Map<String, Serializable> addExpandoMap(Class<?> clazz)
218 throws PortalException {
219
220 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
221 TestPropsValues.getCompanyId(), clazz.getName());
222
223 expandoBridge.addAttribute("key1", false);
224 expandoBridge.addAttribute("key2", false);
225 expandoBridge.addAttribute("key3", false);
226 expandoBridge.addAttribute("key4", false);
227
228 Map<String, Serializable> expandoMap =
229 new HashMap<String, Serializable>();
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(), RandomTestUtil.randomString(),
252 null, 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(), RandomTestUtil.randomString(),
264 serviceContext);
265
266 AssetCategory category = AssetCategoryLocalServiceUtil.addCategory(
267 TestPropsValues.getUserId(), RandomTestUtil.randomString(),
268 vocabulary.getVocabularyId(), serviceContext);
269
270 serviceContext.setAssetCategoryIds(
271 new long[] {category.getCategoryId()});
272 }
273
274 serviceContext.setExpandoBridgeAttributes(addExpandoMap(clazz));
275
276 return serviceContext;
277 }
278
279 }