From bc151f145ee04712c6e9c1fad79bf9e313568f03 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 17 Jul 2025 14:48:34 +0200 Subject: [PATCH] Bug 40430: Fix Toolbar_spec.ts See comment 1 on the bug. We are adding the capability to insert vendors and baskets. Test plan: Confirm that yarn cypress run t/cypress/integration/Acquisitions/Vendors_spec.ts pass, even if you do not have any vendors in DB. --- .../integration/Acquisitions/Vendors_spec.ts | 84 ++++++++++++++----- t/cypress/plugins/insertData.js | 61 ++++++++++++++ 2 files changed, 122 insertions(+), 23 deletions(-) diff --git a/t/cypress/integration/Acquisitions/Vendors_spec.ts b/t/cypress/integration/Acquisitions/Vendors_spec.ts index 8118b99128b..f39079932d3 100644 --- a/t/cypress/integration/Acquisitions/Vendors_spec.ts +++ b/t/cypress/integration/Acquisitions/Vendors_spec.ts @@ -232,32 +232,70 @@ describe("Vendor CRUD operations", () => { .contains("Vendor") .contains("deleted"); }); +}); - it("receive should open in the same tab", () => { - const vendor = cy.getVendor(); - vendor.baskets_count = 1; +describe("Vendor module", () => { + beforeEach(() => { + cy.login(); + cy.title().should("eq", "Koha staff interface"); - cy.intercept("GET", "/api/v1/acquisitions/vendors\?*", { - statusCode: 200, - body: [vendor], - headers: { - "X-Base-Total-Count": "1", - "X-Total-Count": "1", - }, - }); - cy.intercept( - "GET", - new RegExp("/api/v1/acquisitions/vendors/(?!config$).+"), - vendor - ); + cy.task("buildSampleObject", { + object: "vendor", + values: { active: 1 }, + }) + .then(generatedVendor => { + delete generatedVendor.list_currency; + delete generatedVendor.invoice_currency; + return cy.task("insertObject", { + type: "vendor", + object: generatedVendor, + }); + }) + .then(vendor => { + cy.wrap(vendor).as("vendor"); + return cy.task("buildSampleObject", { + object: "basket", + values: { vendor_id: vendor.id }, + }); + }) + .then(generatedBasket => { + return cy.task("insertObject", { + type: "basket", + object: generatedBasket, + }); + }) + .then(basket => { + cy.wrap(basket).as("basket"); + }); + }); + + afterEach(function () { + cy.task("deleteSampleObjects", [ + { vendor: this.vendor, basket: this.basket }, + ]); + }); + + it("receive should open in the same tab", function () { cy.visit("/cgi-bin/koha/acquisition/vendors"); - cy.get("#vendors_list table tbody tr:first") - .contains("Receive shipments") - .click(); - cy.url().should( - "contain", - "/cgi-bin/koha/acqui/parcels.pl?booksellerid=1" - ); + // table_id is currently 'DataTables_Table_0', and it should be fixed + cy.get("#vendors_list table.dataTable") + .invoke("attr", "id") + .then(table_id => { + cy.intercept("GET", "/api/v1/acquisitions/vendors*").as( + "get-vendors" + ); + cy.get(`#${table_id}_wrapper input.dt-input`).type( + this.vendor.name + ); + cy.wait("@get-vendors"); + cy.get(`#${table_id} tbody tr:first`) + .contains("Receive shipments") + .click(); + cy.url().should( + "contain", + `/cgi-bin/koha/acqui/parcels.pl?booksellerid=${this.vendor.id}` + ); + }); }); }); diff --git a/t/cypress/plugins/insertData.js b/t/cypress/plugins/insertData.js index 85915306098..4b522b2dc9a 100644 --- a/t/cypress/plugins/insertData.js +++ b/t/cypress/plugins/insertData.js @@ -333,6 +333,8 @@ const deleteSampleObjects = async allObjects => { hold: "holds", checkout: "checkouts", old_checkout: "old_checkouts", + basket: "baskets", + vendor: "vendors", patron: "patrons", item: "items", biblio: "biblios", @@ -360,6 +362,8 @@ const deleteSampleObjects = async allObjects => { "holds", "checkouts", "old_checkouts", + "baskets", + "vendors", "patrons", "items", "biblios", @@ -431,6 +435,20 @@ const deleteSampleObjects = async allObjects => { values: ids, }); break; + case "baskets": + ids = objects.map(i => i.basket_id); + await query({ + sql: `DELETE FROM aqbasket WHERE basketno IN (${ids.map(() => "?").join(",")})`, + values: ids, + }); + break; + case "vendors": + ids = objects.map(i => i.id); + await query({ + sql: `DELETE FROM aqbooksellers WHERE id IN (${ids.map(() => "?").join(",")})`, + values: ids, + }); + break; default: throw Error( `Not implemented yet: cannot deleted object '${type}'` @@ -544,6 +562,49 @@ const insertObject = async ({ type, object, baseUrl, authHeader }) => { baseUrl, authHeader, }); + } else if (type == "vendor") { + const { + id, + baskets_count, + invoices_count, + subscriptions_count, + external_id, + aliases, + baskets, + contacts, + contracts, + interfaces, + invoices, + ...vendor + } = object; + + let endpoint = "/api/v1/acquisitions/vendors"; + + return apiPost({ + endpoint, + body: vendor, + baseUrl, + authHeader, + }); + } else if (type == "basket") { + const keysToKeep = ["name", "vendor_id", "close_date"]; + const basket = Object.fromEntries( + Object.entries(object).filter(([key]) => keysToKeep.includes(key)) + ); + return query({ + sql: "INSERT INTO aqbasket(basketname, booksellerid, closedate) VALUES (?, ?, ?)", + values: [basket.name, basket.vendor_id, basket.close_date], + }) + .then(result => { + const basket_id = result.insertId; + // FIXME We need /acquisitions/baskets/:basket_id + return apiGet({ + endpoint: `/api/v1/acquisitions/baskets?q={"basket_id":"${basket_id}"}`, + baseUrl, + authHeader, + }); + }) + .then(baskets => baskets[0]); } else { throw Error(`Unsupported object type '${type}' to insert`); } -- 2.34.1