Kbverify theme<\\/p>/' ${HT}/${THEME}/en/modules/opac-main.tt && chown -R kohadev-koha: ${HT}/${THEME}`)
ctx.restore('remove the test theme folder', () => ctx.sh(`rm -rf ${HT}/${THEME}`))
await staff.goto(`${ctx.STAFF}/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=opacthemes`)
const opts = await staff.locator('select[name="pref_opacthemes"] option').allInnerTexts()
ctx.check(`a folder ${THEME}/en in the OPAC template directory appears as a choice for opacthemes`, opts.map((o) => o.trim()).includes(THEME), opts.join(', '))
ctx.setPref('OPACFallback', 'bootstrap')
ctx.setPref('opacthemes', THEME)
const themeMark = Number(ctx.sh('wc -l < /var/log/koha/kohadev/plack-opac-error.log').out)
const home = opac('/cgi-bin/koha/opac-main.pl')
ctx.check('opacthemes = the new theme: its own opac-main.tt is used', home.includes('id="kbv-theme"'))
ctx.check('…every page it does not contain comes from the fallback (bootstrap): search still works', opac('/cgi-bin/koha/opac-search.pl?q=a').includes('id="numresults"') || opac('/cgi-bin/koha/opac-search.pl?q=a').includes('results'))
ctx.check('…but its home page links no opac.css: CSS is looked up in the theme folder only, so a theme without css/ is unstyled', !/opac-tmpl\/[a-z]+\/css\/opac[_0-9]*\.css/.test(home), (home.match(/opac-tmpl\/[^"]*\.css/g) ?? []).join(' '))
const tlog = ctx.sh(`tail -n +${themeMark + 1} /var/log/koha/kohadev/plack-opac-error.log`).out
ctx.check('…plack-opac-error.log: "File not found : css/opac.css at …/Koha/Template/Plugin/Asset.pm" (and js/basket.js, js/Gettext.js…)', tlog.includes('File not found : css/opac.css') && tlog.includes('File not found : js/Gettext.js'), tlog.slice(0, 400))
ctx.sh(`cd ${HT}/${THEME} && ln -s ../bootstrap/css css && ln -s ../bootstrap/js js && ln -s ../bootstrap/images images`)
const fixed = opac('/cgi-bin/koha/opac-main.pl')
ctx.check(`with css, js and images linked to bootstrap's (ln -s ../bootstrap/css css …), the theme's page loads opac-tmpl/${THEME}/css/opac_….css`, new RegExp(`opac-tmpl/${THEME}/css/opac_[0-9.]+\\.css`).test(fixed) && fixed.includes('id="kbv-theme"'), (fixed.match(/opac-tmpl\/[^"]*opac[^"]*\.css/g) ?? []).join(' '))
ctx.setPref('opacthemes', 'bootstrap')
// 9. Private and maintenance.
ctx.setPref('OpacPublic', '0')
ctx.check('OpacPublic Disable: the OPAC home page asks to log in ("Log in to your account")', opac('/cgi-bin/koha/opac-main.pl').includes('Log in to your account'))
ctx.setPref('OpacPublic', '1')
ctx.setPref('OpacMaintenance', '1')
ctx.check('OpacMaintenance Show: every OPAC page shows "System maintenance" instead', opac('/cgi-bin/koha/opac-main.pl').includes('System maintenance') && opac(`/cgi-bin/koha/opac-detail.pl?biblionumber=${bib}`).includes('System maintenance'))
ctx.setPref('OpacMaintenance', '0')
// 9b. Logo, header and navigation links.
ctx.setPref('OPACUserCSS', '#logo { background-image: url("https://cdn.example.org/our-logo.png"); background-size: contain; width: 200px; } #logo a { width: 200px; padding-top: 50px; }')
await b.goto(`${ctx.OPAC}/cgi-bin/koha/opac-main.pl`)
const logo = await b.evaluate(() => { const s = getComputedStyle(document.querySelector('#logo')); return `${s.backgroundImage} ${s.width}` })
ctx.check('logo: an OPACUserCSS rule on #logo replaces the Koha logo with your image', logo.includes('https://cdn.example.org/our-logo.png') && logo.includes('200px'), logo)
ctx.setPref('OPACUserCSS', '')
add('html_customizations', 'opacheader', 'KBV_HEAD', '
Kbverify header strip
')
await b.goto(`${ctx.OPAC}/cgi-bin/koha/opac-search.pl?q=a`)
const headPos = await b.evaluate(() => { const h = document.querySelector('.kbv-head'); const n = document.querySelector('#header-region nav'); return h && n ? (n.compareDocumentPosition(h) & Node.DOCUMENT_POSITION_FOLLOWING ? 'after-nav' : 'before-nav') : 'missing' })
ctx.check('header: an opacheader HTML customization shows on every page (search results too), just under the top bar', headPos === 'after-nav', headPos)
add('html_customizations', 'OpacMoreSearches', 'KBV_MORE', 'Kbverify opening hours')
await b.goto(`${ctx.OPAC}/cgi-bin/koha/opac-search.pl?q=a`)
ctx.check('links: an OpacMoreSearches entry adds a link to the row under the search box (#moresearches), on every page', (await b.locator('#moresearches a', { hasText: 'Kbverify opening hours' }).count()) === 1)
add('html_customizations', 'OpacNavBottom', 'KBV_NAVB', '')
ctx.check('links: an OpacNavBottom entry shows in the left column of the home page', opac('/cgi-bin/koha/opac-main.pl').includes('Kbverify staff directory'))
// 9c. Quote of the day.
const qBefore = ctx.sql('SELECT id, COALESCE(timestamp, "NULL") FROM quotes')
ctx.restore('quotes: remove the test quotes and put back every Last display date', () => {
ctx.sql("DELETE FROM quotes WHERE source = 'Kbverify'")
for (const [id, ts] of qBefore) ctx.sql(`UPDATE quotes SET timestamp = ${ts === 'NULL' ? 'NULL' : `'${ts}'`} WHERE id = ${id}`)
})
ctx.sql("INSERT INTO quotes (source, text, timestamp) VALUES ('Kbverify', 'Kbverify quote one', NULL), ('Kbverify', 'Kbverify quote two', NULL)")
ctx.check('QuoteOfTheDay is empty on a new Koha (no quote shown anywhere)', ctx.sh("grep -E \"^\\('QuoteOfTheDay',\" /kohadevbox/koha/installer/data/mysql/mandatory/sysprefs.sql").out.includes("('QuoteOfTheDay','')"))
ctx.setPref('QuoteOfTheDay', 'opac')
const q1 = opac('/cgi-bin/koha/opac-main.pl')
const text1 = (q1.match(/id="daily-quote-text">([^<]*)) ?? [])[1] ?? ''
ctx.check('QuoteOfTheDay = OPAC: the home page shows "Quote of the day" with a quote', q1.includes('Quote of the day') && text1 !== '', text1)
const text2 = (opac('/cgi-bin/koha/opac-main.pl').match(/id="daily-quote-text">([^<]*)) ?? [])[1] ?? ''
ctx.check('…the same quote all day: a second visit shows the same one', text1 === text2, `${text1} / ${text2}`)
ctx.check("…because showing it stamps that quote's Last display with today", ctx.sqlValue(`SELECT DATE(timestamp) = CURDATE() FROM quotes WHERE text = ${ctx.q(text1)} ORDER BY timestamp DESC LIMIT 1`) === '1')
ctx.sql('UPDATE quotes SET timestamp = timestamp - INTERVAL 1 DAY WHERE DATE(timestamp) = CURDATE()')
const text3 = (opac('/cgi-bin/koha/opac-main.pl').match(/id="daily-quote-text">([^<]*)) ?? [])[1] ?? ''
ctx.check('…and the next day (no quote stamped today) Koha picks a new one at random by itself, no cron job', text3 !== '' && ctx.sqlValue('SELECT COUNT(*) FROM quotes WHERE DATE(timestamp) = CURDATE()') === '1', text3)
ctx.setPref('QuoteOfTheDay', '')
// 9d. New arrivals: a public report, read by OPACUserJS, and its cache.
const REPORT_SQL = NEW_ARRIVALS_SQL
ctx.sql(`INSERT INTO saved_sql (borrowernumber, date_created, last_modified, savedsql, report_name, cache_expiry, public) VALUES (51, NOW(), NOW(), ${ctx.q(REPORT_SQL)}, 'Kbverify new arrivals', 300, 1)`)
const rid = ctx.sqlValue("SELECT MAX(id) FROM saved_sql WHERE report_name = 'Kbverify new arrivals'")
ctx.restore('remove the test report', () => ctx.sql(`DELETE FROM saved_sql WHERE id = ${rid}`))
const perl = (code) => { ctx.sh(`cat > /tmp/kbv-oc.pl <<'PL'\n${code}\nPL\nchmod 644 /tmp/kbv-oc.pl`); return ctx.sh(`koha-shell ${ctx.INSTANCE} -c "perl /tmp/kbv-oc.pl" 2>&1; rm -f /tmp/kbv-oc.pl`).out.trim().split('\n').pop() }
const today = ctx.sqlValue('SELECT CURDATE()')
const newBib = (t) => Number(perl(`use C4::Biblio qw(AddBiblio); use MARC::Record; use Koha::Item; my $r = MARC::Record->new; $r->leader(' nam a22 7a 4500'); $r->append_fields(MARC::Field->new('245','1','0','a'=>'${t}')); my ($bn) = AddBiblio($r, ''); Koha::Item->new({ biblionumber => $bn, homebranch => 'CPL', holdingbranch => 'CPL', itype => 'BK', dateaccessioned => '${today}' })->store; print "$bn\\n";`))
const delBib = (bn) => perl(`use C4::Biblio qw(DelBiblio); use Koha::Biblios; my $b = Koha::Biblios->find(${bn}); $_->safe_delete for $b ? $b->items->as_list : (); DelBiblio(${bn}); print "ok\\n";`)
const made = []
ctx.restore('remove the new-arrival test records', () => { for (const bn of made) { delBib(bn); ctx.sql(`DELETE FROM deleteditems WHERE biblionumber = ${bn}`); ctx.sql(`DELETE FROM deletedbiblio_metadata WHERE biblionumber = ${bn}`); ctx.sql(`DELETE FROM deletedbiblioitems WHERE biblionumber = ${bn}`); ctx.sql(`DELETE FROM deletedbiblio WHERE biblionumber = ${bn}`) } })
const svc = () => JSON.parse(ctx.sh(`curl -s '${ctx.OPAC}/cgi-bin/koha/svc/report?id=${rid}&annotated=1'`).out || '[]')
const one = newBib('Kbverify new arrival one'); made.push(one)
ctx.flushCaches()
let rows = svc()
ctx.check('the public report at /cgi-bin/koha/svc/report?id=…&annotated=1 returns the newest items as JSON (biblionumber, title, author, added)', rows[0]?.title === 'Kbverify new arrival one' && 'biblionumber' in rows[0] && 'added' in rows[0], JSON.stringify(rows[0]))
ctx.check("…20 rows: the report's own LIMIT 20 decides how many", rows.length === 20, rows.length)
ctx.sql(`UPDATE saved_sql SET savedsql = ${ctx.q(REPORT_SQL.replace(/\nLIMIT 20$/, ''))} WHERE id = ${rid}`)
ctx.flushCaches()
const noLimit = svc().length
ctx.sql(`UPDATE saved_sql SET savedsql = ${ctx.q(REPORT_SQL)} WHERE id = ${rid}`)
ctx.flushCaches()
rows = svc()
ctx.check('…without a LIMIT the service returns only 10 (SvcMaxReportRows, default 10)', noLimit === 10, noLimit)
const two = newBib('Kbverify new arrival two'); made.push(two)
rows = svc()
ctx.check('STUCK: a book added after the first request does not appear (the JSON is cached for the report\'s 300 seconds)', !rows.some((r) => r.title === 'Kbverify new arrival two'), rows.slice(0, 2).map((r) => r.title).join(' | '))
delBib(one)
rows = svc()
ctx.check('OLD BOOKS: a deleted book is still in the slider until the cache expires', rows.some((r) => r.title === 'Kbverify new arrival one'), rows.slice(0, 2).map((r) => r.title).join(' | '))
ctx.sql(`UPDATE saved_sql SET cache_expiry = 1 WHERE id = ${rid}`)
ctx.flushCaches()
rows = svc()
ctx.check('fix: with the cache cleared (or expired), the deleted book is gone and the new one is first', rows[0]?.title === 'Kbverify new arrival two' && !rows.some((r) => r.title === 'Kbverify new arrival one'), rows.slice(0, 2).map((r) => r.title).join(' | '))
const three = newBib('Kbverify new arrival three'); made.push(three)
await new Promise((r) => setTimeout(r, 2500))
rows = svc()
ctx.check('fix: with the report\'s cache expiry set low, a newly added book appears on its own within that time', rows[0]?.title === 'Kbverify new arrival three', rows.slice(0, 2).map((r) => r.title).join(' | '))
ctx.check('a withdrawn copy is left out by the report (withdrawn = 0)', (() => { ctx.sql(`UPDATE items SET withdrawn = 1 WHERE biblionumber = ${three}`); ctx.flushCaches(); const r = svc(); ctx.sql(`UPDATE items SET withdrawn = 0 WHERE biblionumber = ${three}`); return !r.some((x) => x.title === 'Kbverify new arrival three') })())
ctx.flushCaches()
add('html_customizations', 'OpacMainUserBlock', 'KBV_SLIDER', SLIDER_HTML)
ctx.setPref('OPACUserCSS', SLIDER_CSS)
ctx.setPref('OPACUserJS', SLIDER_JS.replace('REPORT_ID', rid))
await b.goto(`${ctx.OPAC}/cgi-bin/koha/opac-main.pl`)
await b.waitForSelector('#new-arrivals a', { timeout: 15000 }).catch(() => {})
const cards = await b.locator('#new-arrivals a').allInnerTexts()
const flex = await b.evaluate(() => { const e = document.querySelector('#new-arrivals'); return e ? `${getComputedStyle(e).display} ${getComputedStyle(e).overflowX}` : '' })
ctx.check('the slider: the OPACUserJS code fills #new-arrivals with one link per book, newest first, in a scrolling row', cards.length === 20 && cards[0].includes('Kbverify new arrival three') && flex === 'flex auto', `${cards.length} cards, first "${cards[0]}", ${flex}`)
const href = await b.locator('#new-arrivals a').first().getAttribute('href')
ctx.check("…each card links to the book's page", href === `/cgi-bin/koha/opac-detail.pl?biblionumber=${three}`, href)
ctx.setPref('OPACUserJS', '')
ctx.setPref('OPACUserCSS', '')
// 10. Plugins add to every OPAC page through two hooks.
const hooks = ctx.sh(`grep -rn "KohaPlugins.get_plugins_opac_head\\|KohaPlugins.get_plugins_opac_js" ${HT}/bootstrap/en/includes/ | wc -l`).out
ctx.check('the OPAC includes call KohaPlugins.get_plugins_opac_head and get_plugins_opac_js', Number(hooks) >= 2, hooks)
// 11. What the package owns (an upgrade replaces it), on a real package install.
if (spawnSync('docker', ['image', 'inspect', IMG]).status !== 0) { ctx.check(`image ${IMG} present`, false); return }
const r = spawnSync('docker', ['run', '--rm', IMG, 'bash', '-c', `{ dpkg -S /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/modules/opac-main.tt; dpkg -S /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl; mkdir -p /usr/share/koha/opac/htdocs/opac-tmpl/mytheme/en; dpkg -S /usr/share/koha/opac/htdocs/opac-tmpl/mytheme; dpkg -s koha-common | grep ^Version; } 2>&1`], { encoding: 'utf8' })
const out = (r.stdout ?? '').trim()
ctx.check('package install: bootstrap templates and the default XSLT belong to koha-common (an upgrade overwrites them)',
out.includes('koha-common: /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/modules/opac-main.tt') && out.includes('koha-common: /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl'), out)
ctx.check('…and a theme folder of your own belongs to no package: "dpkg-query: no path found matching pattern …/mytheme"', out.includes('dpkg-query: no path found matching pattern /usr/share/koha/opac/htdocs/opac-tmpl/mytheme'), out)
ctx.check('…on koha-common 26.05.05', /Version: 26\.05\.05/.test(out), out)
const T = '/usr/share/koha/opac/htdocs/opac-tmpl'
const r2 = spawnSync('docker', ['run', '--rm', IMG, 'bash', '-c', `{ ${THEME_CMDS}\necho ---; ${XSLT_CMDS}\n} 2>&1`], { encoding: 'utf8' })
const out2 = (r2.stdout ?? '').trim()
ctx.check('the article\'s theme commands, on a package install: the folder holds one template and three links to bootstrap',
/lrwxrwxrwx .* css -> \.\.\/bootstrap\/css/.test(out2) && /lrwxrwxrwx .* js -> \.\.\/bootstrap\/js/.test(out2) && /lrwxrwxrwx .* images -> \.\.\/bootstrap\/images/.test(out2) && out2.includes(`${T}/mytheme/en/modules/opac-main.tt`), out2)
ctx.check('the article\'s XSLT commands: the copy now imports MARC21slimUtils.xsl by its full path',
out2.includes(``), out2)
ctx._boxOut = out2
}