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

(-)a/t/db_dependent/Template/Plugin/Branches.t (-27 / +68 lines)
Lines 113-150 subtest 'pickup_locations() tests' => sub { Link Here
113
113
114
    plan tests => 8;
114
    plan tests => 8;
115
115
116
    my $library_1 = { branchcode => 'A' };
116
    $schema->storage->txn_begin;
117
    my $library_2 = { branchcode => 'B' };
117
118
    my $library_3 = { branchcode => 'C' };
118
    Koha::Libraries->search->update({ pickup_location => 0 });
119
    my @library_array = ( $library_1, $library_2, $library_3 );
119
120
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
121
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
122
    my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
123
124
    my $plugin           = Koha::Template::Plugin::Branches->new();
125
    my $pickup_locations = $plugin->pickup_locations();
126
127
    is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
120
128
121
    my $libraries = Test::MockModule->new('Koha::Libraries');
129
    $pickup_locations = $plugin->pickup_locations({ search_params => { item => undef }});
122
    $libraries->mock(
130
    is( scalar @{$pickup_locations}, 3, 'item parameter not a ref, fallback to general search' );
131
132
    $pickup_locations = $plugin->pickup_locations({ search_params => { biblio => undef }});
133
    is( scalar @{$pickup_locations}, 3, 'biblio parameter not a ref, fallback to general search' );
134
135
    my $item_class = Test::MockModule->new('Koha::Item');
136
    $item_class->mock(
123
        'pickup_locations',
137
        'pickup_locations',
124
        sub {
138
        sub {
125
            my $result = clone(\@library_array);
139
            return [$library_1];
126
            return @$result;
127
        }
140
        }
128
    );
141
    );
129
142
130
    my $plugin           = Koha::Template::Plugin::Branches->new();
143
    my $item   = $builder->build_sample_item();
131
    my $pickup_locations = $plugin->pickup_locations();
144
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
132
145
133
    is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
146
    $pickup_locations = $plugin->pickup_locations(
134
    for my $library ( @{$pickup_locations} ) {
147
        { search_params => { item => $item, patron => Koha::Patron->new } } );
135
        ok( ( any { $_->{branchcode} eq $library->{branchcode} } @library_array ),
148
136
            'Library ' . $library->{branchcode} . ' in results' );
149
    is( scalar @{$pickup_locations}, 1, 'Only the library returned by $item->pickup_locations is returned' );
137
    }
150
    is( $pickup_locations->[0]->{branchcode}, $library_1->branchcode, 'Not cheating' );
138
151
139
    $pickup_locations = $plugin->pickup_locations({ selected => $library_2->{branchcode} });
152
    my $biblio_class = Test::MockModule->new('Koha::Biblio');
140
    my @selected = grep { exists $_->{selected} } @{ $pickup_locations };
153
    $biblio_class->mock(
141
    is( scalar @selected, 1, '(param) Only one is selected' );
154
        'pickup_locations',
142
    is( $selected[0]->{branchcode}, $library_2->{branchcode}, '(param) The selected one is the right one' );
155
        sub {
143
156
            return [$library_2];
144
    t::lib::Mocks::mock_userenv({ branchcode => $library_3->{branchcode} });
157
        }
145
    $pickup_locations = $plugin->pickup_locations();
158
    );
146
    @selected = grep { exists $_->{selected} } @{ $pickup_locations };
159
147
    is( scalar @selected, 1, '(userenv) Only one is selected' );
160
    my $biblio = $builder->build_sample_biblio();
148
    is( $selected[0]->{branchcode}, $library_3->{branchcode}, '(userenv) The selected one is the right one' );
161
162
    $pickup_locations = $plugin->pickup_locations(
163
        { search_params => { biblio => $biblio, patron => Koha::Patron->new } } );
164
165
    is( scalar @{$pickup_locations}, 1, 'Only the library returned by $biblio->pickup_locations is returned' );
166
    is( $pickup_locations->[0]->{branchcode}, $library_2->branchcode, 'Not cheating' );
167
168
    subtest 'selected tests' => sub {
169
170
        plan tests => 4;
149
171
172
        t::lib::Mocks::mock_userenv({ branchcode => $library_2->branchcode });
173
174
        $pickup_locations = $plugin->pickup_locations();
175
176
        is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
177
        foreach my $pickup_location (@{ $pickup_locations }) {
178
            next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
179
            is( $pickup_location->{branchcode}, $library_2->branchcode, 'The right library is marked as selected' );
180
        }
181
182
        $pickup_locations = $plugin->pickup_locations({ selected => $library_3->branchcode });
183
184
        is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
185
        foreach my $pickup_location (@{ $pickup_locations }) {
186
            next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
187
            is( $pickup_location->{branchcode}, $library_3->branchcode, 'The right library is marked as selected' );
188
        }
189
    };
190
191
    $schema->storage->txn_rollback;
150
};
192
};
151
- 

Return to bug 24368