From 1a8f132cab7070ed35da921daaaa2dd1e6accb83 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Sun, 24 Mar 2019 23:00:36 -0300 Subject: [PATCH] Bug 22284: New methods in Koha::Library::Groups and Koha::Library This patch adds new methods in Koha::Library::Groups and Koha::Library. 1) For Koha::Library::Groups adds get_root_ancestor that returns all root groups for a given search parameters, for example Koha::Library::Groups->get_root_ancestor( { id => $group_id } ) 2) For Koha::Library adds 2.1) get_hold_libraries: returns all libraries (including self) that belongs to the same holdgroups. If $self belongs to several holdgroups it will return a distinct list of all libraries belonging to them. 2.2) validate_hold_sibling: Returns 1 if the given parameters matches any of the libraries that belong to any of the holdgroups this library belongs. For example $library->validate_hold_sibling( { branchcode => $branchcode } ) To test: 1) apply this patch 2) prove t/db_dependent/Koha/Libraries.t t/db_dependent/LibraryGroups.t SUCCESS => green letters :-D 3) Sign off Sponsored-by: VOKAL Signed-off-by: Tomas Cohen Arazi Signed-off-by: Josef Moravec Signed-off-by: Liz Rea --- Koha/Library.pm | 51 +++++++++++++++++++++++++++++++++++++++++ Koha/Library/Groups.pm | 17 ++++++++++++++ t/db_dependent/Koha/Libraries.t | 35 +++++++++++++++++++++++++++- t/db_dependent/LibraryGroups.t | 21 ++++++++++++++++- 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/Koha/Library.pm b/Koha/Library.pm index f1a5597305..7f55ff8075 100644 --- a/Koha/Library.pm +++ b/Koha/Library.pm @@ -77,6 +77,57 @@ sub library_groups { return Koha::Library::Groups->_new_from_dbic( $rs ); } +=head3 get_hold_libraries + +Return all libraries (including self) that belong to the same hold groups + +=cut + +sub get_hold_libraries { + my ( $self ) = @_; + my $library_groups = $self->library_groups; + my @hold_libraries; + while ( my $library_group = $library_groups->next ) { + my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id}); + if($root->ft_local_hold_group) { + push @hold_libraries, $root->all_libraries; + } + } + + my %seen; + @hold_libraries = + grep { !$seen{ $_->id }++ } @hold_libraries; + + return @hold_libraries; +} + +=head3 validate_hold_sibling + +Return if given library is a valid hold group member + +=cut + +sub validate_hold_sibling { + my ( $self, $params ) = @_; + my @hold_libraries = $self->get_hold_libraries; + + foreach (@hold_libraries) { + my $hold_library = $_; + my $is_valid = 1; + foreach my $key (keys %$params) { + unless($hold_library->$key eq $params->{$key}) { + $is_valid=0; + last; + } + } + if($is_valid) { + #Found one library that meets all search parameters + return 1; + } + } + return 0; +} + =head2 Internal methods =head3 _type diff --git a/Koha/Library/Groups.pm b/Koha/Library/Groups.pm index 1ce2cb019a..ac0b6dc2ad 100644 --- a/Koha/Library/Groups.pm +++ b/Koha/Library/Groups.pm @@ -65,6 +65,23 @@ sub get_search_groups { return $self->search( { $field => 1 } ); } + +=head3 get_root_ancestor + +my $root_ancestor = $self->get_root_ancestor( {id => $group_id } ) + +Retrieve root ancestor group for a specified id. + +=cut + +sub get_root_ancestor { + my ( $self, $params ) = @_; + my $row = $self->find($params); + return $row unless $row->parent_id; + return $self->get_root_ancestor( { id => $row->parent_id } ); +} + + =head3 type =cut diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t index 5c356c1998..0b2f7271dd 100644 --- a/t/db_dependent/Koha/Libraries.t +++ b/t/db_dependent/Koha/Libraries.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use C4::Biblio; use C4::Context; @@ -451,3 +451,36 @@ subtest '->get_effective_marcorgcode' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_hold_libraries and validate_hold_sibling' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library3 = $builder->build_object( { class => 'Koha::Libraries' } ); + + my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } ); + my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } ); + my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } ); + + my @hold_libraries = ($library1, $library2); + + my @result = $library1->get_hold_libraries(); + + ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries'); + + my %map = map {$_->branchcode, 1} @result; + + foreach my $hold_library ( @hold_libraries ) { + ok(exists $map{$hold_library->branchcode}, 'library in hold group'); + } + + ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling'); + ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling'); + + $schema->storage->txn_rollback; + +} \ No newline at end of file diff --git a/t/db_dependent/LibraryGroups.t b/t/db_dependent/LibraryGroups.t index 69f9e9d4b7..26824210e8 100644 --- a/t/db_dependent/LibraryGroups.t +++ b/t/db_dependent/LibraryGroups.t @@ -4,7 +4,7 @@ use Modern::Perl; use List::MoreUtils 'any'; -use Test::More tests => 20; +use Test::More tests => 21; use t::lib::TestBuilder; use Koha::Database; @@ -144,3 +144,22 @@ is( ref($groupX->libraries), 'Koha::Libraries', '->libraries should return a Koh @group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries ); is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" ); is( ref(($groupX->all_libraries)[0]), 'Koha::Library', '->all_libraries should return a list of Koha::Library - in the future it should be fixed to return a Koha::Libraries iterator instead'); # FIXME + +subtest 'Koha::Library::Groups->get_root_ancestor' => sub { + plan tests => 2; + + my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store(); + my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id, branchcode => $library1->{branchcode} })->store(); + my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store(); + my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id, branchcode => $library2->{branchcode} })->store(); + my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store(); + my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id, title => "Group Z1" })->store(); + my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id, title => "Group Z2" })->store(); + my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id, branchcode => $library2->{branchcode} })->store(); + + my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed); + my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed); + + is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor"); + ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids"); +}; \ No newline at end of file -- 2.11.0