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

(-)a/t/db_dependent/api/v1/items.t (-2 / +141 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Mojo;
24
use Test::Mojo;
25
use Test::Warn;
25
use Test::Warn;
Lines 99-104 subtest 'list() tests' => sub { Link Here
99
    $schema->storage->txn_rollback;
99
    $schema->storage->txn_rollback;
100
};
100
};
101
101
102
subtest 'list_public() tests' => sub {
103
104
    plan tests => 2;
105
106
    $schema->storage->txn_begin;
107
108
    # Clean out all demo items
109
    Koha::Items->delete();
110
111
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
112
    my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
113
    my $exception = 1;
114
    $mocked_category->mock( 'override_hidden_items', sub {
115
        return $exception;
116
    });
117
118
    my $password = 'thePassword123';
119
    $patron->set_password( { password => $password, skip_validation => 1 } );
120
    my $userid = $patron->userid;
121
122
    # have a fresh biblio
123
    my $biblio = $builder->build_sample_biblio;
124
    # have two itemtypes
125
    my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
126
    my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
127
    # have 5 items on that biblio
128
    my $item_1 = $builder->build_sample_item(
129
        {
130
            biblionumber => $biblio->biblionumber,
131
            itemlost     => -1,
132
            itype        => $itype_1->itemtype,
133
            withdrawn    => 1,
134
            copynumber   => undef
135
        }
136
    );
137
    my $item_2 = $builder->build_sample_item(
138
        {
139
            biblionumber => $biblio->biblionumber,
140
            itemlost     => 0,
141
            itype        => $itype_2->itemtype,
142
            withdrawn    => 2,
143
            copynumber   => undef
144
        }
145
    );
146
    my $item_3 = $builder->build_sample_item(
147
        {
148
            biblionumber => $biblio->biblionumber,
149
            itemlost     => 1,
150
            itype        => $itype_1->itemtype,
151
            withdrawn    => 3,
152
            copynumber   => undef
153
        }
154
    );
155
    my $item_4 = $builder->build_sample_item(
156
        {
157
            biblionumber => $biblio->biblionumber,
158
            itemlost     => 0,
159
            itype        => $itype_2->itemtype,
160
            withdrawn    => 4,
161
            copynumber   => undef
162
        }
163
    );
164
    my $item_5 = $builder->build_sample_item(
165
        {
166
            biblionumber => $biblio->biblionumber,
167
            itemlost     => 0,
168
            itype        => $itype_1->itemtype,
169
            withdrawn    => 5,
170
            copynumber   => undef
171
        }
172
    );
173
    my $item_6 = $builder->build_sample_item(
174
        {
175
            biblionumber => $biblio->biblionumber,
176
            itemlost     => 2,
177
            itype        => $itype_1->itemtype,
178
            withdrawn    => 5,
179
            copynumber   => undef
180
        }
181
    );
182
183
    my $rules = undef;
184
    my $mocked_context = Test::MockModule->new('C4::Context');
185
    $mocked_context->mock( 'yaml_preference', sub {
186
        return $rules;
187
    });
188
189
    subtest 'anonymous access' => sub {
190
        plan tests => 21;
191
192
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
193
        my $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
194
        is( scalar @{ $res }, 6, 'No rules set, hidelostitems unset, all items returned');
195
196
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
197
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
198
        is( scalar @{ $res }, 3, 'No rules set, hidelostitems set, 3 items hidden');
199
200
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
201
        $rules = { biblionumber => [ $biblio->biblionumber ] };
202
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
203
        is( scalar @{ $res }, 0, 'Biblionumber rule set, hidelostitems unset, all items hidden');
204
205
        $rules = { withdrawn => [ 1, 2 ] };
206
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
207
        is( scalar @{ $res }, 4, 'Withdrawn rule set, hidelostitems unset, 2 items hidden');
208
209
        $rules = { itype => [ $itype_1->itemtype ] };
210
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
211
        is( scalar @{ $res }, 2, 'Itype rule set, hidelostitems unset, 4 items hidden');
212
213
        $rules = { withdrawn => [ 1 ] };
214
        $res = $t->get_ok( "/api/v1/public/items?external_id=" . $item_1->barcode )
215
          ->status_is(200)->tx->res->json;
216
        is( scalar @{ $res }, 0, 'Withdrawn rule set, hidelostitems unset, search on barcode returns no item');
217
218
        $rules = undef;
219
        $t->get_ok( "/api/v1/public/items?external_id=" . $item_1->barcode )
220
          ->status_is(200)->json_is(
221
            '/0' => $item_1->to_api( { public => 1 } ),
222
'No rules set, hidelostitems unset, public form of item returned on barcode search'
223
          );
224
    };
225
226
    subtest 'logged in user access' => sub {
227
        plan tests => 3;
228
229
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
230
        $rules = { withdrawn => [ 1, 2 ] };
231
        my $res = $t->get_ok("//$userid:$password@/api/v1/public/items")
232
          ->status_is(200)->tx->res->json;
233
        is(
234
            scalar @{$res},
235
            3,
236
'Rules on withdrawn but patron with override passed, hidelostitems set'
237
        );
238
    };
239
240
    $schema->storage->txn_rollback;
241
};
102
242
103
subtest 'get() tests' => sub {
243
subtest 'get() tests' => sub {
104
244
105
- 

Return to bug 33146