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 => 1; |
21 |
use Test::MockModule; |
22 |
use Test::MockObject; |
23 |
use Test::Mojo; |
24 |
use Test::Warn; |
25 |
|
26 |
use t::lib::TestBuilder; |
27 |
use t::lib::Mocks; |
28 |
|
29 |
use C4::Auth; |
30 |
use Koha::ActionLogs; |
31 |
use Koha::DateUtils qw( format_sqldatetime ); |
32 |
|
33 |
my $schema = Koha::Database->new->schema; |
34 |
my $builder = t::lib::TestBuilder->new; |
35 |
|
36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
37 |
|
38 |
my $remote_address = '127.0.0.1'; |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
40 |
|
41 |
subtest 'list() tests' => sub { |
42 |
|
43 |
plan tests => 24; |
44 |
|
45 |
$schema->storage->txn_begin; |
46 |
|
47 |
Koha::ActionLogs->search->delete; |
48 |
# borrowers => 4 (userflags.sql) |
49 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 4 }); |
50 |
|
51 |
## Authorized user tests |
52 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
53 |
# No logs, so empty array should be returned |
54 |
my $tx = $t->ua->build_tx( GET => '/api/v1/patrons/999/renewals'); |
55 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
56 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
57 |
$t->request_ok($tx)->status_is(200)->json_is( [] ); |
58 |
|
59 |
# Create log entries |
60 |
my $log1 = $builder->build_object( |
61 |
{ |
62 |
class => 'Koha::ActionLogs', |
63 |
value => { |
64 |
module => 'CIRCULATION', |
65 |
action => 'RENEWAL', |
66 |
user => $borrowernumber, # Renewing staff member |
67 |
object => $patron->borrowernumber, # Patron |
68 |
info => 101, # Item ID |
69 |
interface => 'opac' # Renewing interface |
70 |
} |
71 |
} |
72 |
); |
73 |
my $log2 = $builder->build_object( |
74 |
{ |
75 |
class => 'Koha::ActionLogs', |
76 |
value => { |
77 |
module => 'CIRCULATION', |
78 |
action => 'RENEWAL', |
79 |
user => $borrowernumber, # Renewing staff member |
80 |
object => $patron->borrowernumber, # Patron |
81 |
info => 102, # Item ID |
82 |
interface => 'opac' # Renewing interface |
83 |
} |
84 |
} |
85 |
); |
86 |
# Getting all log entries for user |
87 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons/'.$patron->borrowernumber.'/renewals'); |
88 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
89 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
90 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/patron_id' => $patron->borrowernumber ); |
91 |
$t->request_ok($tx)->status_is(200)->json_is( '/1/patron_id' => $patron->borrowernumber ); |
92 |
|
93 |
# Get log entries filtered by item ID |
94 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons/'.$patron->borrowernumber.'/renewals?item_id=102'); |
95 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
96 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
97 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/item_id' => 102 ); |
98 |
|
99 |
# Get logs with patron embedded |
100 |
Koha::ActionLogs->search->delete; |
101 |
my $embed_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
102 |
my $log_patron = $builder->build_object( |
103 |
{ |
104 |
class => 'Koha::ActionLogs', |
105 |
value => { |
106 |
module => 'CIRCULATION', |
107 |
action => 'RENEWAL', |
108 |
object => $embed_patron->borrowernumber, # Patron |
109 |
user => $embed_patron->borrowernumber, # Renewing staff member |
110 |
} |
111 |
} |
112 |
); |
113 |
# Get log entry including patron embed |
114 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons/'.$embed_patron->borrowernumber.'/renewals?embed=patron'); |
115 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
116 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
117 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/patron/borrowernumber' => $embed_patron->borrowernumber ); |
118 |
# Get log entry including renewed_by embed |
119 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons/'.$embed_patron->borrowernumber.'/renewals?embed=renewed_by'); |
120 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
121 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
122 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/renewed_by/borrowernumber' => $embed_patron->borrowernumber ); |
123 |
# Get log entry including both embeds |
124 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons/'.$embed_patron->borrowernumber.'/renewals?embed=patron,renewed_by'); |
125 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
126 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
127 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/patron/borrowernumber' => $embed_patron->borrowernumber ); |
128 |
$t->request_ok($tx)->status_is(200)->json_is( '/0/renewed_by/borrowernumber' => $embed_patron->borrowernumber ); |
129 |
|
130 |
$schema->storage->txn_rollback; |
131 |
}; |
132 |
|
133 |
sub create_user_and_session { |
134 |
|
135 |
my $args = shift; |
136 |
my $dbh = C4::Context->dbh; |
137 |
|
138 |
my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0; |
139 |
|
140 |
my $user = $builder->build( |
141 |
{ |
142 |
source => 'Borrower', |
143 |
value => { |
144 |
flags => $flags |
145 |
} |
146 |
} |
147 |
); |
148 |
|
149 |
# Create a session for the authorized user |
150 |
my $session = C4::Auth::get_session(''); |
151 |
$session->param( 'number', $user->{borrowernumber} ); |
152 |
$session->param( 'id', $user->{userid} ); |
153 |
$session->param( 'ip', '127.0.0.1' ); |
154 |
$session->param( 'lasttime', time() ); |
155 |
$session->flush; |
156 |
|
157 |
return ( $user->{borrowernumber}, $session->id ); |
158 |
} |
159 |
|
160 |
1; |