001
014
015 package com.liferay.portal.util.test;
016
017 import com.liferay.portal.exception.NoSuchLayoutException;
018 import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
019 import com.liferay.portal.kernel.test.randomizerbumpers.UniqueStringRandomizerBumper;
020 import com.liferay.portal.kernel.test.util.RandomTestUtil;
021 import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
022 import com.liferay.portal.kernel.test.util.TestPropsValues;
023 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
024 import com.liferay.portal.kernel.util.LocaleUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.UnicodeProperties;
027 import com.liferay.portal.model.CustomizedPages;
028 import com.liferay.portal.model.Group;
029 import com.liferay.portal.model.Layout;
030 import com.liferay.portal.model.LayoutConstants;
031 import com.liferay.portal.model.LayoutPrototype;
032 import com.liferay.portal.model.LayoutSetPrototype;
033 import com.liferay.portal.model.LayoutTemplate;
034 import com.liferay.portal.model.LayoutTypePortlet;
035 import com.liferay.portal.model.Portlet;
036 import com.liferay.portal.service.LayoutLocalServiceUtil;
037 import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
038 import com.liferay.portal.service.LayoutServiceUtil;
039 import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
040 import com.liferay.portal.service.ServiceContext;
041 import com.liferay.portal.test.randomizerbumpers.FriendlyURLRandomizerBumper;
042 import com.liferay.portlet.PortletPreferencesFactoryUtil;
043
044 import java.util.HashMap;
045 import java.util.List;
046 import java.util.Locale;
047 import java.util.Map;
048
049 import javax.portlet.PortletPreferences;
050
051
055 public class LayoutTestUtil {
056
057 public static Layout addLayout(Group group) throws Exception {
058 return addLayout(group.getGroupId());
059 }
060
061 public static Layout addLayout(Group group, boolean privateLayout)
062 throws Exception {
063
064 return addLayout(group.getGroupId(), privateLayout);
065 }
066
067 public static Layout addLayout(
068 Group group, boolean privateLayout, LayoutPrototype layoutPrototype,
069 boolean linkEnabled)
070 throws Exception {
071
072 return addLayout(
073 group.getGroupId(), privateLayout, layoutPrototype, linkEnabled);
074 }
075
076 public static Layout addLayout(Group group, long parentLayoutPlid)
077 throws Exception {
078
079 return addLayout(group.getGroupId(), parentLayoutPlid);
080 }
081
082 public static Layout addLayout(long groupId) throws Exception {
083 return addLayout(groupId, false);
084 }
085
086 public static Layout addLayout(long groupId, boolean privateLayout)
087 throws Exception {
088
089 return addLayout(groupId, privateLayout, null, false);
090 }
091
092 public static Layout addLayout(
093 long groupId, boolean privateLayout,
094 LayoutPrototype layoutPrototype, boolean linkEnabled)
095 throws Exception {
096
097 return addLayout(
098 groupId,
099 RandomTestUtil.randomString(
100 FriendlyURLRandomizerBumper.INSTANCE,
101 NumericStringRandomizerBumper.INSTANCE,
102 UniqueStringRandomizerBumper.INSTANCE),
103 privateLayout, layoutPrototype, linkEnabled);
104 }
105
106 public static Layout addLayout(
107 long groupId, boolean privateLayout, Map<Locale, String> nameMap,
108 Map<Locale, String> friendlyURLMap)
109 throws Exception {
110
111 ServiceContext serviceContext =
112 ServiceContextTestUtil.getServiceContext(groupId);
113
114 return LayoutLocalServiceUtil.addLayout(
115 serviceContext.getUserId(), groupId, privateLayout,
116 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, nameMap, nameMap,
117 new HashMap<Locale, String>(), new HashMap<Locale, String>(),
118 new HashMap<Locale, String>(), LayoutConstants.TYPE_PORTLET,
119 StringPool.BLANK, false, friendlyURLMap, serviceContext);
120 }
121
122 public static Layout addLayout(long groupId, long parentLayoutPlid)
123 throws Exception {
124
125 Layout layout = addLayout(groupId, false);
126
127 LayoutLocalServiceUtil.updateParentLayoutId(
128 layout.getPlid(), parentLayoutPlid);
129
130 return LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
131 }
132
133 public static Layout addLayout(
134 long groupId, String name, boolean privateLayout)
135 throws Exception {
136
137 return addLayout(groupId, name, privateLayout, null, false);
138 }
139
140 public static Layout addLayout(
141 long groupId, String name, boolean privateLayout,
142 LayoutPrototype layoutPrototype, boolean linkEnabled)
143 throws Exception {
144
145 String friendlyURL =
146 StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
147
148 Layout layout = null;
149
150 try {
151 layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
152 groupId, false, friendlyURL);
153
154 return layout;
155 }
156 catch (NoSuchLayoutException nsle) {
157 }
158
159 String description = "This is a test page.";
160
161 ServiceContext serviceContext =
162 ServiceContextTestUtil.getServiceContext();
163
164 if (layoutPrototype != null) {
165 serviceContext.setAttribute(
166 "layoutPrototypeLinkEnabled", linkEnabled);
167 serviceContext.setAttribute(
168 "layoutPrototypeUuid", layoutPrototype.getUuid());
169 }
170
171 return LayoutLocalServiceUtil.addLayout(
172 TestPropsValues.getUserId(), groupId, privateLayout,
173 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, null, description,
174 LayoutConstants.TYPE_PORTLET, false, friendlyURL, serviceContext);
175 }
176
177 public static Layout addLayout(
178 long groupId, String name, long parentLayoutPlid)
179 throws Exception {
180
181 Layout layout = addLayout(groupId, name, false);
182
183 LayoutLocalServiceUtil.updateParentLayoutId(
184 layout.getPlid(), parentLayoutPlid);
185
186 return LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
187 }
188
189 public static LayoutPrototype addLayoutPrototype(String name)
190 throws Exception {
191
192 HashMap<Locale, String> nameMap = new HashMap<>();
193
194 nameMap.put(LocaleUtil.getDefault(), name);
195
196 return LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
197 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
198 nameMap, (Map<Locale, String>)null, true,
199 ServiceContextTestUtil.getServiceContext());
200 }
201
202 public static LayoutSetPrototype addLayoutSetPrototype(String name)
203 throws Exception {
204
205 HashMap<Locale, String> nameMap = new HashMap<>();
206
207 nameMap.put(LocaleUtil.getDefault(), name);
208
209 return LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
210 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
211 nameMap, (Map<Locale, String>)null, true, true,
212 ServiceContextTestUtil.getServiceContext());
213 }
214
215 public static String addPortletToLayout(Layout layout, String portletId)
216 throws Exception {
217
218 Map<String, String[]> preferenceMap = null;
219
220 return addPortletToLayout(layout, portletId, preferenceMap);
221 }
222
223 public static String addPortletToLayout(
224 Layout layout, String portletId,
225 Map<String, String[]> preferenceMap)
226 throws Exception {
227
228 long userId = TestPropsValues.getUserId();
229
230 LayoutTypePortlet layoutTypePortlet =
231 (LayoutTypePortlet)layout.getLayoutType();
232
233 LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();
234
235 List<String> columns = layoutTemplate.getColumns();
236
237 String columnId = columns.get(0);
238
239 return addPortletToLayout(
240 userId, layout, portletId, columnId, preferenceMap);
241 }
242
243 public static String addPortletToLayout(
244 long userId, Layout layout, String portletId, String columnId,
245 Map<String, String[]> preferenceMap)
246 throws Exception {
247
248 LayoutTypePortlet layoutTypePortlet =
249 (LayoutTypePortlet)layout.getLayoutType();
250
251 String newPortletId = layoutTypePortlet.addPortletId(
252 userId, portletId, columnId, -1);
253
254 LayoutLocalServiceUtil.updateLayout(
255 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
256 layout.getTypeSettings());
257
258 if (preferenceMap == null) {
259 return newPortletId;
260 }
261
262 PortletPreferences portletPreferences = getPortletPreferences(
263 layout, newPortletId);
264
265 for (String key : preferenceMap.keySet()) {
266 portletPreferences.setValues(key, preferenceMap.get(key));
267 }
268
269 portletPreferences.store();
270
271 return newPortletId;
272 }
273
274 public static Layout addTypeLinkToLayoutLayout(
275 long groupId, long linkedToLayoutId)
276 throws Exception {
277
278 Layout layout = addLayout(groupId, false);
279
280 UnicodeProperties typeSettingsProperties =
281 layout.getTypeSettingsProperties();
282
283 typeSettingsProperties.setProperty(
284 "linkToLayoutId", String.valueOf(linkedToLayoutId));
285
286 layout.setType(LayoutConstants.TYPE_LINK_TO_LAYOUT);
287
288 LayoutLocalServiceUtil.updateLayout(layout);
289
290 return layout;
291 }
292
293 public static String getLayoutTemplateId(Layout layout) {
294 LayoutTypePortlet layoutTypePortlet =
295 (LayoutTypePortlet)layout.getLayoutType();
296
297 return layoutTypePortlet.getLayoutTemplateId();
298 }
299
300 public static PortletPreferences getPortletPreferences(
301 Layout layout, String portletId)
302 throws Exception {
303
304 return PortletPreferencesFactoryUtil.getPortletSetup(
305 layout, portletId, null);
306 }
307
308 public static PortletPreferences getPortletPreferences(
309 long plid, String portletId)
310 throws Exception {
311
312 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
313
314 return getPortletPreferences(layout, portletId);
315 }
316
317 public static List<Portlet> getPortlets(Layout layout) throws Exception {
318 LayoutTypePortlet layoutTypePortlet =
319 (LayoutTypePortlet)layout.getLayoutType();
320
321 return layoutTypePortlet.getPortlets();
322 }
323
324 public static boolean isLayoutColumnCustomizable(
325 Layout layout, String columnId) {
326
327 LayoutTypePortlet layoutTypePortlet =
328 (LayoutTypePortlet)layout.getLayoutType();
329
330 return layoutTypePortlet.isColumnCustomizable(columnId);
331 }
332
333 public static Layout updateLayoutColumnCustomizable(
334 Layout layout, String columnId, boolean customizable)
335 throws Exception {
336
337 LayoutTypePortlet layoutTypePortlet =
338 (LayoutTypePortlet)layout.getLayoutType();
339
340 layoutTypePortlet.setTypeSettingsProperty(
341 CustomizedPages.namespaceColumnId(columnId),
342 String.valueOf(customizable));
343
344 layoutTypePortlet.setUpdatePermission(customizable);
345
346 return LayoutServiceUtil.updateLayout(
347 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
348 layout.getTypeSettings());
349 }
350
351 public static Layout updateLayoutPortletPreference(
352 Layout layout, String portletId, String portletPreferenceName,
353 String portletPreferenceValue)
354 throws Exception {
355
356 PortletPreferences layoutPortletPreferences = getPortletPreferences(
357 layout, portletId);
358
359 layoutPortletPreferences.setValue(
360 portletPreferenceName, portletPreferenceValue);
361
362 layoutPortletPreferences.store();
363
364 return LayoutLocalServiceUtil.getLayout(layout.getPlid());
365 }
366
367 public static Layout updateLayoutPortletPreferences(
368 Layout layout, String portletId,
369 Map<String, String> portletPreferences)
370 throws Exception {
371
372 PortletPreferences layoutPortletPreferences = getPortletPreferences(
373 layout, portletId);
374
375 for (Map.Entry<String, String> entry : portletPreferences.entrySet()) {
376 layoutPortletPreferences.setValue(entry.getKey(), entry.getValue());
377 }
378
379 layoutPortletPreferences.store();
380
381 return LayoutLocalServiceUtil.getLayout(layout.getPlid());
382 }
383
384 public static Layout updateLayoutTemplateId(
385 Layout layout, String layoutTemplateId)
386 throws Exception {
387
388 LayoutTypePortlet layoutTypePortlet =
389 (LayoutTypePortlet)layout.getLayoutType();
390
391 layoutTypePortlet.setLayoutTemplateId(
392 TestPropsValues.getUserId(), layoutTemplateId);
393
394 return LayoutServiceUtil.updateLayout(
395 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
396 layout.getTypeSettings());
397 }
398
399 }