/** * Drafts as related documents (owner's decision Q5, 1.18.0): a block, a menu or a System pages link that points at a * page never published no longer brings that page's title, slug and text into what the public helpers answer. * `draftRelations: 'keep'` and a preview (`draft: true`) keep the old behaviour. * Real database; SQLite by default, Postgres / MongoDB with IPAL_TEST_POSTGRES_URI / IPAL_TEST_MONGO_URI. */ import type { Payload } from 'payload' import { lexicalEditor } from '@payloadcms/richtext-lexical' import os from 'os' import path from 'path' import { getPayload } from 'payload' import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest' import { withoutDraftRelations } from '../src/modules/content/draftRelations.js' import { getArchiveEntries } from '../src/modules/content/getArchiveEntries.js' import { resolveRoute } from '../src/modules/content/resolveRoute.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js' vi.mock('server-only', () => ({})) describe('withoutDraftRelations', () => { const draft = { id: 2, _status: 'draft', title: 'Secret' } const live = { id: 1, _status: 'published', title: 'Live' } test('drops a draft related document: null alone, removed from a list, value null in a polymorphic one', () => { const doc = { id: 9, _status: 'published', layout: [{ blockType: 'cards', items: [live, draft], one: draft, poly: { relationTo: 'pages', value: draft } }] } expect(withoutDraftRelations(doc)).toEqual({ id: 9, _status: 'published', layout: [{ blockType: 'cards', items: [live], one: null, poly: { relationTo: 'pages', value: null } }] }) }) test('the document itself, ids, uploads and documents without _status are left alone; nothing is mutated', () => { const doc = { id: 3, _status: 'draft', image: { id: 5, url: '/m.png' }, page: 2, pages: [1, 2] } expect(withoutDraftRelations(doc)).toBe(doc) const nested = { a: { b: draft } } const out = withoutDraftRelations(nested) expect(out).toEqual({ a: { b: null } }) expect(nested.a.b).toBe(draft) }) }) describe('the public helpers on a real database', () => { let payload: Payload beforeAll(async () => { const config = buildTestConfig({ collections: [ { slug: 'pages', access: { read: () => true }, fields: [ { name: 'title', type: 'text' }, { name: 'slug', type: 'text', localized: true }, { name: 'related', type: 'relationship', relationTo: 'pages' }, { name: 'list', type: 'relationship', hasMany: true, relationTo: 'pages' }, { name: 'layout', type: 'blocks', blocks: [{ slug: 'link', fields: [{ name: 'page', type: 'relationship', relationTo: ['pages', 'posts'] }] }] }, ], versions: { drafts: true }, }, { slug: 'posts', fields: [{ name: 'title', type: 'text' }, { name: 'slug', type: 'text', localized: true }, { name: 'see', type: 'relationship', relationTo: 'pages' }], versions: { drafts: true } }, { slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'iapl-draft-relations') } }, ], db: testDatabase('draft_relations'), editor: lexicalEditor(), plugins: [iapl({ access: { authCollection: 'users' }, content: { collections: [{ slug: 'posts' }] }, i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }, mail: { relay: false }, pages: { slug: 'pages' }, twoFactor: false })], secret: 'draft-relations-0123456789-0123456789', telemetry: false, }) payload = await getPayload({ config }) const secret = await payload.create({ collection: 'pages' as never, data: { slug: 'tajna', _status: 'draft', title: 'Secret price list' } as never, draft: true }) const live = await payload.create({ collection: 'pages' as never, data: { slug: 'o-nas', _status: 'published', title: 'About' } as never }) await payload.create({ collection: 'pages' as never, data: { slug: 'start', _status: 'published', layout: [{ blockType: 'link', page: { relationTo: 'pages', value: secret.id } }], list: [live.id, secret.id], related: secret.id, title: 'Start' } as never, }) await payload.create({ collection: 'posts' as never, data: { slug: 'wpis', _status: 'published', see: secret.id, title: 'Post' } as never }) }, 120_000) afterAll(async () => { await payload?.destroy?.() }) const start = async (options: Record = {}) => (await resolveRoute({ locale: 'pl', payload, segments: ['start'], ...options }))?.doc as Record test('resolveRoute: the page answers without the draft it points at (single, list, polymorphic block)', async () => { const doc = await start() expect(doc.related).toBeNull() expect((doc.list as { title: string }[]).map((p) => p.title)).toEqual(['About']) expect((doc.layout as { page: { value: unknown } }[])[0].page.value).toBeNull() expect(JSON.stringify(doc)).not.toContain('Secret price list') }) test("draftRelations: 'keep' and a preview answer them as before", async () => { for (const options of [{ draftRelations: 'keep' }, { draft: true }]) { expect(JSON.stringify(await start(options))).toContain('Secret price list') } }) test('getArchiveEntries: an entry without the draft it points at; keep as before', async () => { const hidden = await getArchiveEntries({ collection: 'posts', locale: 'pl', payload }) expect((hidden.docs[0] as { see: unknown }).see).toBeNull() const kept = await getArchiveEntries({ collection: 'posts', draftRelations: 'keep', locale: 'pl', payload }) expect(JSON.stringify(kept.docs)).toContain('Secret price list') }) })