From 30f7eeb48fb7f4194e55864040e4ee8425916536 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Tue, 22 Dec 2020 15:41:53 +0100 Subject: [PATCH] Bug 19220: Allow XSLT processing for Z39.50 authority targets Test plan: 1) Apply the patch 2) Edit an authority Z3950/SRU source in Home > Administration > Z39.50/SRU servers 3) Add the path to an XSLT file in the "XSLT File(s) for transforming results" input, and save For instance: /koha-tmpl/intranet-tmpl/prog/en/xslt/Bug19220.xsl 4) Search for an authority with the "New from Z39.50/SRU" button in authorities home 5) Check that the XSLT transformation has been applied, both in results list and in the import window 6) Edit the Z3950/SRU source to remove the path to the XSLT file 7) Search again for the same authority, and check that no transformation has been applied 8) prove t/db_dependent/Breeding_Auth.t Here is an example XSLT which adds a 035$a field: XSLT added field Signed-off-by: Martin Renvoize --- C4/Breeding.pm | 12 +++++++++++- t/db_dependent/Breeding_Auth.t | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/C4/Breeding.pm b/C4/Breeding.pm index 8dbaae9fff3..15306f142a9 100644 --- a/C4/Breeding.pm +++ b/C4/Breeding.pm @@ -477,12 +477,18 @@ sub ImportBreedingAuth { my $batch_id = GetZ3950BatchId($filename); my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?"); + my $marc_type = C4::Context->preference('marcflavour'); my $controlnumber = $marcrecord->field('001')->data; $searchbreeding->execute($controlnumber,$heading); my ($breedingid) = $searchbreeding->fetchrow; - return $breedingid if $breedingid; + if ($breedingid) { + my $updbrd = $dbh->prepare("update import_records set marc=?,marcxml=? where import_record_id=?"); + $updbrd->execute( $marcrecord->as_usmarc(), $marcrecord->as_xml($marc_type), $breedingid ); + $updbrd->finish(); + return $breedingid; + } $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding); return $breedingid; } @@ -520,6 +526,7 @@ sub Z3950SearchAuth { my $query; my $nterms=0; + my $xslh = Koha::XSLT::Base->new; my $marcflavour = C4::Context->preference('marcflavour'); my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour; my $authid= $pars->{authid}; @@ -594,6 +601,9 @@ sub Z3950SearchAuth { $marcrecord->encoding('UTF-8'); SetUTF8Flag($marcrecord); + my $error; + ( $marcrecord, $error ) = _do_xslt_proc( $marcrecord, $servers[$k], $xslh ); + my $heading_authtype_code = GuessAuthTypeCode($marcrecord) or next; my $heading = GetAuthorizedHeading({ record => $marcrecord }); my $breedingid = ImportBreedingAuth( $marcrecord, $servers[$k]->{host}, 'UTF-8', $heading ); diff --git a/t/db_dependent/Breeding_Auth.t b/t/db_dependent/Breeding_Auth.t index b019bb127d8..3a718240cc7 100755 --- a/t/db_dependent/Breeding_Auth.t +++ b/t/db_dependent/Breeding_Auth.t @@ -91,7 +91,8 @@ sub mock_objects { $schema->storage->txn_begin; subtest ImportBreedingAuth => sub { - plan tests => 4; + plan tests => 6; + my $dbh = C4::Context->dbh; my $record = MARC::Record->new(); $record->append_fields( @@ -101,8 +102,28 @@ subtest ImportBreedingAuth => sub { my $breedingid = C4::Breeding::ImportBreedingAuth( $record, "kidclamp", "UTF-8", 'Jansson, Tove' ); ok( $breedingid, "We got a breeding id back" ); + my $breedingid_1 = C4::Breeding::ImportBreedingAuth( $record, "kidclamp", "UTF-8", 'Jansson, Tove' ); is( $breedingid, $breedingid_1, "For the same record, we get the same id" ); + + # Bug 19220: In case of XSLT transformation, we make sure to have the new version of the record + $record->append_fields( + MARC::Field->new( '999', ' ', ' ', a => 'Added field' ), + ); + + $breedingid_1 = C4::Breeding::ImportBreedingAuth( $record, "kidclamp", "UTF-8", 'Jansson, Tove' ); + is( $breedingid, $breedingid_1, "For the same record, modified, we get the same id" ); + + my $sth = $dbh->prepare('SELECT * FROM import_records WHERE import_record_id = ?'); + $sth->execute($breedingid); + my $imported_records_record = $sth->fetchrow_hashref(); + my $imported_record = MARC::Record::new_from_xml( $imported_records_record->{marcxml} ); + is( + $imported_record->subfield( '999', 'a' ), $record->subfield( '999', 'a' ), + 'We also get the new the version of the record' + ); + # End Bug 19220 + $breedingid_1 = C4::Breeding::ImportBreedingAuth( $record, "marcelr", "UTF-8", 'Jansson, Tove' ); is( $breedingid, $breedingid_1, "For the same record in a different file, we get a new id" ); my $record_1 = MARC::Record->new(); -- 2.47.0