PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/apps/wiki/wiki-test-util/src/main/java/com/liferay/wiki/test/util/WikiTestUtil.java

http://github.com/liferay/liferay-portal
Java | 522 lines | 374 code | 131 blank | 17 comment | 11 complexity | 954fb65139ca8b7bac0627772f7e1403 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.wiki.test.util;
  15. import com.liferay.portal.kernel.service.ServiceContext;
  16. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  17. import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
  18. import com.liferay.portal.kernel.test.util.TestPropsValues;
  19. import com.liferay.portal.kernel.util.ArrayUtil;
  20. import com.liferay.portal.kernel.util.Constants;
  21. import com.liferay.portal.kernel.util.FileUtil;
  22. import com.liferay.portal.kernel.util.HashMapBuilder;
  23. import com.liferay.portal.kernel.util.MimeTypesUtil;
  24. import com.liferay.portal.kernel.util.Validator;
  25. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  26. import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
  27. import com.liferay.wiki.constants.WikiPageConstants;
  28. import com.liferay.wiki.model.WikiNode;
  29. import com.liferay.wiki.model.WikiPage;
  30. import com.liferay.wiki.service.WikiNodeLocalServiceUtil;
  31. import com.liferay.wiki.service.WikiPageLocalServiceUtil;
  32. import java.io.File;
  33. import java.io.Serializable;
  34. /**
  35. * @author Julio Camarero
  36. * @author Roberto Díaz
  37. */
  38. public class WikiTestUtil {
  39. public static WikiNode addDefaultNode(long groupId) throws Exception {
  40. WorkflowThreadLocal.setEnabled(true);
  41. ServiceContext serviceContext =
  42. ServiceContextTestUtil.getServiceContext(groupId);
  43. serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
  44. serviceContext = (ServiceContext)serviceContext.clone();
  45. return WikiNodeLocalServiceUtil.addDefaultNode(
  46. TestPropsValues.getUserId(), serviceContext);
  47. }
  48. public static WikiNode addNode(long groupId) throws Exception {
  49. return addNode(
  50. TestPropsValues.getUserId(), groupId, RandomTestUtil.randomString(),
  51. RandomTestUtil.randomString(50));
  52. }
  53. public static WikiNode addNode(
  54. long userId, long groupId, String name, String description)
  55. throws Exception {
  56. WorkflowThreadLocal.setEnabled(true);
  57. ServiceContext serviceContext =
  58. ServiceContextTestUtil.getServiceContext(groupId);
  59. serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
  60. serviceContext = (ServiceContext)serviceContext.clone();
  61. return WikiNodeLocalServiceUtil.addNode(
  62. userId, name, description, serviceContext);
  63. }
  64. public static WikiPage addPage(long groupId, long nodeId, boolean approved)
  65. throws Exception {
  66. return addPage(
  67. TestPropsValues.getUserId(), groupId, nodeId,
  68. RandomTestUtil.randomString(), approved);
  69. }
  70. public static WikiPage addPage(
  71. long userId, long groupId, long nodeId, String title,
  72. boolean approved)
  73. throws Exception {
  74. ServiceContext serviceContext =
  75. ServiceContextTestUtil.getServiceContext(groupId);
  76. serviceContext.setCommand(Constants.ADD);
  77. serviceContext.setLayoutFullURL("http://localhost");
  78. return addPage(
  79. userId, nodeId, title, "content", approved, serviceContext);
  80. }
  81. public static WikiPage addPage(
  82. long userId, long nodeId, String title, String content,
  83. boolean approved, ServiceContext serviceContext)
  84. throws Exception {
  85. boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
  86. try {
  87. WorkflowThreadLocal.setEnabled(true);
  88. serviceContext = (ServiceContext)serviceContext.clone();
  89. serviceContext.setWorkflowAction(
  90. WorkflowConstants.ACTION_SAVE_DRAFT);
  91. WikiPage page = WikiPageLocalServiceUtil.addPage(
  92. userId, nodeId, title, content, "Summary", false,
  93. serviceContext);
  94. if (approved) {
  95. page = updateStatus(page, serviceContext);
  96. }
  97. return page;
  98. }
  99. finally {
  100. WorkflowThreadLocal.setEnabled(workflowEnabled);
  101. }
  102. }
  103. public static WikiPage addPage(
  104. long userId, long nodeId, String title, String content,
  105. String parentTitle, boolean approved, ServiceContext serviceContext)
  106. throws Exception {
  107. boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
  108. try {
  109. WorkflowThreadLocal.setEnabled(true);
  110. serviceContext = (ServiceContext)serviceContext.clone();
  111. serviceContext.setCommand(Constants.ADD);
  112. serviceContext.setWorkflowAction(
  113. WorkflowConstants.ACTION_SAVE_DRAFT);
  114. WikiPage page = WikiPageLocalServiceUtil.addPage(
  115. userId, nodeId, title, WikiPageConstants.VERSION_DEFAULT,
  116. content, "Summary", false, "creole", true, parentTitle, null,
  117. serviceContext);
  118. if (approved) {
  119. page = updateStatus(page, serviceContext);
  120. }
  121. return page;
  122. }
  123. finally {
  124. WorkflowThreadLocal.setEnabled(workflowEnabled);
  125. }
  126. }
  127. public static WikiPage[] addPageWithChangedParentPage(
  128. long groupId, long nodeId)
  129. throws Exception {
  130. WikiPage initialParentPage = addPage(
  131. TestPropsValues.getUserId(), groupId, nodeId,
  132. RandomTestUtil.randomString(), true);
  133. ServiceContext serviceContext =
  134. ServiceContextTestUtil.getServiceContext(groupId);
  135. WikiPage childPage = addPage(
  136. TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
  137. RandomTestUtil.randomString(), initialParentPage.getTitle(), true,
  138. serviceContext);
  139. WikiPage finalParentPage = addPage(
  140. TestPropsValues.getUserId(), groupId, nodeId,
  141. RandomTestUtil.randomString(), true);
  142. WikiPageLocalServiceUtil.changeParent(
  143. TestPropsValues.getUserId(), nodeId, childPage.getTitle(),
  144. finalParentPage.getTitle(), serviceContext);
  145. childPage = WikiPageLocalServiceUtil.getPage(
  146. nodeId, childPage.getTitle());
  147. initialParentPage = WikiPageLocalServiceUtil.getPageByPageId(
  148. initialParentPage.getPageId());
  149. finalParentPage = WikiPageLocalServiceUtil.getPageByPageId(
  150. finalParentPage.getPageId());
  151. return new WikiPage[] {childPage, finalParentPage, initialParentPage};
  152. }
  153. public static WikiPage[] addPageWithChildPageAndRedirectPage(
  154. long groupId, long nodeId)
  155. throws Exception {
  156. addPage(TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
  157. ServiceContext serviceContext =
  158. ServiceContextTestUtil.getServiceContext(groupId);
  159. addPage(
  160. TestPropsValues.getUserId(), nodeId, "TestChildPage",
  161. RandomTestUtil.randomString(), "TestPage", true, serviceContext);
  162. WikiPageLocalServiceUtil.renamePage(
  163. TestPropsValues.getUserId(), nodeId, "TestPage", "B",
  164. serviceContext);
  165. WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
  166. WikiPage childPage = WikiPageLocalServiceUtil.getPage(
  167. nodeId, "TestChildPage");
  168. WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(
  169. nodeId, "TestPage");
  170. return new WikiPage[] {page, childPage, redirectPage};
  171. }
  172. public static WikiPage[] addRenamedParentPageWithChildPageAndGrandchildPage(
  173. long groupId, long nodeId)
  174. throws Exception {
  175. ServiceContext serviceContext =
  176. ServiceContextTestUtil.getServiceContext(groupId);
  177. addPage(TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
  178. WikiPage childPage = addPage(
  179. TestPropsValues.getUserId(), nodeId, "TestChildPage",
  180. RandomTestUtil.randomString(), "TestPage", true, serviceContext);
  181. WikiPage grandchildPage = addPage(
  182. TestPropsValues.getUserId(), nodeId, "TestGrandchildPage",
  183. RandomTestUtil.randomString(), "TestChildPage", true,
  184. serviceContext);
  185. WikiPageLocalServiceUtil.renamePage(
  186. TestPropsValues.getUserId(), nodeId, "TestPage", "B",
  187. serviceContext);
  188. WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
  189. WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(
  190. nodeId, "TestPage");
  191. childPage = WikiPageLocalServiceUtil.getPageByPageId(
  192. childPage.getPageId());
  193. grandchildPage = WikiPageLocalServiceUtil.getPageByPageId(
  194. grandchildPage.getPageId());
  195. return new WikiPage[] {page, redirectPage, childPage, grandchildPage};
  196. }
  197. public static WikiPage[] addRenamedTrashedPage(
  198. long groupId, long nodeId, boolean explicitlyRemoveRedirectPage)
  199. throws Exception {
  200. addPage(TestPropsValues.getUserId(), groupId, nodeId, "A", true);
  201. WikiPageLocalServiceUtil.renamePage(
  202. TestPropsValues.getUserId(), nodeId, "A", "B",
  203. ServiceContextTestUtil.getServiceContext(groupId));
  204. WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
  205. WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(nodeId, "A");
  206. if (explicitlyRemoveRedirectPage) {
  207. WikiPageLocalServiceUtil.movePageToTrash(
  208. TestPropsValues.getUserId(), nodeId, "A");
  209. }
  210. WikiPageLocalServiceUtil.movePageToTrash(
  211. TestPropsValues.getUserId(), nodeId, "B");
  212. page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
  213. redirectPage = WikiPageLocalServiceUtil.getPageByPageId(
  214. redirectPage.getPageId());
  215. return new WikiPage[] {page, redirectPage};
  216. }
  217. public static WikiPage[] addRenamedTrashedParentPage(
  218. long groupId, long nodeId, boolean explicitlyRemoveChildPage,
  219. boolean explicitlyRemoveRedirectPage)
  220. throws Exception {
  221. ServiceContext serviceContext =
  222. ServiceContextTestUtil.getServiceContext(groupId);
  223. addPage(TestPropsValues.getUserId(), groupId, nodeId, "A", true);
  224. WikiPageLocalServiceUtil.renamePage(
  225. TestPropsValues.getUserId(), nodeId, "A", "B", serviceContext);
  226. WikiPage page = WikiPageLocalServiceUtil.getPage(nodeId, "B");
  227. WikiPage redirectPage = WikiPageLocalServiceUtil.getPage(nodeId, "A");
  228. WikiPage childPage = addPage(
  229. TestPropsValues.getUserId(), nodeId, "TestChildPage",
  230. RandomTestUtil.randomString(), "B", true, serviceContext);
  231. if (explicitlyRemoveChildPage) {
  232. WikiPageLocalServiceUtil.movePageToTrash(
  233. TestPropsValues.getUserId(), nodeId, "TestChildPage");
  234. }
  235. if (explicitlyRemoveRedirectPage) {
  236. WikiPageLocalServiceUtil.movePageToTrash(
  237. TestPropsValues.getUserId(), nodeId, "A");
  238. }
  239. WikiPageLocalServiceUtil.movePageToTrash(
  240. TestPropsValues.getUserId(), nodeId, "B");
  241. page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
  242. childPage = WikiPageLocalServiceUtil.getPageByPageId(
  243. childPage.getPageId());
  244. redirectPage = WikiPageLocalServiceUtil.getPageByPageId(
  245. redirectPage.getPageId());
  246. return new WikiPage[] {page, childPage, redirectPage};
  247. }
  248. public static WikiPage[] addTrashedPageWithChildPage(
  249. long groupId, long nodeId, boolean explicitlyRemoveChildPage)
  250. throws Exception {
  251. WikiPage page = addPage(
  252. TestPropsValues.getUserId(), groupId, nodeId, "TestPage", true);
  253. WikiPage childPage = addPage(
  254. TestPropsValues.getUserId(), nodeId, "TestChildPage",
  255. RandomTestUtil.randomString(), "TestPage", true,
  256. ServiceContextTestUtil.getServiceContext(groupId));
  257. if (explicitlyRemoveChildPage) {
  258. WikiPageLocalServiceUtil.movePageToTrash(
  259. TestPropsValues.getUserId(), childPage);
  260. }
  261. WikiPageLocalServiceUtil.movePageToTrash(
  262. TestPropsValues.getUserId(), page);
  263. page = WikiPageLocalServiceUtil.getPageByPageId(page.getPageId());
  264. childPage = WikiPageLocalServiceUtil.getPageByPageId(
  265. childPage.getPageId());
  266. return new WikiPage[] {page, childPage};
  267. }
  268. public static WikiPage[] addTrashedParentPageWithChildPageAndGrandchildPage(
  269. long groupId, long nodeId, boolean explicitMoveChildToTrash,
  270. boolean explicitMoveParentToTrash)
  271. throws Exception {
  272. WikiPage parentPage = addPage(
  273. TestPropsValues.getUserId(), groupId, nodeId,
  274. RandomTestUtil.randomString(), true);
  275. ServiceContext serviceContext =
  276. ServiceContextTestUtil.getServiceContext(groupId);
  277. WikiPage childPage = addPage(
  278. TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
  279. RandomTestUtil.randomString(), parentPage.getTitle(), true,
  280. serviceContext);
  281. WikiPage grandchildPage = addPage(
  282. TestPropsValues.getUserId(), nodeId, RandomTestUtil.randomString(),
  283. RandomTestUtil.randomString(), childPage.getTitle(), true,
  284. serviceContext);
  285. if (explicitMoveChildToTrash) {
  286. WikiPageLocalServiceUtil.movePageToTrash(
  287. TestPropsValues.getUserId(), childPage);
  288. }
  289. if (explicitMoveParentToTrash) {
  290. WikiPageLocalServiceUtil.movePageToTrash(
  291. TestPropsValues.getUserId(), parentPage);
  292. }
  293. parentPage = WikiPageLocalServiceUtil.getPageByPageId(
  294. parentPage.getPageId());
  295. childPage = WikiPageLocalServiceUtil.getPageByPageId(
  296. childPage.getPageId());
  297. grandchildPage = WikiPageLocalServiceUtil.getPageByPageId(
  298. grandchildPage.getPageId());
  299. return new WikiPage[] {parentPage, childPage, grandchildPage};
  300. }
  301. public static File addWikiAttachment(
  302. long userId, long nodeId, String title, Class<?> clazz)
  303. throws Exception {
  304. String fileName = RandomTestUtil.randomString() + ".docx";
  305. return addWikiAttachment(userId, nodeId, title, fileName, clazz);
  306. }
  307. public static File addWikiAttachment(
  308. long userId, long nodeId, String title, String fileName,
  309. Class<?> clazz)
  310. throws Exception {
  311. byte[] fileBytes = FileUtil.getBytes(
  312. clazz, "dependencies/OSX_Test.docx");
  313. File file = null;
  314. if (ArrayUtil.isNotEmpty(fileBytes)) {
  315. file = FileUtil.createTempFile(fileBytes);
  316. }
  317. String mimeType = MimeTypesUtil.getExtensionContentType("docx");
  318. WikiPageLocalServiceUtil.addPageAttachment(
  319. userId, nodeId, title, fileName, file, mimeType);
  320. return file;
  321. }
  322. public static WikiPage copyPage(
  323. WikiPage page, boolean approved, ServiceContext serviceContext)
  324. throws Exception {
  325. WikiPage copyPage = addPage(
  326. page.getUserId(), page.getNodeId(), RandomTestUtil.randomString(),
  327. page.getContent(), approved, serviceContext);
  328. WikiPageLocalServiceUtil.copyPageAttachments(
  329. page.getUserId(), page.getNodeId(), page.getTitle(),
  330. copyPage.getNodeId(), copyPage.getTitle());
  331. return copyPage;
  332. }
  333. public static void populateNotificationsServiceContext(
  334. ServiceContext serviceContext, String command)
  335. throws Exception {
  336. serviceContext.setAttribute("entryURL", "http://localhost");
  337. if (Validator.isNotNull(command)) {
  338. serviceContext.setCommand(command);
  339. }
  340. serviceContext.setLayoutFullURL("http://localhost");
  341. }
  342. public static WikiPage updatePage(WikiPage page) throws Exception {
  343. ServiceContext serviceContext =
  344. ServiceContextTestUtil.getServiceContext(page.getGroupId());
  345. serviceContext.setCommand(Constants.UPDATE);
  346. serviceContext.setLayoutFullURL("http://localhost");
  347. return updatePage(
  348. page, page.getUserId(), page.getTitle(),
  349. RandomTestUtil.randomString(50), true, serviceContext);
  350. }
  351. public static WikiPage updatePage(
  352. WikiPage page, long userId, String content,
  353. ServiceContext serviceContext)
  354. throws Exception {
  355. return updatePage(
  356. page, userId, page.getTitle(), content, true, serviceContext);
  357. }
  358. public static WikiPage updatePage(
  359. WikiPage page, long userId, String title, String content,
  360. boolean approved, ServiceContext serviceContext)
  361. throws Exception {
  362. boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
  363. try {
  364. WorkflowThreadLocal.setEnabled(true);
  365. serviceContext = (ServiceContext)serviceContext.clone();
  366. serviceContext.setWorkflowAction(
  367. WorkflowConstants.ACTION_SAVE_DRAFT);
  368. page = WikiPageLocalServiceUtil.updatePage(
  369. userId, page.getNodeId(), title, page.getVersion(), content,
  370. page.getSummary(), false, page.getFormat(),
  371. page.getParentTitle(), page.getRedirectTitle(), serviceContext);
  372. if (approved) {
  373. page = updateStatus(page, serviceContext);
  374. }
  375. return page;
  376. }
  377. finally {
  378. WorkflowThreadLocal.setEnabled(workflowEnabled);
  379. }
  380. }
  381. protected static WikiPage updateStatus(
  382. WikiPage page, ServiceContext serviceContext)
  383. throws Exception {
  384. return WikiPageLocalServiceUtil.updateStatus(
  385. page.getUserId(), page, WorkflowConstants.STATUS_APPROVED,
  386. serviceContext,
  387. HashMapBuilder.<String, Serializable>put(
  388. WorkflowConstants.CONTEXT_URL, "http://localhost"
  389. ).build());
  390. }
  391. }