Koha Solutions knowledge base: run log of a live check Article https://kohasolutions.com/knowledge-base/koha-add-library-branch Check scripts/kb/live/koha-add-library-branch.mjs (SHA-256 0a0ac3fc6a9700e255b02647ac1b1b3ae4b86dae8c5d8f4613ef90581e35955c) Ran 2026-11-05T14:30:20Z (UTC), 26 s Koha 26.05.02 (Koha::version 26.05.02.002, database version 26.0600002) Source https://git.koha-community.org/Koha-community/Koha/src/commit/d3fc229bc5eaea25dfe88f86149386408fe5d32d System Debian GNU/Linux 13 (trixie) Result PASS: all 23 claims held Each claim is something the article says. "seen" is what the check read back when it is not just a yes/no. Changes the check made to the test server were undone afterwards, as its header describes. == Claims == ok New library form: "Library code" and "Name" are the required fields seen: Library code:|Name: ok the code input takes at most 10 characters ok hint: "Note: Whitespace is not permitted. The use of hyphens, dashes, or other punctuation may cause searching issues." ok Pickup location and Public default to Yes ok IP hint: "Enter as a single IP address, or a subnet such as 192.168.1.*" ok the form has "Specific OPAC JS" and "Specific OPAC CSS" fields ok a space in the code → alert "The library code entered contains whitespace characters. Please remove any whitespace characters from the library code" seen: The library code entered contains whitespace characters. Please remove any whitespace characters from the library code ok …and nothing is saved ok Save → "Library added successfully." seen: Circulation | Patrons | Search | More | koha Centerville | | Search system preferences | Search system preferences | Check out | Search catalog | Home | Administration | Libraries | Help | Libra ok …stored with pickup_location 1 and public 1 ok the same code again → "An error occurred when adding this library. The branchcode might already exist." seen: Circulation | Patrons | Search | More | koha Centerville | | Search system preferences | Search system preferences | Check out | Search catalog | Home | Administration | Libraries | Help | An er ok editing: the code is plain text, not an input (it cannot be changed) ok deleting a library in use → "This library cannot be deleted. Patrons or items are still using it (N patrons and N items)." seen: This library cannot be deleted. Patrons or items are still using it (433 patrons and 976 items). | New libra [db: 433 patrons, 976 items] ok circulation rules: "Select a library" offers "Standard rules for all libraries" and the new library ok no Email on the library → notices come from KohaAdminEmailAddress seen: library@example.org|OSt ok no MARC organization code on the library → the MARCOrgCode preference is used seen: library@example.org|OSt ok baseline: "My library" logs koha in at Centerville seen: Centerville ok StaffLoginLibraryBasedOnIP on (the default): a library whose IP matches the computer becomes the logged-in library, even choosing "My library" seen: Kbverify Branch (library IP 172.19.0.*) ok StaffLoginRestrictLibraryByIP on: a library whose IP does not match is not offered in the login Library list ok …a library with no IP is still offered and works (My library → Centerville) seen: Centerville ok logging in at that library from another address → "Error: StaffLoginRestrictLibraryByIP is switched on and you are logging in with an IP address that doesn't match your library." seen: Koha | Error: Session timed out. | Please log in again | Error: StaffLoginRestrictLibraryByIP is switched on and you are logging in with an IP address that doesn't match your library. | Username: | Pa ok an unused library: "Are you sure you want to delete Kbverify Branch (KBVTEST)?" ok Yes, delete → "Library deleted successfully." seen: Circulation | Patrons | Search | More | koha Centerville | | Search system preferences | Search system preferences | Check out | Search catalog | Home | Administration | Libraries | Help | Libra == The check that produced this log (scripts/kb/live/koha-add-library-branch.mjs) == /** * Live check for prisma/ks-kb/64-koha-add-library-branch.ts (plan203 §A2 #19). Adds a throwaway library KBVTEST * "Kbverify Branch" through Administration → Libraries the way the article says (plus the whitespace and duplicate * failures), opens CPL's delete confirmation (refused, CPL has patrons and items: nothing is deleted), and tests what * the IP field does at the staff login with StaffLoginLibraryBasedOnIP (shipped on) and StaffLoginRestrictLibraryByIP. * The test browser reaches Koha from the docker gateway (172.19.0.1 here, read from Apache's log), so a library IP of * that /24 with .* matches it and 10.99.99.* does not. * Undone: KBVTEST deleted through the screen ("Library deleted successfully.", itself a claim), preferences restored. */ export const meta = { koha: '26.05.02', passed: '2026-11-05' } const CODE = 'KBVTEST' const NAME = 'Kbverify Branch' export default async function run(ctx) { const dropLibrary = () => ctx.sql(`DELETE FROM branches WHERE branchcode = '${CODE}'`) dropLibrary() ctx.restore('delete the KBVTEST library if it is still there', dropLibrary) ctx.setPref('StaffLoginRestrictLibraryByIP', '0') ctx.setPref('StaffLoginLibraryBasedOnIP', '1') const staff = await ctx.newPage() await ctx.staffLogin(staff) const L = `${ctx.STAFF}/cgi-bin/koha/admin/branches.pl` // 1. The form. await staff.goto(`${L}?op=add_form`) let t = await ctx.pageText(staff) ctx.check('New library form: "Library code" and "Name" are the required fields', (await staff.locator('label.required').allInnerTexts()).map((s) => s.trim()).join('|') === 'Library code:|Name:', (await staff.locator('label.required').allInnerTexts()).join('|')) ctx.check('the code input takes at most 10 characters', (await staff.locator('#branchcode').getAttribute('maxlength')) === '10') ctx.check('hint: "Note: Whitespace is not permitted. The use of hyphens, dashes, or other punctuation may cause searching issues."', t.includes('Note: Whitespace is not permitted. The use of hyphens, dashes, or other punctuation may cause searching issues.')) ctx.check('Pickup location and Public default to Yes', (await staff.locator('#pickup_location').inputValue()) === '1' && (await staff.locator('#public').inputValue()) === '1') ctx.check('IP hint: "Enter as a single IP address, or a subnet such as 192.168.1.*"', t.includes('Enter as a single IP address, or a subnet such as 192.168.1.*')) ctx.check('the form has "Specific OPAC JS" and "Specific OPAC CSS" fields', t.includes('Specific OPAC JS') && t.includes('Specific OPAC CSS')) // 2. A code with a space: a browser alert, nothing saved. let alertText = '' staff.removeAllListeners('dialog') staff.on('dialog', (d) => { alertText = d.message(); d.dismiss() }) await staff.fill('#branchcode', 'KBV TEST') await staff.fill('#branchname', NAME) await staff.locator('#Aform button[type=submit], #Aform input[type=submit]').first().click() await staff.waitForTimeout(800) ctx.check('a space in the code → alert "The library code entered contains whitespace characters. Please remove any whitespace characters from the library code"', alertText === 'The library code entered contains whitespace characters. Please remove any whitespace characters from the library code', alertText) ctx.check('…and nothing is saved', ctx.sqlValue("SELECT COUNT(*) FROM branches WHERE branchcode LIKE 'KBV%'") === '0') // 3. Add it. const add = async () => { await staff.goto(`${L}?op=add_form`) await staff.fill('#branchcode', CODE) await staff.fill('#branchname', NAME) await staff.locator('#Aform button[type=submit], #Aform input[type=submit]').first().click() await staff.waitForLoadState() return ctx.pageText(staff) } t = await add() ctx.check('Save → "Library added successfully."', t.includes('Library added successfully.'), t.slice(0, 200)) ctx.check('…stored with pickup_location 1 and public 1', ctx.sql(`SELECT pickup_location, public FROM branches WHERE branchcode = '${CODE}'`)[0]?.join('/') === '1/1') t = await add() ctx.check('the same code again → "An error occurred when adding this library. The branchcode might already exist."', t.includes('An error occurred when adding this library. The branchcode might already exist.'), t.slice(0, 200)) await staff.goto(`${L}?op=add_form&branchcode=${CODE}`) ctx.check('editing: the code is plain text, not an input (it cannot be changed)', (await staff.locator('input#branchcode').count()) === 0 && (await ctx.pageText(staff)).includes(CODE)) // 4. Delete refused while patrons and items use the library (CPL); the counts are this database's. const pc = ctx.sqlValue("SELECT COUNT(*) FROM borrowers WHERE branchcode = 'CPL'") const ic = ctx.sqlValue("SELECT COUNT(*) FROM items WHERE homebranch = 'CPL' OR holdingbranch = 'CPL'") await staff.goto(`${L}?op=delete_confirm&branchcode=CPL`) t = await ctx.pageText(staff) ctx.check('deleting a library in use → "This library cannot be deleted. Patrons or items are still using it (N patrons and N items)."', /This library cannot be deleted\. Patrons or items are still using it[\s|]*\(\d+ patrons and \d+ items\)\./.test(t) && Number(pc) > 0, (t.match(/This library cannot.{0,90}/) ?? [''])[0] + ` [db: ${pc} patrons, ${ic} items]`) // 5. New library uses the standard rules: it is offered in the circulation rules selector. await staff.goto(`${ctx.STAFF}/cgi-bin/koha/admin/smart-rules.pl`) t = await ctx.pageText(staff) ctx.check('circulation rules: "Select a library" offers "Standard rules for all libraries" and the new library', t.includes('Select a library') && (await staff.locator('#branch option', { hasText: 'Standard rules for all libraries' }).count()) === 1 && (await staff.locator(`#branch option[value="${CODE}"]`).count()) === 1) // 6. Email fallbacks. ctx.sql(`UPDATE branches SET branchemail = NULL, branchreplyto = NULL WHERE branchcode = '${CODE}'`) const perl = (code) => { ctx.sh(`cat > /tmp/kbv-br.pl <<'PL'\n${code}\nPL\nchmod 644 /tmp/kbv-br.pl`) return ctx.sh(`koha-shell ${ctx.INSTANCE} -c "perl /tmp/kbv-br.pl" 2>&1; rm -f /tmp/kbv-br.pl`).out.trim() } const admin = ctx.sqlValue("SELECT value FROM systempreferences WHERE variable = 'KohaAdminEmailAddress'") const from = perl(`use Koha::Libraries; my $l = Koha::Libraries->find('${CODE}'); print $l->from_email_address, "|", $l->get_effective_marcorgcode, "\\n";`) const orgcode = ctx.sqlValue("SELECT value FROM systempreferences WHERE variable = 'MARCOrgCode'") ctx.check('no Email on the library → notices come from KohaAdminEmailAddress', from.split('|')[0] === admin, from) ctx.check('no MARC organization code on the library → the MARCOrgCode preference is used', from.split('|')[1] === orgcode, from) // 7. The IP field at the staff login. const loginAs = async (branch) => { const p = await ctx.newPage() await p.goto(`${ctx.STAFF}/cgi-bin/koha/mainpage.pl`) const offered = (await p.locator(`#set-library-branch option[value="${CODE}"]`).count()) === 1 await p.fill('#userid', 'koha') await p.fill('#password', 'koha') if (branch && offered) await p.selectOption('#set-library-branch', branch) await p.click('#submit-button') await p.waitForLoadState() const text = await ctx.pageText(p) const lib = ((await p.locator('#logged-in-info-full .logged-in-branch-name, #logged-in-info-full brand').first().textContent().catch(() => '')) || '').trim() await p.context().close() return { offered, text, lib } } const before = await loginAs(null) ctx.check('baseline: "My library" logs koha in at Centerville', before.lib === 'Centerville', before.lib) // The address the test browser reaches Koha from (the docker gateway), read from Apache's log. const seenIp = ctx.sh("grep -v curl /var/log/apache2/other_vhosts_access.log | tail -1 | awk '{print $2}'").out.trim() const prefix = seenIp.split('.').slice(0, 3).join('.') + '.*' ctx.sql(`UPDATE branches SET branchip = ${ctx.q(prefix)} WHERE branchcode = '${CODE}'`) const auto = await loginAs(null) ctx.check('StaffLoginLibraryBasedOnIP on (the default): a library whose IP matches the computer becomes the logged-in library, even choosing "My library"', auto.lib === NAME, `${auto.lib} (library IP ${prefix})`) ctx.setPref('StaffLoginRestrictLibraryByIP', '1') ctx.sql(`UPDATE branches SET branchip = '10.99.99.*' WHERE branchcode = '${CODE}'`) ctx.flushCaches() const hidden = await loginAs(CODE) ctx.check('StaffLoginRestrictLibraryByIP on: a library whose IP does not match is not offered in the login Library list', !hidden.offered) ctx.check('…a library with no IP is still offered and works (My library → Centerville)', hidden.lib === 'Centerville', hidden.lib) // Choosing it anyway (a stale form, or a bookmark) gets the login error. const p = await ctx.newPage() await p.goto(`${ctx.STAFF}/cgi-bin/koha/mainpage.pl`) await p.evaluate((code) => { const s = document.querySelector('#set-library-branch'); const o = document.createElement('option'); o.value = code; s.appendChild(o); s.value = code }, CODE) await p.fill('#userid', 'koha') await p.fill('#password', 'koha') await p.click('#submit-button') await p.waitForLoadState() t = await ctx.pageText(p) ctx.check('logging in at that library from another address → "Error: StaffLoginRestrictLibraryByIP is switched on and you are logging in with an IP address that doesn\'t match your library."', t.includes("StaffLoginRestrictLibraryByIP is switched on and you are logging in with an IP address that doesn't match your library."), t.slice(0, 200)) await p.context().close() ctx.setPref('StaffLoginRestrictLibraryByIP', '0') ctx.sql(`UPDATE branches SET branchip = NULL WHERE branchcode = '${CODE}'`) // 8. Delete it again through the screen (no patrons or items). await staff.goto(`${L}?op=delete_confirm&branchcode=${CODE}`) t = await ctx.pageText(staff) ctx.check(`an unused library: "Are you sure you want to delete ${NAME} (${CODE})?"`, t.includes(`Are you sure you want to delete ${NAME} (${CODE})?`)) await staff.locator('button.approve').click() await staff.waitForLoadState() t = await ctx.pageText(staff) ctx.check('Yes, delete → "Library deleted successfully."', t.includes('Library deleted successfully.') && ctx.sqlValue(`SELECT COUNT(*) FROM branches WHERE branchcode = '${CODE}'`) === '0', t.slice(0, 200)) }