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

(-)a/Koha/Library.pm (+51 lines)
Lines 121-126 sub to_api_mapping { Link Here
121
    };
121
    };
122
}
122
}
123
123
124
=head3 get_hold_libraries
125
126
Return all libraries (including self) that belong to the same hold groups
127
128
=cut
129
130
sub get_hold_libraries {
131
    my ( $self ) = @_;
132
    my $library_groups = $self->library_groups;
133
    my @hold_libraries;
134
    while ( my $library_group = $library_groups->next ) {
135
        my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
136
        if($root->ft_local_hold_group) {
137
            push @hold_libraries, $root->all_libraries;
138
        }
139
    }
140
141
    my %seen;
142
    @hold_libraries =
143
      grep { !$seen{ $_->id }++ } @hold_libraries;
144
145
    return @hold_libraries;
146
}
147
148
=head3 validate_hold_sibling
149
150
Return if given library is a valid hold group member
151
152
=cut
153
154
sub validate_hold_sibling {
155
    my ( $self, $params ) = @_;
156
    my @hold_libraries = $self->get_hold_libraries;
157
158
    foreach (@hold_libraries) {
159
        my $hold_library = $_;
160
        my $is_valid = 1;
161
        foreach my $key (keys %$params) {
162
            unless($hold_library->$key eq $params->{$key}) {
163
                $is_valid=0;
164
                last;
165
            }
166
        }
167
        if($is_valid) {
168
            #Found one library that meets all search parameters
169
            return 1;
170
        }
171
    }
172
    return 0;
173
}
174
124
=head2 Internal methods
175
=head2 Internal methods
125
176
126
=head3 _type
177
=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 => 8;
22
use Test::More tests => 9;
23
23
24
use C4::Biblio;
24
use C4::Biblio;
25
use C4::Context;
25
use C4::Context;
Lines 468-470 subtest 'cash_registers' => sub { Link Here
468
468
469
    $schema->storage->txn_rollback;
469
    $schema->storage->txn_rollback;
470
};
470
};
471
472
subtest 'get_hold_libraries and validate_hold_sibling' => sub {
473
474
    plan tests => 5;
475
476
    $schema->storage->txn_begin;
477
478
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
479
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
480
    my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
481
482
    my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
483
    my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
484
    my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
485
486
    my @hold_libraries = ($library1, $library2);
487
488
    my @result = $library1->get_hold_libraries();
489
490
    ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
491
492
    my %map = map {$_->branchcode, 1} @result;
493
494
    foreach my $hold_library ( @hold_libraries ) {
495
        ok(exists $map{$hold_library->branchcode}, 'library in hold group');
496
    }
497
498
    ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
499
    ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
500
501
    $schema->storage->txn_rollback;
502
503
};
(-)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