Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix bulk category add/remove for taxonomy-less categories + add e2e

Damian Legawiece95e94544198
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index ea002e041e..0d7219e48b 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -13,7 +13,8 @@
"Bash(rm -rf server)",
"Bash(git clone *)",
"Bash(rm -rf server/.git server/.gitignore)",
- "Skill(update-config)"
+ "Skill(update-config)",
+ "Skill(simplify)"
],
"deny": [
"Bash(sudo *)",
diff --git a/packages/dashboard/e2e/categories.spec.ts b/packages/dashboard/e2e/categories.spec.ts
new file mode 100644
index 0000000000..8682b8c356
--- /dev/null
+++ b/packages/dashboard/e2e/categories.spec.ts
@@ -0,0 +1,150 @@
+import { expect, type Page, test } from '@playwright/test'
+import { FIXTURE_PROMO_PRODUCT, gotoIndex, login } from './helpers'
+
+const CATEGORIES_PATH = (storeId: string) => `/${storeId}/products/categories`
+const CTA = /new category/i
+
+// The category tree renders each row's name as a clickable <button> (it
+// navigates to the detail page). Anchor on the exact name to avoid matching
+// substrings of sibling categories created by other tests in the serial run.
+function treeRow(page: Page, name: string) {
+ return page.getByRole('button', { name: new RegExp(`^${name}$`) })
+}
+
+// Create a top-level category through the full-page "New category" form and
+// land back on the tree. Returns once the new row is visible.
+async function createCategory(page: Page, name: string) {
+ await page.getByRole('button', { name: CTA }).click()
+ await expect(page.getByRole('heading', { name: CTA })).toBeVisible({ timeout: 15_000 })
+
+ await page.locator('#category-name').fill(name)
+ await page.getByRole('button', { name: /^save$/i }).click()
+
+ // On success the create route navigates to the edit page; go back to the
+ // tree and confirm the row landed.
+ await page.goto(CATEGORIES_PATH((await currentStoreId(page)) ?? ''))
+ await expect(treeRow(page, name)).toBeVisible({ timeout: 15_000 })
+}
+
+// The store id is in the URL; read it so we can navigate back to the index.
+async function currentStoreId(page: Page): Promise<string | null> {
+ const match = page.url().match(/\/([^/]+)\/products\/categories/)
+ return match?.[1] ?? null
+}
+
+test.describe('categories management', () => {
+ test('creates a category', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, CATEGORIES_PATH(creds.store_id), CTA)
+
+ const name = `E2E Category ${Date.now()}`
+ await createCategory(page, name)
+
+ await expect(treeRow(page, name)).toBeVisible()
+ })
+
+ test('edits a category name', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, CATEGORIES_PATH(creds.store_id), CTA)
+
+ const original = `E2E Edit ${Date.now()}`
+ await createCategory(page, original)
+
+ // Click the category name in the tree → full-page edit.
+ await treeRow(page, original).click()
+ await expect(page.locator('#category-name')).toHaveValue(original, { timeout: 15_000 })
+
+ const renamed = `${original} Renamed`
+ await page.locator('#category-name').fill(renamed)
+ await page.getByRole('button', { name: /^save$/i }).click()
+
+ // Back to the tree — the renamed row is present, the old name is gone.
+ await page.goto(CATEGORIES_PATH(creds.store_id))
+ await expect(treeRow(page, renamed)).toBeVisible({ timeout: 15_000 })
+ await expect(treeRow(page, original)).toHaveCount(0)
+ })
+
+ test('adds and removes a product', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, CATEGORIES_PATH(creds.store_id), CTA)
+
+ const name = `E2E Products ${Date.now()}`
+ await createCategory(page, name)
+ await treeRow(page, name).click()
+ await expect(page.locator('#category-name')).toHaveValue(name, { timeout: 15_000 })
+
+ // Fresh category: the products panel shows its empty state.
+ await expect(page.getByText(/no products in this category yet/i)).toBeVisible({
+ timeout: 15_000,
+ })
+
+ // Open the picker sheet, search for the seeded product, select it, confirm.
+ await page.getByRole('button', { name: /add products/i }).click()
+ const picker = page.getByRole('dialog')
+ await expect(picker.getByRole('heading', { name: /add products to category/i })).toBeVisible()
+
+ // Search is debounced + async; wait for the matching option to render
+ // before selecting it.
+ await picker.getByRole('searchbox').fill(FIXTURE_PROMO_PRODUCT)
+ const option = picker
+ .getByRole('button', { name: new RegExp(FIXTURE_PROMO_PRODUCT, 'i') })
+ .first()
+ await expect(option).toBeVisible({ timeout: 15_000 })
+ await option.click()
+
+ // Selecting stages it; the confirm button flips from "Add 0" to "Add 1".
+ const confirm = picker.getByRole('button', { name: /^add 1$/i })
+ await expect(confirm).toBeEnabled({ timeout: 15_000 })
+ await confirm.click()
+
+ // The product row now renders in the products table.
+ const productRow = page.getByRole('row', { name: new RegExp(FIXTURE_PROMO_PRODUCT, 'i') })
+ await expect(productRow).toBeVisible({ timeout: 15_000 })
+
+ // Remove it via the per-row remove button.
+ await productRow.getByRole('button', { name: /remove from category/i }).click()
+ await expect(page.getByText(/no products in this category yet/i)).toBeVisible({
+ timeout: 15_000,
+ })
+ })
+
+ test('repositions a top-level category', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, CATEGORIES_PATH(creds.store_id), CTA)
+
+ // Two fresh siblings with a shared prefix so their relative order is
+ // unambiguous and isolated from other rows.
+ const prefix = `E2E Reorder ${Date.now()}`
+ const first = `${prefix} A`
+ const second = `${prefix} B`
+ await createCategory(page, first)
+ await createCategory(page, second)
+
+ const rowA = treeRow(page, first)
+ const rowB = treeRow(page, second)
+ await expect(rowA).toBeVisible()
+ await expect(rowB).toBeVisible()
+
+ // Drag B above A. dnd-kit's PointerSensor has `activationConstraint:
+ // { distance: 5 }`, so move past the threshold before settling on the
+ // target — a single dragTo() can land below the activation distance.
+ const handleB = rowB.locator('xpath=ancestor::tr').getByLabel(/drag to reorder/i)
+ const targetBox = await rowA.locator('xpath=ancestor::tr').boundingBox()
+ if (!targetBox) throw new Error('could not resolve row A bounding box')
+
+ await handleB.hover()
+ await page.mouse.down()
+ await page.mouse.move(targetBox.x + targetBox.width / 2, targetBox.y - 4, { steps: 8 })
+ await page.mouse.move(targetBox.x + targetBox.width / 2, targetBox.y + 2, { steps: 4 })
+ await page.mouse.up()
+
+ // After reordering, B precedes A in the tree's DOM order. Compare the
+ // vertical position of the two rows.
+ await expect(async () => {
+ const boxA = await rowA.boundingBox()
+ const boxB = await rowB.boundingBox()
+ expect(boxA && boxB).toBeTruthy()
+ expect((boxB as { y: number }).y).toBeLessThan((boxA as { y: number }).y)
+ }).toPass({ timeout: 15_000 })
+ })
+})
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index 8081509110..308ba94887 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -218,7 +218,8 @@ module Spree
authorize! :update, model_class
category_ids = decode_ids(params[:category_ids])
- categories = current_store.taxons.accessible_by(current_ability, :update).where(id: category_ids)
+ categories = current_store.categories.
+ accessible_by(current_ability, :update).where(id: category_ids)
service.call(taxons: categories, products: bulk_collection)
Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index 449f74c575..86f89f8b48 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -1311,6 +1311,21 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(second_product.reload.taxons).to include(category, other_category)
end
+ it 'attaches to a taxonomy-less, store-owned category' do
+ # Regression: categories were scoped via the through-taxonomy association,
+ # which misses store-owned categories that have no taxonomy.
+ store_category = Spree::Category.create!(name: 'Store Owned', store: store)
+
+ post :bulk_add_to_categories, params: {
+ ids: [product.prefixed_id],
+ category_ids: [store_category.prefixed_id]
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['category_count']).to eq(1)
+ expect(product.reload.taxons).to include(store_category)
+ end
+
it 'silently ignores categories from other stores' do
foreign_taxonomy = create(:taxonomy, store: create(:store))
foreign_category = create(:taxon, taxonomy: foreign_taxonomy)
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index a17f118e69..3b5bb62446 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -91,8 +91,8 @@ module Spree
has_many :store_credit_events, through: :store_credits, class_name: 'Spree::StoreCreditEvent'
has_many :taxonomies, class_name: 'Spree::Taxonomy'
- has_many :taxons, through: :taxonomies, class_name: 'Spree::Taxon'
- has_many :categories, through: :taxonomies, class_name: 'Spree::Category', source: :taxons
+ has_many :taxons, class_name: 'Spree::Taxon'
+ has_many :categories, class_name: 'Spree::Category'
has_many :store_promotions, class_name: 'Spree::StorePromotion'
has_many :promotions, through: :store_promotions, class_name: 'Spree::Promotion'