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 under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 27; |
21 |
use Test::MockModule; |
22 |
use Test::Mojo; |
23 |
use t::lib::TestBuilder; |
24 |
|
25 |
use DateTime; |
26 |
use MARC::Record; |
27 |
|
28 |
use C4::Context; |
29 |
use C4::Biblio; |
30 |
use C4::Circulation; |
31 |
use C4::Items; |
32 |
|
33 |
use Koha::Database; |
34 |
use Koha::Patron; |
35 |
use Koha::OldIssue; |
36 |
use Koha::OldIssues; |
37 |
|
38 |
my $schema = Koha::Database->schema; |
39 |
$schema->storage->txn_begin; |
40 |
my $dbh = C4::Context->dbh; |
41 |
my $builder = t::lib::TestBuilder->new; |
42 |
$dbh->{RaiseError} = 1; |
43 |
|
44 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
45 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
46 |
|
47 |
$dbh->do('DELETE FROM issues'); |
48 |
$dbh->do('DELETE FROM items'); |
49 |
$dbh->do('DELETE FROM issuingrules'); |
50 |
my $loggedinuser = $builder->build({ source => 'Borrower' }); |
51 |
|
52 |
$dbh->do(q{ |
53 |
INSERT INTO user_permissions (borrowernumber, module_bit, code) |
54 |
VALUES (?, 1, 'circulate_remaining_permissions') |
55 |
}, undef, $loggedinuser->{borrowernumber}); |
56 |
|
57 |
my $session = C4::Auth::get_session(''); |
58 |
$session->param('number', $loggedinuser->{ borrowernumber }); |
59 |
$session->param('id', $loggedinuser->{ userid }); |
60 |
$session->param('ip', '127.0.0.1'); |
61 |
$session->param('lasttime', time()); |
62 |
$session->flush; |
63 |
|
64 |
my $borrower = $builder->build({ source => 'Borrower' }); |
65 |
my $borrowernumber = $borrower->{borrowernumber}; |
66 |
|
67 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
68 |
my $module = new Test::MockModule('C4::Context'); |
69 |
$module->mock('userenv', sub { { branch => $branchcode } }); |
70 |
|
71 |
my $tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber"); |
72 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
73 |
$t->request_ok($tx) |
74 |
->status_is(200) |
75 |
->json_is([]); |
76 |
|
77 |
my $notexisting_borrowernumber = $borrowernumber + 1; |
78 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$notexisting_borrowernumber"); |
79 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
80 |
$t->request_ok($tx) |
81 |
->status_is(404) |
82 |
->json_has('/error'); |
83 |
|
84 |
my $biblionumber = create_biblio('RESTful Web APIs'); |
85 |
my $itemnumber1 = create_item($biblionumber, 'TEST000001'); |
86 |
my $itemnumber2 = create_item($biblionumber, 'TEST000002'); |
87 |
|
88 |
my $date_due = DateTime->now->add(weeks => 2); |
89 |
|
90 |
my $issueId = Koha::OldIssues->count({}) + 1500; |
91 |
my $issue1; |
92 |
$issue1 = Koha::OldIssue->new({ issue_id => $issueId, borrowernumber => $borrowernumber, itemnumber => $itemnumber1, date_due => $date_due}); |
93 |
$issue1->store(); |
94 |
|
95 |
my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due ); |
96 |
|
97 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber"); |
98 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
99 |
$t->request_ok($tx) |
100 |
->status_is(200) |
101 |
->json_is('/0/issue_id' => $issueId) |
102 |
->json_hasnt('/1') |
103 |
->json_hasnt('/error'); |
104 |
|
105 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history/" . $issue1->issue_id); |
106 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
107 |
$t->request_ok($tx) |
108 |
->status_is(200) |
109 |
->json_is('/borrowernumber' => $borrowernumber) |
110 |
->json_is('/itemnumber' => $itemnumber1) |
111 |
->json_is('/date_due' => $date_due1->ymd . ' ' . $date_due1->hms) |
112 |
->json_hasnt('/error'); |
113 |
|
114 |
$issue1->delete(); |
115 |
|
116 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history/" . $issue1->issue_id); |
117 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
118 |
$t->request_ok($tx) |
119 |
->status_is(404) |
120 |
->json_has('/error'); |
121 |
|
122 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber"); |
123 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
124 |
$t->request_ok($tx) |
125 |
->status_is(200) |
126 |
->json_hasnt('/0') |
127 |
->json_hasnt('/error'); |
128 |
|
129 |
Koha::Patrons->find($borrowernumber)->delete(); |
130 |
|
131 |
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber"); |
132 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
133 |
$t->request_ok($tx) |
134 |
->status_is(404) |
135 |
->json_has('/error'); |
136 |
|
137 |
sub create_biblio { |
138 |
my ($title) = @_; |
139 |
|
140 |
my $record = new MARC::Record; |
141 |
$record->append_fields( |
142 |
new MARC::Field('200', ' ', ' ', a => $title), |
143 |
); |
144 |
|
145 |
my ($biblionumber) = C4::Biblio::AddBiblio($record, ''); |
146 |
|
147 |
return $biblionumber; |
148 |
} |
149 |
|
150 |
sub create_item { |
151 |
my ($biblionumber, $barcode) = @_; |
152 |
|
153 |
my $item = { |
154 |
barcode => $barcode, |
155 |
}; |
156 |
|
157 |
my $itemnumber = C4::Items::AddItem($item, $biblionumber); |
158 |
|
159 |
return $itemnumber; |
160 |
} |