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

(-)a/Koha/Library.pm (+53 lines)
Lines 25-30 use C4::Context; Link Here
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::StockRotationStages;
27
use Koha::StockRotationStages;
28
use Koha::Libraries;
29
30
use Data::Dumper qw(Dumper);
28
31
29
use base qw(Koha::Object);
32
use base qw(Koha::Object);
30
33
Lines 77-82 sub library_groups { Link Here
77
    return Koha::Library::Groups->_new_from_dbic( $rs );
80
    return Koha::Library::Groups->_new_from_dbic( $rs );
78
}
81
}
79
82
83
=head3 get_hold_libraries
84
85
Return all libraries (including self) that belong to the same hold groups
86
87
=cut
88
89
sub get_hold_libraries {
90
    my ( $self ) = @_;
91
    my $library_groups = $self->library_groups;
92
    my @hold_libraries;
93
    while ( my $library_group = $library_groups->next ) {
94
        my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
95
        if($root->ft_local_hold_group) {
96
            push @hold_libraries, $root->all_libraries;
97
        }
98
    }
99
100
    my %seen;
101
    @hold_libraries =
102
      grep { !$seen{ $_->id }++ } @hold_libraries;
103
104
    return @hold_libraries;
105
}
106
107
=head3 validate_hold_sibling
108
109
Return if given library is a valid hold group member
110
111
=cut
112
113
sub validate_hold_sibling {
114
    my ( $self, $params ) = @_;
115
    my @hold_libraries = $self->get_hold_libraries;
116
117
    foreach (@hold_libraries) {
118
        my $hold_library = $_;
119
        my $cond = 1;
120
        foreach my $key (keys %$params) {
121
            unless($hold_library->$key eq $params->{$key}) {
122
                $cond=0;
123
                last;
124
            }
125
        }
126
        if($cond) {
127
            return 1;
128
        }
129
    }
130
    return 0;
131
}
132
80
=head2 Internal methods
133
=head2 Internal methods
81
134
82
=head3 _type
135
=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 / +22 lines)
Lines 4-14 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;
11
11
12
use Data::Dumper;
13
12
BEGIN {
14
BEGIN {
13
    use FindBin;
15
    use FindBin;
14
    use lib $FindBin::Bin;
16
    use lib $FindBin::Bin;
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 );
146
@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
145
is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
147
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
148
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
- 
149
150
subtest 'Koha::Library::Groups->get_root_ancestor' => sub {
151
    plan tests => 2;
152
153
    my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store();
154
    my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id,  branchcode => $library1->{branchcode} })->store();
155
    my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store();
156
    my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id,  branchcode => $library2->{branchcode} })->store();
157
    my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store();
158
    my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id,  title => "Group Z1" })->store();
159
    my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id,  title => "Group Z2" })->store();
160
    my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id,  branchcode => $library2->{branchcode} })->store();
161
162
    my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed);
163
    my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed);
164
165
    is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor");
166
    ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids");
167
};

Return to bug 22284