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

(-)a/t/db_dependent/api/v1/deleted_biblios.t (-1 / +70 lines)
Lines 21-27 use utf8; Link Here
21
use Encode;
21
use Encode;
22
22
23
use Test::NoWarnings;
23
use Test::NoWarnings;
24
use Test::More tests => 3;
24
use Test::More tests => 4;
25
use Test::MockModule;
25
use Test::MockModule;
26
use Test::Mojo;
26
use Test::Mojo;
27
use Test::Warn;
27
use Test::Warn;
Lines 261-263 subtest 'list() tests' => sub { Link Here
261
261
262
    $schema->storage->txn_rollback;
262
    $schema->storage->txn_rollback;
263
};
263
};
264
265
subtest 'restore() tests' => sub {
266
267
    plan tests => 13;
268
269
    $schema->storage->txn_begin;
270
271
    my $librarian = $builder->build_object(
272
        {
273
            class => 'Koha::Patrons',
274
            value => { flags => 0 }
275
        }
276
    );
277
    my $password = 'thePassword123';
278
    $librarian->set_password( { password => $password, skip_validation => 1 } );
279
    my $userid = $librarian->userid;
280
281
    $builder->build(
282
        {
283
            source => 'UserPermission',
284
            value  => {
285
                borrowernumber => $librarian->borrowernumber,
286
                module_bit     => 13,
287
                code           => 'records_restore',
288
            }
289
        }
290
    );
291
292
    my $patron = $builder->build_object(
293
        {
294
            class => 'Koha::Patrons',
295
            value => { flags => 0 }
296
        }
297
    );
298
299
    $patron->set_password( { password => $password, skip_validation => 1 } );
300
    my $unauth_userid = $patron->userid;
301
302
    my $biblio = $builder->build_sample_biblio(
303
        {
304
            title  => 'Test Restore Biblio',
305
            author => 'Test Author'
306
        }
307
    );
308
309
    my $biblionumber = $biblio->biblionumber;
310
311
    DelBiblio($biblionumber);
312
313
    is( Koha::Biblios->find($biblionumber), undef, 'Biblio deleted successfully' );
314
    my $deleted_biblio = Koha::Old::Biblios->find($biblionumber);
315
    ok( $deleted_biblio, 'Biblio found in deleted table' );
316
317
    $t->put_ok("//$unauth_userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(403);
318
319
    $t->put_ok("//$userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(200)
320
        ->json_is( '/biblio_id' => $biblionumber );
321
322
    my $restored_biblio = Koha::Biblios->find($biblionumber);
323
    ok( $restored_biblio, 'Biblio restored successfully' );
324
    is( $restored_biblio->title,  'Test Restore Biblio', 'Biblio title preserved' );
325
    is( $restored_biblio->author, 'Test Author',         'Biblio author preserved' );
326
327
    is( Koha::Old::Biblios->find($biblionumber), undef, 'Biblio removed from deleted table' );
328
329
    $t->put_ok("//$userid:$password@/api/v1/deleted/biblios/$biblionumber")->status_is(404);
330
331
    $schema->storage->txn_rollback;
332
};
(-)a/t/db_dependent/api/v1/deleted_items.t (-1 / +187 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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 4;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Items;
28
use Koha::Old::Items;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'list() tests' => sub {
38
39
    plan tests => 9;
40
41
    $schema->storage->txn_begin;
42
43
    Koha::Old::Items->search->delete;
44
45
    my $librarian = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => { flags => 2**2 }
49
        }
50
    );
51
    my $password = 'thePassword123';
52
    $librarian->set_password( { password => $password, skip_validation => 1 } );
53
    my $userid = $librarian->userid;
54
55
    my $patron = $builder->build_object(
56
        {
57
            class => 'Koha::Patrons',
58
            value => { flags => 0 }
59
        }
60
    );
61
62
    $patron->set_password( { password => $password, skip_validation => 1 } );
63
    my $unauth_userid = $patron->userid;
64
65
    $t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200)->json_is( [] );
66
67
    my $item         = $builder->build_sample_item();
68
    my $item_id      = $item->itemnumber;
69
    my $item_data    = $item->unblessed;
70
    my $deleted_item = Koha::Old::Item->new($item_data)->store;
71
    $item->delete;
72
73
    $t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200);
74
75
    my $expected = $deleted_item->to_api;
76
77
    $t->json_has('/0')->json_is( '/0' => $expected );
78
79
    $t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items")->status_is(403);
80
81
    $schema->storage->txn_rollback;
82
};
83
84
subtest 'get() tests' => sub {
85
86
    plan tests => 8;
87
88
    $schema->storage->txn_begin;
89
90
    my $item         = $builder->build_sample_item();
91
    my $item_id      = $item->itemnumber;
92
    my $item_data    = $item->unblessed;
93
    my $deleted_item = Koha::Old::Item->new($item_data)->store;
94
    $item->delete;
95
96
    my $librarian = $builder->build_object(
97
        {
98
            class => 'Koha::Patrons',
99
            value => { flags => 2**2 }
100
        }
101
    );
102
    my $password = 'thePassword123';
103
    $librarian->set_password( { password => $password, skip_validation => 1 } );
104
    my $userid = $librarian->userid;
105
106
    my $patron = $builder->build_object(
107
        {
108
            class => 'Koha::Patrons',
109
            value => { flags => 0 }
110
        }
111
    );
112
113
    $patron->set_password( { password => $password, skip_validation => 1 } );
114
    my $unauth_userid = $patron->userid;
115
116
    $t->get_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200)->json_is( $deleted_item->to_api );
117
118
    $t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403);
119
120
    my $non_existent_id = $item_id + 99999;
121
122
    $t->get_ok("//$userid:$password@/api/v1/deleted/items/$non_existent_id")->status_is(404)
123
        ->json_is( '/error' => 'Item not found' );
124
125
    $schema->storage->txn_rollback;
126
};
127
128
subtest 'restore() tests' => sub {
129
130
    plan tests => 11;
131
132
    $schema->storage->txn_begin;
133
134
    my $librarian = $builder->build_object(
135
        {
136
            class => 'Koha::Patrons',
137
            value => { flags => 0 }
138
        }
139
    );
140
    my $password = 'thePassword123';
141
    $librarian->set_password( { password => $password, skip_validation => 1 } );
142
    my $userid = $librarian->userid;
143
144
    $builder->build(
145
        {
146
            source => 'UserPermission',
147
            value  => {
148
                borrowernumber => $librarian->borrowernumber,
149
                module_bit     => 13,
150
                code           => 'records_restore',
151
            }
152
        }
153
    );
154
155
    my $patron = $builder->build_object(
156
        {
157
            class => 'Koha::Patrons',
158
            value => { flags => 0 }
159
        }
160
    );
161
162
    $patron->set_password( { password => $password, skip_validation => 1 } );
163
    my $unauth_userid = $patron->userid;
164
165
    my $item         = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM' } );
166
    my $item_id      = $item->itemnumber;
167
    my $item_data    = $item->unblessed;
168
    my $deleted_item = Koha::Old::Item->new($item_data)->store;
169
    $item->delete;
170
171
    is( Koha::Items->find($item_id), undef, 'Item deleted successfully' );
172
    ok( $deleted_item, 'Item found in deleted table' );
173
174
    $t->put_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403);
175
176
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200);
177
178
    my $restored_item = Koha::Items->find($item_id);
179
    ok( $restored_item, 'Item restored successfully' );
180
    is( $restored_item->barcode, 'TEST_RESTORE_ITEM', 'Item barcode preserved' );
181
182
    is( Koha::Old::Items->find($item_id), undef, 'Item removed from deleted table' );
183
184
    $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404);
185
186
    $schema->storage->txn_rollback;
187
};

Return to bug 17387