Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/payload/src/collections/operations/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const countOperation = async <TSlug extends CollectionSlug>(

result = await payload.db.count({
collection: collectionConfig.slug,
locale: req?.locale || undefined,
req,
where: fullWhere,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const countGlobalVersionsOperation = async <TSlug extends GlobalSlug>(

const result = await payload.db.countGlobalVersions({
global: global.slug,
locale: req?.locale || undefined,
req,
where: fullWhere,
})
Expand Down
87 changes: 86 additions & 1 deletion test/localization/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Payload, User, Where } from 'payload'
import path from 'path'
import { createLocalReq } from 'payload'
import { fileURLToPath } from 'url'
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'

import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js'
import type {
Expand Down Expand Up @@ -66,6 +66,60 @@ describe('Localization', () => {
await payload.destroy()
})

describe('count with localized field query', () => {
const createdIDs: Array<number | string> = []

afterEach(async () => {
for (const id of createdIDs) {
await payload.delete({ collection: localizedPostsSlug, id })
}
createdIDs.length = 0
})

it('should respect the requested locale and agree with find', async () => {
const doc = await payload.create({
collection: localizedPostsSlug,
data: { title: 'count-en' },
locale: englishLocale,
})
createdIDs.push(doc.id)

await payload.update({
id: doc.id,
collection: localizedPostsSlug,
data: { title: 'count-es' },
locale: spanishLocale,
})

const englishWhere = { title: { equals: 'count-en' } }

const { totalDocs: countEn } = await payload.count({
collection: localizedPostsSlug,
locale: englishLocale,
where: englishWhere,
})

const { docs: findEn } = await payload.find({
collection: localizedPostsSlug,
locale: englishLocale,
where: englishWhere,
})

// count must agree with find for a localized-field query
expect(countEn).toBe(findEn.length)
expect(countEn).toBe(1)

// The same value must not match in a locale where the field holds a different value
const { totalDocs: countEsForEnglishTitle } = await payload.count({
collection: localizedPostsSlug,
locale: spanishLocale,
where: englishWhere,
})

expect(countEsForEnglishTitle).toBe(0)
})
})

describe('Localization with fallback true', () => {
let post1: LocalizedPost
let postWithLocalizedData: LocalizedPost
Expand Down Expand Up @@ -4558,6 +4612,37 @@ describe('Localization', () => {
})
expect(result2.totalDocs).toBe(1)
})

it('should count global versions with query on localized field respecting locale', async () => {
await payload.updateGlobal({
slug: globalWithDraftsSlug,
data: { text: 'global count en', _status: 'published' },
locale: defaultLocale,
})

await payload.updateGlobal({
slug: globalWithDraftsSlug,
data: { text: 'global count es', _status: 'published' },
locale: spanishLocale,
})

const englishWhere = { 'version.text': { equals: 'global count en' } }

const inEnglish = await payload.countGlobalVersions({
global: globalWithDraftsSlug,
locale: defaultLocale,
where: englishWhere,
})

const inSpanish = await payload.countGlobalVersions({
global: globalWithDraftsSlug,
locale: spanishLocale,
where: englishWhere,
})

expect(inEnglish.totalDocs).toBeGreaterThan(0)
expect(inSpanish.totalDocs).toBe(0)
})
})
})

Expand Down
Loading