From e8c2fa576265281c71963b1dc3cfd3a4ca788f6b Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 31 Aug 2020 16:47:51 +0000 Subject: [PATCH] Bug 26326: Add Koha Objects for Import Records and Import Record Matches To test: 1 - Apply patches 2 - Update database 3 - Generate schema files (dbic) 4 - prove -v t/db_dependent/Koha/Import/Records.t 5 - prove -v t/db_dependent/Koha/Import/Record/Matches.t https://bugs.koha-community.org/show_bug.cgi?id=22785 --- Koha/Import/Record.pm | 44 ++++++++++++++++ Koha/Import/Record/Match.pm | 60 ++++++++++++++++++++++ Koha/Import/Record/Matches.pm | 65 ++++++++++++++++++++++++ Koha/Import/Records.pm | 50 ++++++++++++++++++ Koha/Schema/Result/ImportRecord.pm | 7 +++ Koha/Schema/Result/ImportRecordMatch.pm | 7 ++- installer/data/mysql/atomicupdate/bug_26326.perl | 12 +++++ installer/data/mysql/kohastructure.sql | 1 + t/db_dependent/Koha/Import/Record/Matches.t | 59 +++++++++++++++++++++ t/db_dependent/Koha/Import/Records.t | 46 +++++++++++++++++ 10 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 Koha/Import/Record.pm create mode 100644 Koha/Import/Record/Match.pm create mode 100644 Koha/Import/Record/Matches.pm create mode 100644 Koha/Import/Records.pm create mode 100644 installer/data/mysql/atomicupdate/bug_26326.perl create mode 100644 t/db_dependent/Koha/Import/Record/Matches.t create mode 100644 t/db_dependent/Koha/Import/Records.t diff --git a/Koha/Import/Record.pm b/Koha/Import/Record.pm new file mode 100644 index 0000000000..189d878dd8 --- /dev/null +++ b/Koha/Import/Record.pm @@ -0,0 +1,44 @@ +package Koha::Import::Record; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Import::Record - Koha Import Record Object class + +=head1 API + +=head2 Class methods + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ImportRecord'; +} + +1; diff --git a/Koha/Import/Record/Match.pm b/Koha/Import/Record/Match.pm new file mode 100644 index 0000000000..3389fe2db7 --- /dev/null +++ b/Koha/Import/Record/Match.pm @@ -0,0 +1,60 @@ +package Koha::Import::Record::Match; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; +use Koha::Import::Record; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Import::Record::Match - Koha Import Record Match Object class + +=head1 API + +=head2 Class methods + +=head3 import_record + +my $import_record = $match->import_record; + +Return the import record of this match + +=cut + +sub import_record { + my ( $self ) = @_; + my $record_rs = $self->_result->import_record; + return Koha::Import::Record->_new_from_dbic( $record_rs ); +} + + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ImportRecordMatch'; +} + +1; diff --git a/Koha/Import/Record/Matches.pm b/Koha/Import/Record/Matches.pm new file mode 100644 index 0000000000..d4a08ce212 --- /dev/null +++ b/Koha/Import/Record/Matches.pm @@ -0,0 +1,65 @@ +package Koha::Import::Record::Matches; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Import::Record::Match; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Import::Record::Matches - Koha Import Record Matches Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 unset_chosen + +Koha:Import::Record::Matches->search->unset_chosen(); + +Unselect all candidates as matches for the current resultset + +=cut + +sub unset_chosen { + my ( $self ) = @_; + while ( my $match = $self->next ){ + $match->chosen(0)->store; + } +} + +=head3 type + +=cut + +sub _type { + return 'ImportRecordMatch'; +} + +sub object_class { + return 'Koha::Import::Record::Match'; +} + +1; diff --git a/Koha/Import/Records.pm b/Koha/Import/Records.pm new file mode 100644 index 0000000000..3ed561866b --- /dev/null +++ b/Koha/Import/Records.pm @@ -0,0 +1,50 @@ +package Koha::Import::Records; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Import::Record; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Import::Records - Koha Import Record Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ImportRecord'; +} + +sub object_class { + return 'Koha::Import::Record'; +} + +1; diff --git a/Koha/Schema/Result/ImportRecord.pm b/Koha/Schema/Result/ImportRecord.pm index 8499e4bb86..b32c9fea6a 100644 --- a/Koha/Schema/Result/ImportRecord.pm +++ b/Koha/Schema/Result/ImportRecord.pm @@ -265,6 +265,13 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-04-18 10:50:48 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bqIAQzhgioWtBWU8zFdtjw +sub koha_object_class { + 'Koha::Import::Record'; +} +sub koha_objects_class { + 'Koha::Import::Records'; +} + # You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/Koha/Schema/Result/ImportRecordMatch.pm b/Koha/Schema/Result/ImportRecordMatch.pm index 0ea5d56914..1330ce42eb 100644 --- a/Koha/Schema/Result/ImportRecordMatch.pm +++ b/Koha/Schema/Result/ImportRecordMatch.pm @@ -72,6 +72,11 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hJW36EeP+H8+0/Ij3iuIFw +sub koha_object_class { + 'Koha::Import::Record::Match'; +} +sub koha_objects_class { + 'Koha::Import::Record::Matches'; +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/installer/data/mysql/atomicupdate/bug_26326.perl b/installer/data/mysql/atomicupdate/bug_26326.perl new file mode 100644 index 0000000000..b6a9c380fb --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_26326.perl @@ -0,0 +1,12 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + unless( + primary_key_exists('import_record_matches','import_record_id') && + primary_key_exists('import_record_matches','candidate_match_id') + ){ + $dbh->do( "ALTER TABLE import_record_matches DROP PRIMARY KEY" ); + $dbh->do( "ALTER TABLE import_record_matches ADD PRIMARY KEY (import_record_id,candidate_match_id)" ); + } + + NewVersion( $DBversion, 26326, "Add primary key to import_record_matches"); +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index ef3bff04b0..2cd118c444 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -787,6 +787,7 @@ CREATE TABLE `import_record_matches` ( -- matches found when importing a batch o `import_record_id` int(11) NOT NULL, -- the id given to the imported bib record (import_records.import_record_id) `candidate_match_id` int(11) NOT NULL, -- the biblio the imported record matches (biblio.biblionumber) `score` int(11) NOT NULL default 0, -- the match score + PRIMARY KEY (`import_record_id`,`candidate_match_id`), CONSTRAINT `import_record_matches_ibfk_1` FOREIGN KEY (`import_record_id`) REFERENCES `import_records` (`import_record_id`) ON DELETE CASCADE ON UPDATE CASCADE, KEY `record_score` (`import_record_id`, `score`) diff --git a/t/db_dependent/Koha/Import/Record/Matches.t b/t/db_dependent/Koha/Import/Record/Matches.t new file mode 100644 index 0000000000..87c16d94ac --- /dev/null +++ b/t/db_dependent/Koha/Import/Record/Matches.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 7; + +use Koha::Import::Record::Matches; +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $nb_of_matches = Koha::Import::Record::Matches->search->count; +my $nb_of_chosen_matches = Koha::Import::Record::Matches->search({ chosen => 1 })->count; + +my $match_1 = $builder->build({ source => 'ImportRecordMatch', value => { chosen => 1 } }); +my $match_2 = $builder->build({ source => 'ImportRecordMatch', value => { chosen => 1, import_record_id => $match_1->{import_record_id} } }); + +is( Koha::Import::Record::Matches->search->count, $nb_of_matches + 2, 'The 2 matches should have been added' ); + +is( Koha::Import::Record::Matches->search({chosen => 1 })->count, $nb_of_chosen_matches + 2, 'The 2 chosen matches should have been added' ); + +my $retrieved_match_1 = Koha::Import::Record::Matches->search({ import_record_id => $match_1->{import_record_id}, candidate_match_id => $match_1->{candidate_match_id} })->next; +is_deeply( $retrieved_match_1->unblessed, $match_1, 'Find a match by import record id and candidate should return the correct match' ); + +my $added_chose = Koha::Import::Record::Matches->search({ import_record_id => $match_1->{import_record_id} }); +$added_chose->unset_chosen(); + +is( Koha::Import::Record::Matches->search({chosen => 1 })->count, $nb_of_chosen_matches, 'The 2 added chosen matches should have been unchosen' ); + +$retrieved_match_1->delete; +is( Koha::Import::Record::Matches->search->count, $nb_of_matches + 1, 'Delete should have deleted the match' ); + +my $retrieved_match_2 = Koha::Import::Record::Matches->search({ import_record_id => $match_2->{import_record_id}, candidate_match_id => $match_2->{candidate_match_id} })->next; +my $import_record = $retrieved_match_2->import_record; +is( ref($import_record), 'Koha::Import::Record', "import_record has the correct class"); +is( $import_record->import_record_id, $retrieved_match_2->import_record_id, "We have the right record"); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Import/Records.t b/t/db_dependent/Koha/Import/Records.t new file mode 100644 index 0000000000..0c9974ebc8 --- /dev/null +++ b/t/db_dependent/Koha/Import/Records.t @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 3; + +use Koha::Import::Records; +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $nb_of_records = Koha::Import::Records->search->count; + +my $record_1 = $builder->build({ source => 'ImportRecord' }); +my $record_2 = $builder->build({ source => 'ImportRecord' }); + +is( Koha::Import::Records->search->count, $nb_of_records + 2, 'The 2 records should have been added' ); + +my $retrieved_record_1 = Koha::Import::Records->search({ import_record_id => $record_1->{import_record_id}})->next; +is_deeply( $retrieved_record_1->unblessed, $record_1, 'Find a record by import record id should return the correct record' ); + +$retrieved_record_1->delete; +is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' ); + +$schema->storage->txn_rollback; -- 2.11.0