From 1c45c864b80347c753e76598a3c58ab4e4f4f53e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 6 Apr 2017 16:09:46 -0300 Subject: [PATCH] Bug 18403: Add new method Koha::Library->library_group This is more a follow-up for bug 15707. It could be moved on its own bug report if necessary. Test plan: prove t/db_dependent/LibraryGroups.t should return green Signed-off-by: Jon McGowan --- Koha/Library.pm | 12 ++++++++++++ t/db_dependent/LibraryGroups.t | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Koha/Library.pm b/Koha/Library.pm index 786f457..304f33b 100644 --- a/Koha/Library.pm +++ b/Koha/Library.pm @@ -35,6 +35,18 @@ Koha::Library - Koha Library Object class =cut +=head3 group + +Return the Library groups of this library + +=cut + +sub library_groups { + my ( $self ) = @_; + my $rs = $self->_result->library_groups; + return Koha::Library::Groups->_new_from_dbic( $rs ); +} + =head3 type =cut diff --git a/t/db_dependent/LibraryGroups.t b/t/db_dependent/LibraryGroups.t index c3aef9f..e6e0fd4 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 => 13; +use Test::More tests => 14; use t::lib::TestBuilder; @@ -51,8 +51,10 @@ ok( $in_list, 'New root group is in the list returned by the get_root_groups met my $groupA = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group A' })->store(); my $groupA1 = Koha::Library::Group->new({ parent_id => $groupA->id, title => 'Group A1' })->store(); my $groupA2 = Koha::Library::Group->new({ parent_id => $groupA->id, title => 'Group A2' })->store(); +my $groupB = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group B' })->store(); my $groupA_library1 = Koha::Library::Group->new({ parent_id => $groupA->id, branchcode => $library1->{branchcode} })->store(); +my $groupB_library1 = Koha::Library::Group->new({ parent_id => $groupB->id, branchcode => $library1->{branchcode} })->store(); my $groupA1_library2 = Koha::Library::Group->new({ parent_id => $groupA1->id, branchcode => $library2->{branchcode} })->store(); my @children = $root_group->children()->as_list(); @@ -77,3 +79,16 @@ $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_ ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 1 is not in the list'); $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children; ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 2 is in the list'); + +subtest 'Koha::Library->library_groups' => sub { + plan tests => 4; + my $library3 = Koha::Libraries->find( $library3->{branchcode} ); + my $groups = $library3->library_groups; + is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' ); + is( $groups->count, 0, 'Library 3 should not be part of any groups'); + + my $library1 = Koha::Libraries->find( $library1->{branchcode} ); + $groups = $library1->library_groups; + is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' ); + is( $groups->count, 2, 'Library 1 should be part of 2 groups' ); +}; -- 2.1.4