From d8051431bc3265bcb7252e4946b1c9022d2609c5 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Fri, 18 Mar 2016 14:30:50 +0100 Subject: [PATCH] Bug 15336 - Fix QA comments --- Koha/Acquisition/Bookseller.pm | 14 ++-- misc/migration_tools/merge_booksellers.pl | 6 +- t/Bookseller.t | 16 +---- t/db_dependent/Bookseller.t | 105 +++++++++++++++++++++++++++++- 4 files changed, 113 insertions(+), 28 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index 35d51b8..63d34f4 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -111,15 +111,13 @@ sub insert { return $self; } -sub merge_to { - my ($self, $mergeto) = @_; +sub merge_with { + my ($self, $booksellerid) = @_; - my $error = ''; my $result = {}; - unless (my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto})) { - $error = "Unknown target bookseller number: $mergeto"; - return ($error, {}); + unless (Koha::Acquisition::Bookseller->fetch({id => $booksellerid})) { + die("Unknown target bookseller number: $booksellerid"); } foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) { @@ -129,12 +127,12 @@ sub merge_to { $rs = $rs->search({$foreign_key => $self->{id} }); while (my $e = $rs->next) { - $e->set_column($foreign_key, $mergeto); + $e->set_column($foreign_key, $booksellerid); $e->update(); push @{ $result->{$entity} }, $e->id(); } } - return ($error, $result); + return $result; } # TODO Move code from ModBookseller diff --git a/misc/migration_tools/merge_booksellers.pl b/misc/migration_tools/merge_booksellers.pl index ec5156e..1862cf3 100644 --- a/misc/migration_tools/merge_booksellers.pl +++ b/misc/migration_tools/merge_booksellers.pl @@ -50,11 +50,7 @@ $schema->storage->txn_begin; my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom}); die "Unknown bookseller id ($mergefrom)" unless $from_bookseller; -my ($error, $report) = $from_bookseller->merge_to($mergeto); - -if ($error) { - die $error; -} +my $report = $from_bookseller->merge_with($mergeto); if ($confirm) { $schema->storage->txn_commit; diff --git a/t/Bookseller.t b/t/Bookseller.t index 92f7127..50776d2 100755 --- a/t/Bookseller.t +++ b/t/Bookseller.t @@ -1,19 +1,7 @@ #!/usr/bin/perl - -# 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 . +# This Koha test module is a stub! +# Add more tests here!!! use strict; use warnings; diff --git a/t/db_dependent/Bookseller.t b/t/db_dependent/Bookseller.t index 3fa5be3..5ac68e2 100644 --- a/t/db_dependent/Bookseller.t +++ b/t/db_dependent/Bookseller.t @@ -2,7 +2,7 @@ use Modern::Perl; -use Test::More tests => 88; +use Test::More tests => 101; use Test::MockModule; use Test::Warn; @@ -11,9 +11,12 @@ use Koha::DateUtils; use DateTime::Duration; use t::lib::Mocks; use C4::Acquisition; +use C4::Contract qw( GetContract ); +use C4::Items qw ( GetItem ); use C4::Serials; use C4::Budgets; use C4::Biblio; +use t::lib::TestBuilder; use Koha::Acquisition::Order; use Koha::Database; @@ -40,6 +43,8 @@ $schema->storage->txn_begin(); $dbh->{RaiseError} = 1; +our $builder = t::lib::TestBuilder->new; + #Start tests $dbh->do(q|DELETE FROM aqorders|); $dbh->do(q|DELETE FROM aqbasket|); @@ -746,6 +751,91 @@ is( $contacts->[0]->phone, is( scalar @$contacts, 1, 'Only one contact after modification' ); +my $bookseller1 = create('Aqbookseller'); +my $booksellerid1 = $bookseller1->{id}; +my $bookseller2 = create('Aqbookseller'); +my $booksellerid2 = $bookseller2->{id}; + +my $bg1 = create( 'Aqbasketgroup', $booksellerid1 ); +my $bg2 = create( 'Aqbasketgroup', $booksellerid1 ); +my $bg3 = create( 'Aqbasketgroup', $booksellerid2 ); + +my $basket1 = create( 'Aqbasket', $booksellerid1 ); +my $basket2 = create( 'Aqbasket', $booksellerid1 ); + +my $contract = create( 'Aqcontract', $booksellerid1 ); + +my $contact = create( 'Aqcontact', $booksellerid1 ); + +my $invoice = create( 'Aqinvoice', $booksellerid1 ); + +my $deleteditem = create( 'Deleteditem', $booksellerid1 ); + +my $item1 = create( 'Item', $booksellerid1 ); + +my $subscription = create( 'Subscription', $booksellerid1 ); + +$bookseller = Koha::Acquisition::Bookseller->fetch( { id => $booksellerid1 } ); + +my $result = eval { $bookseller->merge_with(345876876) }; +ok( + $@ =~ /^Unknown target bookseller number: 345876876/, + 'Merge to unknown bookseller return an error' +); + +$result = $bookseller->merge_with($booksellerid2); +my $expected_result = { + Aqbasketgroup => [ $bg1->{id}, $bg2->{id} ], + Aqbasket => [ $basket1->{basketno}, $basket2->{basketno} ], + Aqcontract => [ $contract->{contractnumber} ], + Aqcontact => [ $contact->{id} ], + Aqinvoice => [ $invoice->{invoiceid} ], + Deleteditem => [ $deleteditem->{itemnumber} ], + Item => [ $item1->{itemnumber} ], + Subscription => [ $subscription->{subscriptionid} ] +}; + +is_deeply( $result, $expected_result, 'Result should be as expected' ); + +is( GetBasketgroup( $bg1->{id} )->{'booksellerid'}, + $booksellerid2, 'Basket group 1 has bookseller 2' ); + +is( GetBasketgroup( $bg2->{id} )->{'booksellerid'}, + $booksellerid2, 'Basket group 2 has bookseller 2' ); + +is( GetBasketgroup( $bg3->{id} )->{'booksellerid'}, + $booksellerid2, 'Basket group 3 keep bookseller 2' ); + +is( GetBasket( $basket1->{basketno} )->{'booksellerid'}, + $booksellerid2, 'Basket 1 has bookseller 2' ); + +is( GetBasket( $basket2->{basketno} )->{'booksellerid'}, + $booksellerid2, 'Basket 2 has bookseller 2' ); + +is( + GetContract( { contractnumber => $contract->{contractnumber} } ) + ->{'booksellerid'}, + $booksellerid2, + 'Basket 1 has bookseller 2' +); + +is( C4::Bookseller::Contact->fetch( $contact->{id} )->{'booksellerid'}, + $booksellerid2, 'Contact 1 has booksellerid 2' ); + +is( GetInvoice( $invoice->{invoiceid} )->{'booksellerid'}, + $booksellerid2, 'Invoice has bookseller 2' ); + +my $item = Koha::Database->new()->schema->resultset('Deleteditem') + ->find( { itemnumber => $deleteditem->{itemnumber} } ); +is( $item->get_column('booksellerid'), + $booksellerid2, 'Deleted item has bookseller 2' ); + +is( GetItem( $item1->{itemnumber} )->{'booksellerid'}, + $booksellerid2, 'Item has bookseller 2' ); + +is( GetSubscription( $subscription->{subscriptionid} )->{'aqbooksellerid'}, + $booksellerid2, 'Subscription has bookseller 2' ); + #End transaction $schema->storage->txn_rollback(); @@ -769,3 +859,16 @@ sub field_filter { } return $struct; } + +sub create { + my ( $source, $booksellerid ) = @_; + + my $build = { source => $source }; + my $foreign_key = + $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; + $build->{value} = { $foreign_key => $booksellerid } if $booksellerid; + + my $entity = $builder->build($build); + + return $entity; +} -- 2.7.0