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

(-)a/t/db_dependent/Koha/Volumes.t (+151 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 6;
21
22
use Koha::Database;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
BEGIN {
28
    use_ok('Koha::Biblio::Volume');
29
    use_ok('Koha::Biblio::Volumes');
30
}
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference('EnableVolumes', 1);
36
37
subtest 'Test Koha::Biblio::volumes' => sub {
38
39
    plan tests => 6;
40
41
    $schema->storage->txn_begin;
42
43
    my $biblio = $builder->build_sample_biblio();
44
45
    my @volumes = $biblio->volumes->as_list;
46
    is( scalar(@volumes), 0, 'Got zero volumes');
47
48
    my $volume_1 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
49
50
    @volumes = $biblio->volumes->as_list;
51
    is( scalar(@volumes), 1, 'Got one volume');
52
    is( $volumes[0]->id, $volume_1->id, 'Got correct volume');
53
54
    my $volume_2 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
55
56
    @volumes = $biblio->volumes->as_list;
57
    is( scalar(@volumes), 2, 'Got two volumes');
58
    is( $volumes[0]->id, $volume_1->id, 'Got correct volume 1');
59
    is( $volumes[1]->id, $volume_2->id, 'Got correct volume 2');
60
61
    $schema->storage->txn_rollback;
62
};
63
64
subtest 'Test Koha::Biblio::Volume::add_item & Koha::Biblio::Volume::items' => sub {
65
66
    plan tests => 6;
67
68
    $schema->storage->txn_begin;
69
70
    my $biblio = $builder->build_sample_biblio();
71
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
72
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
73
74
    my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
75
76
    my @items = $volume->items;
77
    is( scalar(@items), 0, 'Volume has no items');
78
79
    $volume->add_item({ item_id => $item_1->id });
80
    @items = $volume->items->as_list;
81
    is( scalar(@items), 1, 'Volume has one item');
82
    is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
83
84
    $volume->add_item({ item_id => $item_2->id });
85
    @items = $volume->items->as_list;
86
    is( scalar(@items), 2, 'Volume has two items');
87
    is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
88
    is( $items[1]->id, $item_2->id, 'Item 2 is correct' );
89
90
    $schema->storage->txn_rollback;
91
};
92
93
subtest 'Test Koha::Item::volume' => sub {
94
95
    plan tests => 4;
96
97
    $schema->storage->txn_begin;
98
99
    my $biblio = $builder->build_sample_biblio();
100
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
101
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
102
103
    is( $item_1->volume, undef, 'Item 1 has no volume');
104
    is( $item_2->volume, undef, 'Item 2 has no volume');
105
106
    my $volume_1 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
107
    my $volume_2 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
108
109
    $volume_1->add_item({ item_id => $item_1->id });
110
    $volume_2->add_item({ item_id => $item_2->id });
111
112
    is( $item_1->volume->id, $volume_1->id, 'Got volume 1 correctly' );
113
    is( $item_2->volume->id, $volume_2->id, 'Got volume 2 correctly' );
114
115
    $schema->storage->txn_rollback;
116
};
117
118
subtest 'Koha::Item::delete should delete volume if no other items are using the volume' => sub {
119
120
    plan tests => 8;
121
122
    $schema->storage->txn_begin;
123
124
    my $biblio = $builder->build_sample_biblio();
125
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
126
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
127
    is( $biblio->items->count, 2, 'Bib has 2 items');
128
129
    is( $item_1->volume, undef, 'Item 1 has no volume');
130
    is( $item_2->volume, undef, 'Item 2 has no volume');
131
132
    my $volume_1 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store();
133
134
    $volume_1->add_item({ item_id => $item_1->id });
135
    $volume_1->add_item({ item_id => $item_2->id });
136
137
    my $volume = Koha::Biblio::Volumes->find( $volume_1->id );
138
    is( $volume->id, $volume_1->id, 'Found the correct volume');
139
140
    $item_1->delete();
141
    is( $biblio->items->count, 1, 'Bib has 2 item');
142
    $volume = Koha::Biblio::Volumes->find( $volume_1->id );
143
    is( $volume->id, $volume_1->id, 'Volume still exists after deleting and item, but other items remain');
144
145
    $item_2->delete();
146
    is( $biblio->items->count, 0, 'Bib has 0 items');
147
    $volume = Koha::Biblio::Volumes->find( $volume_1->id );
148
    is( $volume, undef, 'Volume was deleted when last item was deleted');
149
150
    $schema->storage->txn_rollback;
151
};
(-)a/t/db_dependent/api/v1/volumes.t (-1 / +257 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Mojo;
22
use Test::Warn;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use List::Util qw(min);
28
29
use Koha::Libraries;
30
use Koha::Database;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
t::lib::Mocks::mock_preference( 'EnableVolumes', 1 );
37
38
my $t = Test::Mojo->new('Koha::REST::V1');
39
40
subtest 'volumes list() tests' => sub {
41
    plan tests => 9;
42
43
    $schema->storage->txn_begin;
44
45
    my $patron = $builder->build_object({
46
        class => 'Koha::Patrons',
47
        value => { flags => 4 }
48
    });
49
    my $password = 'thePassword123';
50
    $patron->set_password({ password => $password, skip_validation => 1 });
51
    my $userid = $patron->userid;
52
53
    my $biblio = $builder->build_sample_biblio();
54
    my $biblio_id = $biblio->id;
55
56
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" )
57
        ->status_is( 200, 'SWAGGER3.2.2' );
58
    my $response_count = scalar @{ $t->tx->res->json };
59
    is( $response_count, 0, 'Results count is 2');
60
61
    my $volume_1 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
62
63
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" )
64
        ->status_is( 200, 'SWAGGER3.2.2' );
65
    $response_count = scalar @{ $t->tx->res->json };
66
    is( $response_count, 1, 'Results count is 2');
67
68
    my $volume_2 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 2, description => "Vol 2" } )->store();
69
70
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" )
71
      ->status_is( 200, 'SWAGGER3.2.2' );
72
73
    $response_count = scalar @{ $t->tx->res->json };
74
    is( $response_count, 2, 'Results count is 2');
75
76
    $schema->storage->txn_rollback;
77
};
78
79
subtest 'volumes add() tests' => sub {
80
81
    plan tests => 6;
82
83
    $schema->storage->txn_begin;
84
85
    my $authorized_patron = $builder->build_object({
86
        class => 'Koha::Patrons',
87
        value => { flags => 1 }
88
    });
89
    my $password = 'thePassword123';
90
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
91
    my $auth_userid = $authorized_patron->userid;
92
93
    my $unauthorized_patron = $builder->build_object({
94
        class => 'Koha::Patrons',
95
        value => { flags => 0 }
96
    });
97
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
98
    my $unauth_userid = $unauthorized_patron->userid;
99
100
    my $biblio = $builder->build_sample_biblio();
101
    my $biblio_id = $biblio->id;
102
    my $volume = { description => 'Vol 1', display_order => 1 };
103
104
    # Unauthorized attempt
105
    $t->post_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes" => json => $volume )
106
      ->status_is(403);
107
108
    # Authorized attempt
109
    $t->post_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes" => json => $volume )
110
      ->status_is( 201, 'SWAGGER3.2.1' );
111
112
    # Invalid biblio id
113
    $t->post_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes" => json => $volume )
114
        ->status_is( 409, 'SWAGGER3.2.1' );
115
116
    $schema->storage->txn_rollback;
117
};
118
119
subtest 'volumes update() tests' => sub {
120
    plan tests => 9;
121
122
    $schema->storage->txn_begin;
123
124
    my $authorized_patron = $builder->build_object({
125
        class => 'Koha::Patrons',
126
        value => { flags => 1 }
127
    });
128
    my $password = 'thePassword123';
129
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
130
    my $auth_userid = $authorized_patron->userid;
131
132
    my $unauthorized_patron = $builder->build_object({
133
        class => 'Koha::Patrons',
134
        value => { flags => 0 }
135
    });
136
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
137
    my $unauth_userid = $unauthorized_patron->userid;
138
139
    my $biblio = $builder->build_sample_biblio();
140
    my $biblio_id = $biblio->id;
141
    my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
142
    my $volume_id = $volume->id;
143
144
    # Unauthorized attempt
145
    $t->put_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id"
146
                    => json => { description => 'New unauthorized desc change' } )
147
      ->status_is(403);
148
149
    # Authorized attempt
150
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" => json => { description => "Vol A" } )
151
      ->status_is(200, 'SWAGGER3.2.1')
152
      ->json_has( '/description' => "Vol A", 'SWAGGER3.3.3' );
153
154
    # Invalid biblio id
155
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes/$volume_id" => json => { description => "Vol A" } )
156
        ->status_is(404);
157
158
    # Invalid volume id
159
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/XXX" => json => { description => "Vol A" } )
160
        ->status_is(404);
161
162
    $schema->storage->txn_rollback;
163
};
164
165
subtest 'volumes delete() tests' => sub {
166
    plan tests => 9;
167
168
    $schema->storage->txn_begin;
169
170
    my $authorized_patron = $builder->build_object({
171
        class => 'Koha::Patrons',
172
        value => { flags => 1 }
173
    });
174
    my $password = 'thePassword123';
175
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
176
    my $auth_userid = $authorized_patron->userid;
177
178
    my $unauthorized_patron = $builder->build_object({
179
        class => 'Koha::Patrons',
180
        value => { flags => 0 }
181
    });
182
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
183
    my $unauth_userid = $unauthorized_patron->userid;
184
185
    my $biblio = $builder->build_sample_biblio();
186
    my $biblio_id = $biblio->id;
187
    my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
188
    my $volume_id = $volume->id;
189
190
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" )
191
        ->status_is(204, 'SWAGGER3.2.4')
192
        ->content_is('', 'SWAGGER3.3.4');
193
194
    # Unauthorized attempt to delete
195
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" )
196
      ->status_is(403);
197
198
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes/$volume_id" )
199
        ->status_is(404);
200
201
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/XXX" )
202
        ->status_is(404);
203
204
    $schema->storage->txn_rollback;
205
};
206
207
subtest 'volume items add() + delete() tests' => sub {
208
    plan tests => 11;
209
210
    $schema->storage->txn_begin;
211
212
    my $patron = $builder->build_object({
213
        class => 'Koha::Patrons',
214
        value => { flags => 4 }
215
    });
216
    my $password = 'thePassword123';
217
    $patron->set_password({ password => $password, skip_validation => 1 });
218
    my $userid = $patron->userid;
219
220
    my $biblio = $builder->build_sample_biblio();
221
    my $biblio_id = $biblio->id;
222
223
    my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
224
    my $volume_id = $volume->id;
225
226
    my @items = $volume->items;
227
    is( scalar(@items), 0, 'Volume has no items');
228
229
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
230
    my $item_1_id = $item_1->id;
231
232
    $t->post_ok( "//$userid:$password@/api/v1/biblios/{biblio_id}/volumes/$volume_id/items" => json => { item_id => $item_1->id } )
233
        ->status_is( 201, 'SWAGGER3.2.1' );
234
235
    @items = $volume->items;
236
    is( scalar(@items), 1, 'Volume now has one item');
237
238
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
239
    my $item_2_id = $item_2->id;
240
241
    $t->post_ok( "//$userid:$password@/api/v1/biblios/{biblio_id}/volumes/$volume_id/items" => json => { item_id => $item_2->id } )
242
        ->status_is( 201, 'SWAGGER3.2.1' );
243
244
    @items = $volume->items;
245
    is( scalar(@items), 2, 'Volume now has two items');
246
247
    warn "A VOLUME ID: $volume_id";
248
    warn "A ITEM ID: $item_1_id";
249
    $t->delete_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id/items/$item_1_id" )
250
        ->status_is(204, 'SWAGGER3.2.4')
251
        ->content_is('', 'SWAGGER3.3.4');
252
253
    @items = $volume->items;
254
    is( scalar(@items), 1, 'Volume now has one item');
255
256
    $schema->storage->txn_rollback;
257
};

Return to bug 24857