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 => 9; |
21 |
use Test::Mojo; |
22 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use C4::Auth; |
27 |
use Koha::UsersColumnsSetting; |
28 |
use Koha::UsersColumnsSettings; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
$schema->storage->txn_begin; |
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
|
35 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
36 |
|
37 |
my $remote_address = '127.0.0.1'; |
38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
39 |
|
40 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
41 |
create_user_and_session( { authorized => 0 } ); |
42 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
43 |
create_user_and_session( { authorized => 1 } ); |
44 |
|
45 |
my $user_columns_settings = { |
46 |
borrowernumber => $authorized_borrowernumber, |
47 |
module => 'catalgue', |
48 |
page => 'marcdetail', |
49 |
tablename => 'itemst', |
50 |
additional_param => 'ACQ', |
51 |
columns => [ |
52 |
{name => 'foo', is_hidden => \0}, |
53 |
{name => 'bar', is_hidden => \1} |
54 |
] |
55 |
}; |
56 |
|
57 |
my $tx = $t->ua->build_tx( POST => "/api/v1/user_columns_settings/" => json => $user_columns_settings ); |
58 |
$tx->req->cookies( |
59 |
{ name => 'CGISESSID', value => $unauthorized_session_id } ); |
60 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
61 |
$t->request_ok($tx)->status_is(403); |
62 |
|
63 |
$tx = $t->ua->build_tx( POST => "/api/v1/user_columns_settings/" => json => $user_columns_settings ); |
64 |
$tx->req->cookies( |
65 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
66 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
67 |
$t->request_ok($tx)->status_is(200); |
68 |
|
69 |
my $col_foo = Koha::UsersColumnsSettings->search({ |
70 |
borrowernumber => $authorized_borrowernumber, |
71 |
module => 'catalgue', |
72 |
page => 'marcdetail', |
73 |
tablename => 'itemst', |
74 |
additional_param => 'ACQ', |
75 |
columnname => 'foo' |
76 |
|
77 |
})->next; |
78 |
|
79 |
is($col_foo->is_hidden, 0, 'Column foo added'); |
80 |
|
81 |
my $col_bar = Koha::UsersColumnsSettings->search({ |
82 |
borrowernumber => $authorized_borrowernumber, |
83 |
module => 'catalgue', |
84 |
page => 'marcdetail', |
85 |
tablename => 'itemst', |
86 |
additional_param => 'ACQ', |
87 |
columnname => 'bar' |
88 |
})->next; |
89 |
|
90 |
is($col_bar->is_hidden, 1, 'Column bar added'); |
91 |
|
92 |
# Update user columns |
93 |
$user_columns_settings->{columns}->[0]->{is_hidden} = \1; |
94 |
|
95 |
$tx = $t->ua->build_tx( POST => "/api/v1/user_columns_settings/" => json => $user_columns_settings ); |
96 |
$tx->req->cookies( |
97 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
98 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
99 |
$t->request_ok($tx)->status_is(200); |
100 |
|
101 |
$col_foo = Koha::UsersColumnsSettings->search({ |
102 |
borrowernumber => $authorized_borrowernumber, |
103 |
module => 'catalgue', |
104 |
page => 'marcdetail', |
105 |
tablename => 'itemst', |
106 |
additional_param => 'ACQ', |
107 |
columnname => 'foo' |
108 |
|
109 |
})->next; |
110 |
|
111 |
is($col_foo->is_hidden, 1, 'Column foo updated'); |
112 |
|
113 |
sub create_user_and_session { |
114 |
|
115 |
my $args = shift; |
116 |
my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
117 |
my $dbh = C4::Context->dbh; |
118 |
|
119 |
my $user = $builder->build( |
120 |
{ |
121 |
source => 'Borrower', |
122 |
value => { |
123 |
flags => $flags |
124 |
} |
125 |
} |
126 |
); |
127 |
|
128 |
# Create a session for the authorized user |
129 |
my $session = C4::Auth::get_session(''); |
130 |
$session->param( 'number', $user->{borrowernumber} ); |
131 |
$session->param( 'id', $user->{userid} ); |
132 |
$session->param( 'ip', '127.0.0.1' ); |
133 |
$session->param( 'lasttime', time() ); |
134 |
$session->flush; |
135 |
|
136 |
if ( $args->{authorized} ) { |
137 |
$dbh->do( " |
138 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
139 |
VALUES (?,3,'parameters_remaining_permissions')", undef, |
140 |
$user->{borrowernumber} ); |
141 |
} |
142 |
|
143 |
return ( $user->{borrowernumber}, $session->id ); |
144 |
} |
145 |
|
146 |
$schema->storage->txn_rollback; |
147 |
|
148 |
1; |