View | Details | Raw Unified | Return to bug 22284
Collapse All | Expand All

(-)a/Koha/Library.pm (+51 lines)
Lines 77-82 sub library_groups { Link Here
77
    return Koha::Library::Groups->_new_from_dbic( $rs );
77
    return Koha::Library::Groups->_new_from_dbic( $rs );
78
}
78
}
79
79
80
=head3 get_hold_libraries
81
82
Return all libraries (including self) that belong to the same hold groups
83
84
=cut
85
86
sub get_hold_libraries {
87
    my ( $self ) = @_;
88
    my $library_groups = $self->library_groups;
89
    my @hold_libraries;
90
    while ( my $library_group = $library_groups->next ) {
91
        my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
92
        if($root->ft_local_hold_group) {
93
            push @hold_libraries, $root->all_libraries;
94
        }
95
    }
96
97
    my %seen;
98
    @hold_libraries =
99
      grep { !$seen{ $_->id }++ } @hold_libraries;
100
101
    return @hold_libraries;
102
}
103
104
=head3 validate_hold_sibling
105
106
Return if given library is a valid hold group member
107
108
=cut
109
110
sub validate_hold_sibling {
111
    my ( $self, $params ) = @_;
112
    my @hold_libraries = $self->get_hold_libraries;
113
114
    foreach (@hold_libraries) {
115
        my $hold_library = $_;
116
        my $is_valid = 1;
117
        foreach my $key (keys %$params) {
118
            unless($hold_library->$key eq $params->{$key}) {
119
                $is_valid=0;
120
                last;
121
            }
122
        }
123
        if($is_valid) {
124
            #Found one library that meets all search parameters
125
            return 1;
126
        }
127
    }
128
    return 0;
129
}
130
80
=head2 Internal methods
131
=head2 Internal methods
81
132
82
=head3 _type
133
=head3 _type
(-)a/Koha/Library/Groups.pm (+17 lines)
Lines 65-70 sub get_search_groups { Link Here
65
    return $self->search( { $field => 1 } );
65
    return $self->search( { $field => 1 } );
66
}
66
}
67
67
68
69
=head3 get_root_ancestor
70
71
my $root_ancestor = $self->get_root_ancestor( {id => $group_id } )
72
73
Retrieve root ancestor group for a specified id.
74
75
=cut
76
77
sub get_root_ancestor {
78
    my ( $self, $params ) = @_;
79
    my $row = $self->find($params);
80
    return $row unless $row->parent_id;
81
    return $self->get_root_ancestor( { id => $row->parent_id } );
82
}
83
84
68
=head3 type
85
=head3 type
69
86
70
=cut
87
=cut
(-)a/t/db_dependent/Koha/Libraries.t (-1 / +34 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
23
24
use C4::Biblio;
24
use C4::Biblio;
25
use C4::Context;
25
use C4::Context;
Lines 451-453 subtest '->get_effective_marcorgcode' => sub { Link Here
451
451
452
    $schema->storage->txn_rollback;
452
    $schema->storage->txn_rollback;
453
};
453
};
454
455
subtest 'get_hold_libraries and validate_hold_sibling' => sub {
456
457
    plan tests => 5;
458
459
    $schema->storage->txn_begin;
460
461
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
462
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
463
    my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
464
465
    my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
466
    my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
467
    my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
468
469
    my @hold_libraries = ($library1, $library2);
470
471
    my @result = $library1->get_hold_libraries();
472
473
    ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
474
475
    my %map = map {$_->branchcode, 1} @result;
476
477
    foreach my $hold_library ( @hold_libraries ) {
478
        ok(exists $map{$hold_library->branchcode}, 'library in hold group');
479
    }
480
481
    ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
482
    ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
483
484
    $schema->storage->txn_rollback;
485
486
}
(-)a/t/db_dependent/LibraryGroups.t (-2 / +20 lines)
Lines 4-10 use Modern::Perl; Link Here
4
4
5
use List::MoreUtils 'any';
5
use List::MoreUtils 'any';
6
6
7
use Test::More tests => 20;
7
use Test::More tests => 21;
8
8
9
use t::lib::TestBuilder;
9
use t::lib::TestBuilder;
10
use Koha::Database;
10
use Koha::Database;
Lines 144-146 is( ref($groupX->libraries), 'Koha::Libraries', '->libraries should return a Koh Link Here
144
@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
144
@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
145
is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
145
is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
146
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
146
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
147
- 
147
148
subtest 'Koha::Library::Groups->get_root_ancestor' => sub {
149
    plan tests => 2;
150
151
    my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store();
152
    my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id,  branchcode => $library1->{branchcode} })->store();
153
    my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store();
154
    my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id,  branchcode => $library2->{branchcode} })->store();
155
    my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store();
156
    my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id,  title => "Group Z1" })->store();
157
    my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id,  title => "Group Z2" })->store();
158
    my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id,  branchcode => $library2->{branchcode} })->store();
159
160
    my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed);
161
    my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed);
162
163
    is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor");
164
    ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids");
165
};

Return to bug 22284