page.test.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /* eslint-disable no-unused-vars */
  2. import { GroupType } from '@growi/core';
  3. import { advanceTo } from 'jest-date-mock';
  4. import { PageSingleDeleteCompConfigValue, PageRecursiveDeleteCompConfigValue } from '~/interfaces/page-delete-config';
  5. import PageTagRelation from '~/server/models/page-tag-relation';
  6. import Tag from '~/server/models/tag';
  7. import UserGroup from '~/server/models/user-group';
  8. import UserGroupRelation from '~/server/models/user-group-relation';
  9. import { generalXssFilter } from '../../../src/services/general-xss-filter';
  10. const mongoose = require('mongoose');
  11. const { getInstance } = require('../setup-crowi');
  12. let rootPage;
  13. let dummyUser1;
  14. let testUser1;
  15. let testUser2;
  16. let testUser3;
  17. let parentTag;
  18. let childTag;
  19. let parentForRename1;
  20. let parentForRename2;
  21. let parentForRename3;
  22. let parentForRename4;
  23. let parentForRename5;
  24. let parentForRename6;
  25. let parentForRename7;
  26. let parentForRename8;
  27. let parentForRename9;
  28. let irrelevantPage1;
  29. let irrelevantPage2;
  30. let childForRename1;
  31. let childForRename2;
  32. let childForRename3;
  33. let parentForDuplicate;
  34. let parentForDelete1;
  35. let parentForDelete2;
  36. let childForDelete;
  37. let canDeleteCompletelyTestPage;
  38. let parentForDeleteCompletely;
  39. let parentForRevert1;
  40. let parentForRevert2;
  41. let childForDuplicate;
  42. let childForDeleteCompletely;
  43. let childForRevert;
  44. describe('PageService', () => {
  45. let crowi;
  46. let Page;
  47. let Revision;
  48. let User;
  49. let Bookmark;
  50. let Comment;
  51. let ShareLink;
  52. let generalXssFilterProcessSpy;
  53. beforeAll(async() => {
  54. crowi = await getInstance();
  55. await crowi.configManager.updateConfig('app:isV5Compatible', null);
  56. User = mongoose.model('User');
  57. Page = mongoose.model('Page');
  58. Revision = mongoose.model('Revision');
  59. Bookmark = mongoose.model('Bookmark');
  60. Comment = mongoose.model('Comment');
  61. ShareLink = mongoose.model('ShareLink');
  62. await User.insertMany([
  63. { name: 'someone1', username: 'someone1', email: 'someone1@example.com' },
  64. { name: 'someone2', username: 'someone2', email: 'someone2@example.com' },
  65. { name: 'someone3', username: 'someone3', email: 'someone3@example.com' },
  66. ]);
  67. testUser1 = await User.findOne({ username: 'someone1' });
  68. testUser2 = await User.findOne({ username: 'someone2' });
  69. testUser3 = await User.findOne({ username: 'someone3' });
  70. dummyUser1 = await User.findOne({ username: 'v5DummyUser1' });
  71. await UserGroup.insertMany([
  72. {
  73. name: 'userGroupForCanDeleteCompletelyTest1',
  74. parent: null,
  75. },
  76. {
  77. name: 'userGroupForCanDeleteCompletelyTest2',
  78. parent: null,
  79. },
  80. ]);
  81. const userGroupForCanDeleteCompletelyTest1 = await UserGroup.findOne({ name: 'userGroupForCanDeleteCompletelyTest1' });
  82. const userGroupForCanDeleteCompletelyTest2 = await UserGroup.findOne({ name: 'userGroupForCanDeleteCompletelyTest2' });
  83. await UserGroupRelation.insertMany([
  84. {
  85. relatedGroup: userGroupForCanDeleteCompletelyTest1._id,
  86. relatedUser: testUser1._id,
  87. },
  88. {
  89. relatedGroup: userGroupForCanDeleteCompletelyTest2._id,
  90. relatedUser: testUser2._id,
  91. },
  92. {
  93. relatedGroup: userGroupForCanDeleteCompletelyTest1._id,
  94. relatedUser: testUser3._id,
  95. },
  96. {
  97. relatedGroup: userGroupForCanDeleteCompletelyTest2._id,
  98. relatedUser: testUser3._id,
  99. },
  100. ]);
  101. rootPage = await Page.findOne({ path: '/' });
  102. await Page.insertMany([
  103. {
  104. path: '/parentForRename1',
  105. grant: Page.GRANT_PUBLIC,
  106. creator: testUser1,
  107. lastUpdateUser: testUser1,
  108. },
  109. {
  110. path: '/parentForRename2',
  111. grant: Page.GRANT_PUBLIC,
  112. creator: testUser1,
  113. lastUpdateUser: testUser1,
  114. },
  115. {
  116. path: '/parentForRename3',
  117. grant: Page.GRANT_PUBLIC,
  118. creator: testUser1,
  119. lastUpdateUser: testUser1,
  120. },
  121. {
  122. path: '/parentForRename4',
  123. grant: Page.GRANT_PUBLIC,
  124. creator: testUser1,
  125. lastUpdateUser: testUser1,
  126. },
  127. {
  128. path: '/parentForRename5',
  129. grant: Page.GRANT_PUBLIC,
  130. creator: testUser1,
  131. lastUpdateUser: testUser1,
  132. },
  133. {
  134. path: '/parentForRename6',
  135. grant: Page.GRANT_PUBLIC,
  136. creator: testUser1,
  137. lastUpdateUser: testUser1,
  138. },
  139. {
  140. path: '/level1/level2',
  141. grant: Page.GRANT_PUBLIC,
  142. creator: testUser1,
  143. lastUpdateUser: testUser1,
  144. },
  145. {
  146. path: '/level1/level2/child',
  147. grant: Page.GRANT_PUBLIC,
  148. creator: testUser1,
  149. lastUpdateUser: testUser1,
  150. },
  151. {
  152. path: '/level1/level2/level2',
  153. grant: Page.GRANT_PUBLIC,
  154. creator: testUser1,
  155. lastUpdateUser: testUser1,
  156. },
  157. {
  158. path: '/parentForRename6-2021H1',
  159. grant: Page.GRANT_PUBLIC,
  160. creator: testUser1,
  161. lastUpdateUser: testUser1,
  162. },
  163. {
  164. path: '/level1-2021H1',
  165. grant: Page.GRANT_PUBLIC,
  166. creator: testUser1,
  167. lastUpdateUser: testUser1,
  168. },
  169. {
  170. path: '/parentForRename1/child',
  171. grant: Page.GRANT_PUBLIC,
  172. creator: testUser1,
  173. lastUpdateUser: testUser1,
  174. },
  175. {
  176. path: '/parentForRename2/child',
  177. grant: Page.GRANT_PUBLIC,
  178. creator: testUser1,
  179. lastUpdateUser: testUser1,
  180. },
  181. {
  182. path: '/parentForRename3/child',
  183. grant: Page.GRANT_PUBLIC,
  184. creator: testUser1,
  185. lastUpdateUser: testUser1,
  186. },
  187. {
  188. path: '/canDeleteCompletelyTestPage',
  189. grant: Page.GRANT_USER_GROUP,
  190. creator: testUser2,
  191. grantedGroups: [
  192. { item: userGroupForCanDeleteCompletelyTest1._id, type: GroupType.userGroup },
  193. { item: userGroupForCanDeleteCompletelyTest2._id, type: GroupType.userGroup },
  194. ],
  195. lastUpdateUser: testUser1,
  196. },
  197. {
  198. path: '/parentForDuplicate',
  199. grant: Page.GRANT_PUBLIC,
  200. creator: testUser1,
  201. lastUpdateUser: testUser1,
  202. revision: '600d395667536503354cbe91',
  203. },
  204. {
  205. path: '/parentForDuplicate/child',
  206. grant: Page.GRANT_PUBLIC,
  207. creator: testUser1,
  208. lastUpdateUser: testUser1,
  209. revision: '600d395667536503354cbe92',
  210. },
  211. {
  212. path: '/parentForDelete1',
  213. grant: Page.GRANT_PUBLIC,
  214. creator: testUser1,
  215. lastUpdateUser: testUser1,
  216. },
  217. {
  218. path: '/parentForDelete2',
  219. grant: Page.GRANT_PUBLIC,
  220. creator: testUser1,
  221. lastUpdateUser: testUser1,
  222. },
  223. {
  224. path: '/parentForDelete/child',
  225. grant: Page.GRANT_PUBLIC,
  226. creator: testUser1,
  227. lastUpdateUser: testUser1,
  228. },
  229. {
  230. path: '/parentForDeleteCompletely',
  231. grant: Page.GRANT_PUBLIC,
  232. creator: testUser1,
  233. lastUpdateUser: testUser1,
  234. },
  235. {
  236. path: '/parentForDeleteCompletely/child',
  237. grant: Page.GRANT_PUBLIC,
  238. creator: testUser1,
  239. lastUpdateUser: testUser1,
  240. },
  241. {
  242. path: '/trash/parentForRevert1',
  243. status: Page.STATUS_DELETED,
  244. grant: Page.GRANT_PUBLIC,
  245. creator: testUser1,
  246. lastUpdateUser: testUser1,
  247. },
  248. {
  249. path: '/trash/parentForRevert2',
  250. status: Page.STATUS_DELETED,
  251. grant: Page.GRANT_PUBLIC,
  252. creator: testUser1,
  253. lastUpdateUser: testUser1,
  254. },
  255. {
  256. path: '/trash/parentForRevert/child',
  257. status: Page.STATUS_DELETED,
  258. grant: Page.GRANT_PUBLIC,
  259. creator: testUser1,
  260. lastUpdateUser: testUser1,
  261. },
  262. ]);
  263. parentForRename1 = await Page.findOne({ path: '/parentForRename1' });
  264. parentForRename2 = await Page.findOne({ path: '/parentForRename2' });
  265. parentForRename3 = await Page.findOne({ path: '/parentForRename3' });
  266. parentForRename4 = await Page.findOne({ path: '/parentForRename4' });
  267. parentForRename5 = await Page.findOne({ path: '/parentForRename5' });
  268. parentForRename6 = await Page.findOne({ path: '/parentForRename6' });
  269. parentForRename7 = await Page.findOne({ path: '/level1/level2' });
  270. parentForRename8 = await Page.findOne({ path: '/level1/level2/child' });
  271. parentForRename9 = await Page.findOne({ path: '/level1/level2/level2' });
  272. irrelevantPage1 = await Page.findOne({ path: '/parentForRename6-2021H1' });
  273. irrelevantPage2 = await Page.findOne({ path: '/level1-2021H1' });
  274. parentForDuplicate = await Page.findOne({ path: '/parentForDuplicate' });
  275. parentForDelete1 = await Page.findOne({ path: '/parentForDelete1' });
  276. parentForDelete2 = await Page.findOne({ path: '/parentForDelete2' });
  277. canDeleteCompletelyTestPage = await Page.findOne({ path: '/canDeleteCompletelyTestPage' });
  278. parentForDeleteCompletely = await Page.findOne({ path: '/parentForDeleteCompletely' });
  279. parentForRevert1 = await Page.findOne({ path: '/trash/parentForRevert1' });
  280. parentForRevert2 = await Page.findOne({ path: '/trash/parentForRevert2' });
  281. childForRename1 = await Page.findOne({ path: '/parentForRename1/child' });
  282. childForRename2 = await Page.findOne({ path: '/parentForRename2/child' });
  283. childForRename3 = await Page.findOne({ path: '/parentForRename3/child' });
  284. childForDuplicate = await Page.findOne({ path: '/parentForDuplicate/child' });
  285. childForDelete = await Page.findOne({ path: '/parentForDelete/child' });
  286. childForDeleteCompletely = await Page.findOne({ path: '/parentForDeleteCompletely/child' });
  287. childForRevert = await Page.findOne({ path: '/trash/parentForRevert/child' });
  288. await Tag.insertMany([
  289. { name: 'Parent' },
  290. { name: 'Child' },
  291. ]);
  292. parentTag = await Tag.findOne({ name: 'Parent' });
  293. childTag = await Tag.findOne({ name: 'Child' });
  294. await PageTagRelation.insertMany([
  295. { relatedPage: parentForDuplicate, relatedTag: parentTag },
  296. { relatedPage: childForDuplicate, relatedTag: childTag },
  297. ]);
  298. await Revision.insertMany([
  299. {
  300. _id: '600d395667536503354cbe91',
  301. pageId: parentForDuplicate._id,
  302. body: 'duplicateBody',
  303. },
  304. {
  305. _id: '600d395667536503354cbe92',
  306. pageId: childForDuplicate._id,
  307. body: 'duplicateChildBody',
  308. },
  309. ]);
  310. generalXssFilterProcessSpy = jest.spyOn(generalXssFilter, 'process');
  311. /**
  312. * getParentAndFillAncestors
  313. */
  314. const pageIdPAF1 = new mongoose.Types.ObjectId();
  315. const pageIdPAF2 = new mongoose.Types.ObjectId();
  316. const pageIdPAF3 = new mongoose.Types.ObjectId();
  317. await Page.insertMany([
  318. {
  319. _id: pageIdPAF1,
  320. path: '/PAF1',
  321. grant: Page.GRANT_PUBLIC,
  322. creator: dummyUser1,
  323. lastUpdateUser: dummyUser1._id,
  324. isEmpty: false,
  325. parent: rootPage._id,
  326. descendantCount: 0,
  327. },
  328. {
  329. _id: pageIdPAF2,
  330. path: '/emp_anc3',
  331. grant: Page.GRANT_PUBLIC,
  332. isEmpty: true,
  333. descendantCount: 1,
  334. parent: rootPage._id,
  335. },
  336. {
  337. path: '/emp_anc3/PAF3',
  338. grant: Page.GRANT_PUBLIC,
  339. creator: dummyUser1,
  340. lastUpdateUser: dummyUser1._id,
  341. isEmpty: false,
  342. descendantCount: 0,
  343. parent: pageIdPAF2,
  344. },
  345. {
  346. _id: pageIdPAF3,
  347. path: '/emp_anc4',
  348. grant: Page.GRANT_PUBLIC,
  349. isEmpty: true,
  350. descendantCount: 1,
  351. parent: rootPage._id,
  352. },
  353. {
  354. path: '/emp_anc4/PAF4',
  355. grant: Page.GRANT_PUBLIC,
  356. creator: dummyUser1,
  357. lastUpdateUser: dummyUser1._id,
  358. isEmpty: false,
  359. descendantCount: 0,
  360. parent: pageIdPAF3,
  361. },
  362. {
  363. path: '/emp_anc4',
  364. grant: Page.GRANT_OWNER,
  365. grantedUsers: [dummyUser1._id],
  366. creator: dummyUser1,
  367. lastUpdateUser: dummyUser1._id,
  368. isEmpty: false,
  369. },
  370. {
  371. path: '/get_parent_A',
  372. creator: dummyUser1,
  373. lastUpdateUser: dummyUser1,
  374. parent: null,
  375. },
  376. {
  377. path: '/get_parent_A/get_parent_B',
  378. creator: dummyUser1,
  379. lastUpdateUser: dummyUser1,
  380. parent: null,
  381. },
  382. {
  383. path: '/get_parent_C',
  384. creator: dummyUser1,
  385. lastUpdateUser: dummyUser1,
  386. parent: rootPage._id,
  387. },
  388. {
  389. path: '/get_parent_C/get_parent_D',
  390. creator: dummyUser1,
  391. lastUpdateUser: dummyUser1,
  392. parent: null,
  393. },
  394. ]);
  395. });
  396. describe('rename page without using renameDescendantsWithStreamSpy', () => {
  397. test('rename page with different tree with isRecursively [deeper]', async() => {
  398. const resultPage = await crowi.pageService.renamePage(parentForRename6, '/parentForRename6/renamedChild', testUser1, { isRecursively: true },
  399. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  400. const wrongPage = await Page.findOne({ path: '/parentForRename6/renamedChild/renamedChild' });
  401. const expectPage1 = await Page.findOne({ path: '/parentForRename6/renamedChild' });
  402. const expectPage2 = await Page.findOne({ path: '/parentForRename6-2021H1' });
  403. expect(resultPage.path).toEqual(expectPage1.path);
  404. expect(expectPage2.path).not.toBeNull();
  405. // Check that pages that are not to be renamed have not been renamed
  406. expect(wrongPage).toBeNull();
  407. });
  408. test('rename page with different tree with isRecursively [shallower]', async() => {
  409. // setup
  410. expect(await Page.findOne({ path: '/level1' })).toBeNull();
  411. expect(await Page.findOne({ path: '/level1/level2' })).not.toBeNull();
  412. expect(await Page.findOne({ path: '/level1/level2/child' })).not.toBeNull();
  413. expect(await Page.findOne({ path: '/level1/level2/level2' })).not.toBeNull();
  414. expect(await Page.findOne({ path: '/level1-2021H1' })).not.toBeNull();
  415. // when
  416. // rename /level1/level2 --> /level1
  417. await crowi.pageService.renamePage(parentForRename7, '/level1', testUser1, { isRecursively: true },
  418. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  419. // then
  420. expect(await Page.findOne({ path: '/level1' })).not.toBeNull();
  421. expect(await Page.findOne({ path: '/level1/child' })).not.toBeNull();
  422. expect(await Page.findOne({ path: '/level1/level2' })).not.toBeNull();
  423. expect(await Page.findOne({ path: '/level1/level2/child' })).toBeNull();
  424. expect(await Page.findOne({ path: '/level1/level2/level2' })).toBeNull();
  425. // Check that pages that are not to be renamed have not been renamed
  426. expect(await Page.findOne({ path: '/level1-2021H1' })).not.toBeNull();
  427. });
  428. });
  429. describe('rename page', () => {
  430. let pageEventSpy;
  431. let renameDescendantsWithStreamSpy;
  432. // mock new Date() and Date.now()
  433. advanceTo(new Date(2000, 1, 1, 0, 0, 0));
  434. const dateToUse = new Date();
  435. beforeEach(async() => {
  436. pageEventSpy = jest.spyOn(crowi.pageService.pageEvent, 'emit').mockImplementation();
  437. renameDescendantsWithStreamSpy = jest.spyOn(crowi.pageService, 'renameDescendantsWithStream').mockImplementation();
  438. });
  439. describe('renamePage()', () => {
  440. test('rename page without options', async() => {
  441. const resultPage = await crowi.pageService.renamePage(parentForRename1,
  442. '/renamed1', testUser2, {}, { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  443. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  444. expect(pageEventSpy).toHaveBeenCalledWith('rename');
  445. expect(resultPage.path).toBe('/renamed1');
  446. expect(resultPage.updatedAt).toEqual(parentForRename1.updatedAt);
  447. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  448. });
  449. test('rename page with updateMetadata option', async() => {
  450. const resultPage = await crowi.pageService.renamePage(parentForRename2, '/renamed2', testUser2, { updateMetadata: true },
  451. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  452. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  453. expect(pageEventSpy).toHaveBeenCalledWith('rename');
  454. expect(resultPage.path).toBe('/renamed2');
  455. expect(resultPage.updatedAt).toEqual(dateToUse);
  456. expect(resultPage.lastUpdateUser).toEqual(testUser2._id);
  457. });
  458. test('rename page with createRedirectPage option', async() => {
  459. const resultPage = await crowi.pageService.renamePage(parentForRename3, '/renamed3', testUser2, { createRedirectPage: true },
  460. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  461. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  462. expect(pageEventSpy).toHaveBeenCalledWith('rename');
  463. expect(resultPage.path).toBe('/renamed3');
  464. expect(resultPage.updatedAt).toEqual(parentForRename3.updatedAt);
  465. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  466. });
  467. test('rename page with isRecursively', async() => {
  468. const resultPage = await crowi.pageService.renamePage(parentForRename4, '/renamed4', testUser2, { isRecursively: true },
  469. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  470. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  471. expect(renameDescendantsWithStreamSpy).toHaveBeenCalled();
  472. expect(pageEventSpy).toHaveBeenCalledWith('rename');
  473. expect(resultPage.path).toBe('/renamed4');
  474. expect(resultPage.updatedAt).toEqual(parentForRename4.updatedAt);
  475. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  476. });
  477. test('rename page with different tree with isRecursively', async() => {
  478. const resultPage = await crowi.pageService.renamePage(parentForRename5, '/parentForRename5/renamedChild', testUser1, { isRecursively: true },
  479. { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
  480. const wrongPage = await Page.findOne({ path: '/parentForRename5/renamedChild/renamedChild' });
  481. const expectPage = await Page.findOne({ path: '/parentForRename5/renamedChild' });
  482. expect(resultPage.path).toEqual(expectPage.path);
  483. expect(wrongPage).toBeNull();
  484. });
  485. });
  486. test('renameDescendants without options', async() => {
  487. const oldPagePathPrefix = new RegExp('^/parentForRename1', 'i');
  488. const newPagePathPrefix = '/renamed1';
  489. await crowi.pageService.renameDescendants([childForRename1], testUser2, {}, oldPagePathPrefix, newPagePathPrefix);
  490. const resultPage = await Page.findOne({ path: '/renamed1/child' });
  491. expect(resultPage).not.toBeNull();
  492. expect(pageEventSpy).toHaveBeenCalledWith('updateMany', [childForRename1], testUser2);
  493. expect(resultPage.path).toBe('/renamed1/child');
  494. expect(resultPage.updatedAt).toEqual(childForRename1.updatedAt);
  495. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  496. });
  497. test('renameDescendants with updateMetadata option', async() => {
  498. const oldPagePathPrefix = new RegExp('^/parentForRename2', 'i');
  499. const newPagePathPrefix = '/renamed2';
  500. await crowi.pageService.renameDescendants([childForRename2], testUser2, { updateMetadata: true }, oldPagePathPrefix, newPagePathPrefix);
  501. const resultPage = await Page.findOne({ path: '/renamed2/child' });
  502. expect(resultPage).not.toBeNull();
  503. expect(pageEventSpy).toHaveBeenCalledWith('updateMany', [childForRename2], testUser2);
  504. expect(resultPage.path).toBe('/renamed2/child');
  505. expect(resultPage.updatedAt).toEqual(dateToUse);
  506. expect(resultPage.lastUpdateUser).toEqual(testUser2._id);
  507. });
  508. test('renameDescendants with createRedirectPage option', async() => {
  509. const oldPagePathPrefix = new RegExp('^/parentForRename3', 'i');
  510. const newPagePathPrefix = '/renamed3';
  511. await crowi.pageService.renameDescendants([childForRename3], testUser2, { createRedirectPage: true }, oldPagePathPrefix, newPagePathPrefix);
  512. const resultPage = await Page.findOne({ path: '/renamed3/child' });
  513. expect(resultPage).not.toBeNull();
  514. expect(pageEventSpy).toHaveBeenCalledWith('updateMany', [childForRename3], testUser2);
  515. expect(resultPage.path).toBe('/renamed3/child');
  516. expect(resultPage.updatedAt).toEqual(childForRename3.updatedAt);
  517. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  518. });
  519. });
  520. describe('duplicate page', () => {
  521. let duplicateDescendantsWithStreamSpy;
  522. // TODO https://redmine.weseek.co.jp/issues/87537 : activate outer module mockImplementation
  523. // jest.mock('~/server/models/serializers/page-serializer');
  524. // const { serializePageSecurely } = require('~/server/models/serializers/page-serializer');
  525. // serializePageSecurely.mockImplementation(page => page);
  526. beforeEach(async() => {
  527. duplicateDescendantsWithStreamSpy = jest.spyOn(crowi.pageService, 'duplicateDescendantsWithStream').mockImplementation();
  528. });
  529. test('duplicate page (isRecursively: false)', async() => {
  530. const dummyId = '600d395667536503354c9999';
  531. crowi.models.Page.findRelatedTagsById = jest.fn().mockImplementation(() => { return parentTag });
  532. const originTagsMock = jest.spyOn(Page, 'findRelatedTagsById').mockImplementation(() => { return parentTag });
  533. jest.spyOn(PageTagRelation, 'updatePageTags').mockImplementation(() => { return [dummyId, parentTag.name] });
  534. jest.spyOn(PageTagRelation, 'listTagNamesByPage').mockImplementation(() => { return [parentTag.name] });
  535. const resultPage = await crowi.pageService.duplicate(parentForDuplicate, '/newParentDuplicate', testUser2, false);
  536. const duplicatedToPageRevision = await Revision.findOne({ pageId: resultPage._id });
  537. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  538. expect(duplicateDescendantsWithStreamSpy).not.toHaveBeenCalled();
  539. // TODO https://redmine.weseek.co.jp/issues/87537 : activate outer module mockImplementation
  540. // expect(serializePageSecurely).toHaveBeenCalled();
  541. expect(resultPage.path).toBe('/newParentDuplicate');
  542. expect(resultPage.lastUpdateUser._id).toEqual(testUser2._id);
  543. expect(duplicatedToPageRevision._id).not.toEqual(parentForDuplicate.revision._id);
  544. expect(resultPage.grant).toEqual(parentForDuplicate.grant);
  545. expect(resultPage.tags).toEqual([originTagsMock().name]);
  546. });
  547. test('duplicate page (isRecursively: true)', async() => {
  548. const dummyId = '600d395667536503354c9999';
  549. crowi.models.Page.findRelatedTagsById = jest.fn().mockImplementation(() => { return parentTag });
  550. const originTagsMock = jest.spyOn(Page, 'findRelatedTagsById').mockImplementation(() => { return parentTag });
  551. jest.spyOn(PageTagRelation, 'updatePageTags').mockImplementation(() => { return [dummyId, parentTag.name] });
  552. jest.spyOn(PageTagRelation, 'listTagNamesByPage').mockImplementation(() => { return [parentTag.name] });
  553. const resultPageRecursivly = await crowi.pageService.duplicate(parentForDuplicate, '/newParentDuplicateRecursively', testUser2, true);
  554. const duplicatedRecursivelyToPageRevision = await Revision.findOne({ pageId: resultPageRecursivly._id });
  555. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  556. expect(duplicateDescendantsWithStreamSpy).toHaveBeenCalled();
  557. // TODO https://redmine.weseek.co.jp/issues/87537 : activate outer module mockImplementation
  558. // expect(serializePageSecurely).toHaveBeenCalled();
  559. expect(resultPageRecursivly.path).toBe('/newParentDuplicateRecursively');
  560. expect(resultPageRecursivly.lastUpdateUser._id).toEqual(testUser2._id);
  561. expect(duplicatedRecursivelyToPageRevision._id).not.toEqual(parentForDuplicate.revision._id);
  562. expect(resultPageRecursivly.grant).toEqual(parentForDuplicate.grant);
  563. expect(resultPageRecursivly.tags).toEqual([originTagsMock().name]);
  564. });
  565. test('duplicateDescendants()', async() => {
  566. const duplicateTagsMock = await jest.spyOn(crowi.pageService, 'duplicateTags').mockImplementationOnce();
  567. await crowi.pageService.duplicateDescendants([childForDuplicate], testUser2, parentForDuplicate.path, '/newPathPrefix');
  568. const childForDuplicateRevision = await Revision.findOne({ pageId: childForDuplicate._id });
  569. const insertedPage = await Page.findOne({ path: '/newPathPrefix/child' });
  570. const insertedRevision = await Revision.findOne({ pageId: insertedPage._id });
  571. expect(insertedPage).not.toBeNull();
  572. expect(insertedPage.path).toEqual('/newPathPrefix/child');
  573. expect(insertedPage.lastUpdateUser).toEqual(testUser2._id);
  574. expect([insertedRevision]).not.toBeNull();
  575. expect(insertedRevision.pageId).toEqual(insertedPage._id);
  576. expect(insertedRevision._id).not.toEqual(childForDuplicateRevision._id);
  577. expect(insertedRevision.body).toEqual(childForDuplicateRevision.body);
  578. expect(duplicateTagsMock).toHaveBeenCalled();
  579. });
  580. test('duplicateTags()', async() => {
  581. const pageIdMapping = {
  582. [parentForDuplicate._id]: '60110bdd85339d7dc732dddd',
  583. };
  584. const duplicateTagsReturn = await crowi.pageService.duplicateTags(pageIdMapping);
  585. const parentoForDuplicateTag = await PageTagRelation.findOne({ relatedPage: parentForDuplicate._id });
  586. expect(duplicateTagsReturn).toHaveLength(1);
  587. expect(duplicateTagsReturn[0].relatedTag).toEqual(parentoForDuplicateTag.relatedTag);
  588. });
  589. });
  590. describe('delete page', () => {
  591. let getDeletedPageNameSpy;
  592. let pageEventSpy;
  593. let deleteDescendantsWithStreamSpy;
  594. const dateToUse = new Date('2000-01-01');
  595. beforeEach(async() => {
  596. jest.spyOn(global.Date, 'now').mockImplementation(() => dateToUse);
  597. getDeletedPageNameSpy = jest.spyOn(Page, 'getDeletedPageName');
  598. pageEventSpy = jest.spyOn(crowi.pageService.pageEvent, 'emit');
  599. deleteDescendantsWithStreamSpy = jest.spyOn(crowi.pageService, 'deleteDescendantsWithStream').mockImplementation();
  600. });
  601. test('delete page without options', async() => {
  602. const resultPage = await crowi.pageService.deletePage(parentForDelete1, testUser2, { }, false, {
  603. ip: '::ffff:127.0.0.1',
  604. endpoint: '/_api/v3/pages/delete',
  605. });
  606. expect(getDeletedPageNameSpy).toHaveBeenCalled();
  607. expect(deleteDescendantsWithStreamSpy).not.toHaveBeenCalled();
  608. expect(resultPage.status).toBe(Page.STATUS_DELETED);
  609. expect(resultPage.path).toBe('/trash/parentForDelete1');
  610. expect(resultPage.deleteUser).toEqual(testUser2._id);
  611. expect(resultPage.deletedAt).toEqual(dateToUse);
  612. expect(resultPage.updatedAt).toEqual(parentForDelete1.updatedAt);
  613. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  614. expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForDelete1, resultPage, testUser2);
  615. });
  616. test('delete page with isRecursively', async() => {
  617. const resultPage = await crowi.pageService.deletePage(parentForDelete2, testUser2, { }, true, {
  618. ip: '::ffff:127.0.0.1',
  619. endpoint: '/_api/v3/pages/delete',
  620. });
  621. expect(getDeletedPageNameSpy).toHaveBeenCalled();
  622. expect(deleteDescendantsWithStreamSpy).toHaveBeenCalled();
  623. expect(resultPage.status).toBe(Page.STATUS_DELETED);
  624. expect(resultPage.path).toBe('/trash/parentForDelete2');
  625. expect(resultPage.deleteUser).toEqual(testUser2._id);
  626. expect(resultPage.deletedAt).toEqual(dateToUse);
  627. expect(resultPage.updatedAt).toEqual(parentForDelete2.updatedAt);
  628. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  629. expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForDelete2, resultPage, testUser2);
  630. });
  631. test('deleteDescendants', async() => {
  632. await crowi.pageService.deleteDescendants([childForDelete], testUser2);
  633. const resultPage = await Page.findOne({ path: '/trash/parentForDelete/child' });
  634. expect(resultPage.status).toBe(Page.STATUS_DELETED);
  635. expect(resultPage.path).toBe('/trash/parentForDelete/child');
  636. expect(resultPage.deleteUser).toEqual(testUser2._id);
  637. expect(resultPage.deletedAt).toEqual(dateToUse);
  638. expect(resultPage.updatedAt).toEqual(childForDelete.updatedAt);
  639. expect(resultPage.lastUpdateUser).toEqual(testUser1._id);
  640. });
  641. });
  642. describe('delete page completely', () => {
  643. describe('canDeleteCompletely', () => {
  644. describe(`when user is not admin or author,
  645. pageCompleteDeletionAuthority is 'anyone',
  646. user is not related to all granted groups,
  647. and isAllGroupMembershipRequiredForPageCompleteDeletion is true`, () => {
  648. beforeEach(async() => {
  649. const config = {
  650. 'security:isAllGroupMembershipRequiredForPageCompleteDeletion': true,
  651. 'security:pageCompleteDeletionAuthority': PageSingleDeleteCompConfigValue.Anyone,
  652. 'security:pageRecursiveCompleteDeletionAuthority': PageRecursiveDeleteCompConfigValue.Anyone,
  653. };
  654. await crowi.configManager.updateConfigs(config);
  655. });
  656. test('is not deletable', async() => {
  657. const creatorId = await crowi.pageService.getCreatorIdForCanDelete(canDeleteCompletelyTestPage);
  658. const userRelatedGroups = await crowi.pageGrantService.getUserRelatedGroups(testUser1);
  659. const isDeleteable = crowi.pageService.canDeleteCompletely(canDeleteCompletelyTestPage, creatorId, testUser1, false, userRelatedGroups);
  660. expect(isDeleteable).toBe(false);
  661. });
  662. });
  663. describe(`when user is not admin or author,
  664. pageCompleteDeletionAuthority is 'anyone',
  665. user is related to all granted groups,
  666. and isAllGroupMembershipRequiredForPageCompleteDeletion is true`, () => {
  667. beforeEach(async() => {
  668. const config = {
  669. 'security:isAllGroupMembershipRequiredForPageCompleteDeletion': true,
  670. 'security:pageCompleteDeletionAuthority': PageSingleDeleteCompConfigValue.Anyone,
  671. 'security:pageRecursiveCompleteDeletionAuthority': PageRecursiveDeleteCompConfigValue.Anyone,
  672. };
  673. await crowi.configManager.updateConfigs(config);
  674. });
  675. test('is not deletable', async() => {
  676. const creatorId = await crowi.pageService.getCreatorIdForCanDelete(canDeleteCompletelyTestPage);
  677. const userRelatedGroups = await crowi.pageGrantService.getUserRelatedGroups(testUser3);
  678. const isDeleteable = crowi.pageService.canDeleteCompletely(canDeleteCompletelyTestPage, creatorId, testUser3, false, userRelatedGroups);
  679. expect(isDeleteable).toBe(true);
  680. });
  681. });
  682. describe(`when user is not admin or author,
  683. pageCompleteDeletionAuthority is 'anyone',
  684. user is not related to all granted groups,
  685. and isAllGroupMembershipRequiredForPageCompleteDeletion is false`, () => {
  686. beforeEach(async() => {
  687. const config = {
  688. 'security:isAllGroupMembershipRequiredForPageCompleteDeletion': false,
  689. 'security:pageCompleteDeletionAuthority': PageSingleDeleteCompConfigValue.Anyone,
  690. 'security:pageRecursiveCompleteDeletionAuthority': PageRecursiveDeleteCompConfigValue.Anyone,
  691. };
  692. await crowi.configManager.updateConfigs(config);
  693. });
  694. test('is deletable', async() => {
  695. const creatorId = await crowi.pageService.getCreatorIdForCanDelete(canDeleteCompletelyTestPage);
  696. const userRelatedGroups = await crowi.pageGrantService.getUserRelatedGroups(testUser1);
  697. const isDeleteable = crowi.pageService.canDeleteCompletely(canDeleteCompletelyTestPage, creatorId, testUser1, false, userRelatedGroups);
  698. expect(isDeleteable).toBe(true);
  699. });
  700. });
  701. describe(`when user is author,
  702. pageCompleteDeletionAuthority is 'anyone',
  703. user is not related to all granted groups,
  704. and isAllGroupMembershipRequiredForPageCompleteDeletion is true`, () => {
  705. beforeEach(async() => {
  706. const config = {
  707. 'security:isAllGroupMembershipRequiredForPageCompleteDeletion': false,
  708. 'security:pageCompleteDeletionAuthority': PageSingleDeleteCompConfigValue.Anyone,
  709. 'security:pageRecursiveCompleteDeletionAuthority': PageRecursiveDeleteCompConfigValue.Anyone,
  710. };
  711. await crowi.configManager.updateConfigs(config);
  712. });
  713. test('is deletable', async() => {
  714. const creatorId = await crowi.pageService.getCreatorIdForCanDelete(canDeleteCompletelyTestPage);
  715. const userRelatedGroups = await crowi.pageGrantService.getUserRelatedGroups(testUser2);
  716. const isDeleteable = crowi.pageService.canDeleteCompletely(canDeleteCompletelyTestPage, creatorId, testUser2, false, userRelatedGroups);
  717. expect(isDeleteable).toBe(true);
  718. });
  719. });
  720. });
  721. describe('actual delete process', () => {
  722. let pageEventSpy;
  723. let deleteCompletelyOperationSpy;
  724. let deleteCompletelyDescendantsWithStreamSpy;
  725. let deleteManyBookmarkSpy;
  726. let deleteManyCommentSpy;
  727. let deleteManyPageTagRelationSpy;
  728. let deleteManyShareLinkSpy;
  729. let deleteManyRevisionSpy;
  730. let deleteManyPageSpy;
  731. let removeAllAttachmentsSpy;
  732. beforeEach(async() => {
  733. pageEventSpy = jest.spyOn(crowi.pageService.pageEvent, 'emit');
  734. deleteCompletelyOperationSpy = jest.spyOn(crowi.pageService, 'deleteCompletelyOperation');
  735. deleteCompletelyDescendantsWithStreamSpy = jest.spyOn(crowi.pageService, 'deleteCompletelyDescendantsWithStream').mockImplementation();
  736. deleteManyBookmarkSpy = jest.spyOn(Bookmark, 'deleteMany').mockImplementation();
  737. deleteManyCommentSpy = jest.spyOn(Comment, 'deleteMany').mockImplementation();
  738. deleteManyPageTagRelationSpy = jest.spyOn(PageTagRelation, 'deleteMany').mockImplementation();
  739. deleteManyShareLinkSpy = jest.spyOn(ShareLink, 'deleteMany').mockImplementation();
  740. deleteManyRevisionSpy = jest.spyOn(Revision, 'deleteMany').mockImplementation();
  741. deleteManyPageSpy = jest.spyOn(Page, 'deleteMany').mockImplementation();
  742. removeAllAttachmentsSpy = jest.spyOn(crowi.attachmentService, 'removeAllAttachments').mockImplementation();
  743. });
  744. test('deleteCompletelyOperation', async() => {
  745. await crowi.pageService.deleteCompletelyOperation([parentForDeleteCompletely._id], [parentForDeleteCompletely.path], { });
  746. // bookmarks should not be deleted
  747. expect(deleteManyBookmarkSpy).not.toHaveBeenCalled();
  748. expect(deleteManyCommentSpy).toHaveBeenCalledWith({ page: { $in: [parentForDeleteCompletely._id] } });
  749. expect(deleteManyPageTagRelationSpy).toHaveBeenCalledWith({ relatedPage: { $in: [parentForDeleteCompletely._id] } });
  750. expect(deleteManyShareLinkSpy).toHaveBeenCalledWith({ relatedPage: { $in: [parentForDeleteCompletely._id] } });
  751. expect(deleteManyRevisionSpy).toHaveBeenCalledWith({ pageId: { $in: [parentForDeleteCompletely._id] } });
  752. expect(deleteManyPageSpy).toHaveBeenCalledWith({ _id: { $in: [parentForDeleteCompletely._id] } });
  753. expect(removeAllAttachmentsSpy).toHaveBeenCalled();
  754. });
  755. test('delete completely without options', async() => {
  756. await crowi.pageService.deleteCompletely(parentForDeleteCompletely, testUser2, { }, false, false, {
  757. ip: '::ffff:127.0.0.1',
  758. endpoint: '/_api/v3/pages/deletecompletely',
  759. });
  760. expect(deleteCompletelyOperationSpy).toHaveBeenCalled();
  761. expect(deleteCompletelyDescendantsWithStreamSpy).not.toHaveBeenCalled();
  762. expect(pageEventSpy).toHaveBeenCalledWith('deleteCompletely', parentForDeleteCompletely, testUser2);
  763. });
  764. test('delete completely with isRecursively', async() => {
  765. await crowi.pageService.deleteCompletely(parentForDeleteCompletely, testUser2, { }, true, false, {
  766. ip: '::ffff:127.0.0.1',
  767. endpoint: '/_api/v3/pages/deletecompletely',
  768. });
  769. expect(deleteCompletelyOperationSpy).toHaveBeenCalled();
  770. expect(deleteCompletelyDescendantsWithStreamSpy).toHaveBeenCalled();
  771. expect(pageEventSpy).toHaveBeenCalledWith('deleteCompletely', parentForDeleteCompletely, testUser2);
  772. });
  773. });
  774. });
  775. describe('revert page', () => {
  776. let getRevertDeletedPageNameSpy;
  777. let findByPathSpy;
  778. let findSpy;
  779. let deleteCompletelySpy;
  780. let revertDeletedDescendantsWithStreamSpy;
  781. beforeEach(async() => {
  782. getRevertDeletedPageNameSpy = jest.spyOn(Page, 'getRevertDeletedPageName');
  783. deleteCompletelySpy = jest.spyOn(crowi.pageService, 'deleteCompletely').mockImplementation();
  784. revertDeletedDescendantsWithStreamSpy = jest.spyOn(crowi.pageService, 'revertDeletedDescendantsWithStream').mockImplementation();
  785. });
  786. test('revert deleted page when the redirect from page exists', async() => {
  787. const resultPage = await crowi.pageService.revertDeletedPage(parentForRevert1, testUser2, {}, false, {
  788. ip: '::ffff:127.0.0.1',
  789. endpoint: '/_api/v3/pages/revert',
  790. });
  791. expect(getRevertDeletedPageNameSpy).toHaveBeenCalledWith(parentForRevert1.path);
  792. expect(revertDeletedDescendantsWithStreamSpy).not.toHaveBeenCalled();
  793. expect(resultPage.path).toBe('/parentForRevert1');
  794. expect(resultPage.lastUpdateUser._id).toEqual(testUser2._id);
  795. expect(resultPage.status).toBe(Page.STATUS_PUBLISHED);
  796. expect(resultPage.deleteUser).toBeNull();
  797. expect(resultPage.deletedAt).toBeNull();
  798. });
  799. test('revert deleted page when the redirect from page does not exist', async() => {
  800. findByPathSpy = jest.spyOn(Page, 'findByPath').mockImplementation(() => {
  801. return null;
  802. });
  803. const resultPage = await crowi.pageService.revertDeletedPage(parentForRevert2, testUser2, {}, true, {
  804. ip: '::ffff:127.0.0.1',
  805. endpoint: '/_api/v3/pages/revert',
  806. });
  807. expect(getRevertDeletedPageNameSpy).toHaveBeenCalledWith(parentForRevert2.path);
  808. expect(findByPathSpy).toHaveBeenCalledWith('/parentForRevert2');
  809. expect(deleteCompletelySpy).not.toHaveBeenCalled();
  810. expect(revertDeletedDescendantsWithStreamSpy).toHaveBeenCalled();
  811. expect(resultPage.path).toBe('/parentForRevert2');
  812. expect(resultPage.lastUpdateUser._id).toEqual(testUser2._id);
  813. expect(resultPage.status).toBe(Page.STATUS_PUBLISHED);
  814. expect(resultPage.deleteUser).toBeNull();
  815. expect(resultPage.deletedAt).toBeNull();
  816. });
  817. test('revert deleted descendants', async() => {
  818. await crowi.pageService.revertDeletedDescendants([childForRevert], testUser2);
  819. const resultPage = await Page.findOne({ path: '/parentForRevert/child' });
  820. const revrtedFromPage = await Page.findOne({ path: '/trash/parentForRevert/child' });
  821. const revrtedFromPageRevision = await Revision.findOne({ pageId: resultPage._id });
  822. expect(getRevertDeletedPageNameSpy).toHaveBeenCalledWith(childForRevert.path);
  823. expect(resultPage.path).toBe('/parentForRevert/child');
  824. expect(resultPage.lastUpdateUser._id).toEqual(testUser2._id);
  825. expect(resultPage.status).toBe(Page.STATUS_PUBLISHED);
  826. expect(resultPage.deleteUser).toBeNull();
  827. expect(resultPage.deletedAt).toBeNull();
  828. expect(revrtedFromPage).toBeNull();
  829. expect(revrtedFromPageRevision).toBeNull();
  830. });
  831. });
  832. describe('getParentAndFillAncestors', () => {
  833. test('return parent if exist', async() => {
  834. const page1 = await Page.findOne({ path: '/PAF1' });
  835. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, page1.path);
  836. expect(parent).toBeTruthy();
  837. expect(page1.parent).toStrictEqual(parent._id);
  838. });
  839. test('create parent and ancestors when they do not exist, and return the new parent', async() => {
  840. const path1 = '/emp_anc1';
  841. const path2 = '/emp_anc1/emp_anc2';
  842. const path3 = '/emp_anc1/emp_anc2/PAF2';
  843. const _page1 = await Page.findOne({ path: path1 }); // not exist
  844. const _page2 = await Page.findOne({ path: path2 }); // not exist
  845. const _page3 = await Page.findOne({ path: path3 }); // not exist
  846. expect(_page1).toBeNull();
  847. expect(_page2).toBeNull();
  848. expect(_page3).toBeNull();
  849. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, path3);
  850. const page1 = await Page.findOne({ path: path1 });
  851. const page2 = await Page.findOne({ path: path2 });
  852. const page3 = await Page.findOne({ path: path3 });
  853. expect(parent._id).toStrictEqual(page2._id);
  854. expect(parent.path).toStrictEqual(page2.path);
  855. expect(parent.parent).toStrictEqual(page2.parent);
  856. expect(parent).toBeTruthy();
  857. expect(page1).toBeTruthy();
  858. expect(page2).toBeTruthy();
  859. expect(page3).toBeNull();
  860. expect(page1.parent).toStrictEqual(rootPage._id);
  861. expect(page2.parent).toStrictEqual(page1._id);
  862. });
  863. test('return parent even if the parent page is empty', async() => {
  864. const path1 = '/emp_anc3';
  865. const path2 = '/emp_anc3/PAF3';
  866. const _page1 = await Page.findOne({ path: path1, isEmpty: true });
  867. const _page2 = await Page.findOne({ path: path2, isEmpty: false });
  868. expect(_page1).toBeTruthy();
  869. expect(_page2).toBeTruthy();
  870. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, _page2.path);
  871. const page1 = await Page.findOne({ path: path1, isEmpty: true }); // parent
  872. const page2 = await Page.findOne({ path: path2, isEmpty: false });
  873. // check for the parent (should be the same as page1)
  874. expect(parent._id).toStrictEqual(page1._id);
  875. expect(parent.path).toStrictEqual(page1.path);
  876. expect(parent.parent).toStrictEqual(page1.parent);
  877. expect(page1.parent).toStrictEqual(rootPage._id);
  878. expect(page2.parent).toStrictEqual(page1._id);
  879. });
  880. test('should find parent while NOT updating private legacy page\'s parent', async() => {
  881. const path1 = '/emp_anc4';
  882. const path2 = '/emp_anc4/PAF4';
  883. const _page1 = await Page.findOne({ path: path1, isEmpty: true, grant: Page.GRANT_PUBLIC });
  884. const _page2 = await Page.findOne({ path: path2, isEmpty: false, grant: Page.GRANT_PUBLIC });
  885. const _page3 = await Page.findOne({ path: path1, isEmpty: false, grant: Page.GRANT_OWNER });
  886. expect(_page1).toBeTruthy();
  887. expect(_page2).toBeTruthy();
  888. expect(_page3).toBeTruthy();
  889. expect(_page3.parent).toBeNull();
  890. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, _page2.path);
  891. const page1 = await Page.findOne({ path: path1, isEmpty: true, grant: Page.GRANT_PUBLIC });
  892. const page2 = await Page.findOne({ path: path2, isEmpty: false, grant: Page.GRANT_PUBLIC });
  893. const page3 = await Page.findOne({ path: path1, isEmpty: false, grant: Page.GRANT_OWNER });
  894. expect(page1).toBeTruthy();
  895. expect(page2).toBeTruthy();
  896. expect(page3).toBeTruthy();
  897. expect(page3.parent).toBeNull(); // parent property of page in private legacy pages should be null
  898. expect(page1._id).toStrictEqual(parent._id);
  899. expect(page2.parent).toStrictEqual(parent._id);
  900. });
  901. test('should find parent while NOT creating unnecessary empty pages with all v4 public pages', async() => {
  902. // All pages does not have parent (v4 schema)
  903. const _pageA = await Page.findOne({
  904. path: '/get_parent_A',
  905. grant: Page.GRANT_PUBLIC,
  906. isEmpty: false,
  907. parent: null,
  908. });
  909. const _pageAB = await Page.findOne({
  910. path: '/get_parent_A/get_parent_B',
  911. grant: Page.GRANT_PUBLIC,
  912. isEmpty: false,
  913. parent: null,
  914. });
  915. const _emptyA = await Page.findOne({
  916. path: '/get_parent_A',
  917. grant: Page.GRANT_PUBLIC,
  918. isEmpty: true,
  919. });
  920. const _emptyAB = await Page.findOne({
  921. path: '/get_parent_A/get_parent_B',
  922. grant: Page.GRANT_PUBLIC,
  923. isEmpty: true,
  924. });
  925. expect(_pageA).not.toBeNull();
  926. expect(_pageAB).not.toBeNull();
  927. expect(_emptyA).toBeNull();
  928. expect(_emptyAB).toBeNull();
  929. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, '/get_parent_A/get_parent_B/get_parent_C');
  930. const pageA = await Page.findOne({ path: '/get_parent_A', grant: Page.GRANT_PUBLIC, isEmpty: false });
  931. const pageAB = await Page.findOne({ path: '/get_parent_A/get_parent_B', grant: Page.GRANT_PUBLIC, isEmpty: false });
  932. const emptyA = await Page.findOne({ path: '/get_parent_A', grant: Page.GRANT_PUBLIC, isEmpty: true });
  933. const emptyAB = await Page.findOne({ path: '/get_parent_A/get_parent_B', grant: Page.GRANT_PUBLIC, isEmpty: true });
  934. // -- Check existance
  935. expect(parent).not.toBeNull();
  936. expect(pageA).not.toBeNull();
  937. expect(pageAB).not.toBeNull();
  938. expect(emptyA).toBeNull();
  939. expect(emptyAB).toBeNull();
  940. // -- Check parent
  941. expect(pageA.parent).not.toBeNull();
  942. expect(pageAB.parent).not.toBeNull();
  943. });
  944. test('should find parent while NOT creating unnecessary empty pages with some v5 public pages', async() => {
  945. const _pageC = await Page.findOne({
  946. path: '/get_parent_C',
  947. grant: Page.GRANT_PUBLIC,
  948. isEmpty: false,
  949. parent: { $ne: null },
  950. });
  951. const _pageCD = await Page.findOne({
  952. path: '/get_parent_C/get_parent_D',
  953. grant: Page.GRANT_PUBLIC,
  954. isEmpty: false,
  955. });
  956. const _emptyC = await Page.findOne({
  957. path: '/get_parent_C',
  958. grant: Page.GRANT_PUBLIC,
  959. isEmpty: true,
  960. });
  961. const _emptyCD = await Page.findOne({
  962. path: '/get_parent_C/get_parent_D',
  963. grant: Page.GRANT_PUBLIC,
  964. isEmpty: true,
  965. });
  966. expect(_pageC).not.toBeNull();
  967. expect(_pageCD).not.toBeNull();
  968. expect(_emptyC).toBeNull();
  969. expect(_emptyCD).toBeNull();
  970. const parent = await crowi.pageService.getParentAndFillAncestorsByUser(dummyUser1, '/get_parent_C/get_parent_D/get_parent_E');
  971. const pageC = await Page.findOne({ path: '/get_parent_C', grant: Page.GRANT_PUBLIC, isEmpty: false });
  972. const pageCD = await Page.findOne({ path: '/get_parent_C/get_parent_D', grant: Page.GRANT_PUBLIC, isEmpty: false });
  973. const emptyC = await Page.findOne({ path: '/get_parent_C', grant: Page.GRANT_PUBLIC, isEmpty: true });
  974. const emptyCD = await Page.findOne({ path: '/get_parent_C/get_parent_D', grant: Page.GRANT_PUBLIC, isEmpty: true });
  975. // -- Check existance
  976. expect(parent).not.toBeNull();
  977. expect(pageC).not.toBeNull();
  978. expect(pageCD).not.toBeNull();
  979. expect(emptyC).toBeNull();
  980. expect(emptyCD).toBeNull();
  981. // -- Check parent attribute
  982. expect(pageC.parent).toStrictEqual(rootPage._id);
  983. expect(pageCD.parent).toStrictEqual(pageC._id);
  984. // -- Check the found parent
  985. expect(parent.toObject()).toStrictEqual(pageCD.toObject());
  986. });
  987. });
  988. });