Bug 12482: removing the use of the module C4::SQLHelper.pm, it is replaced by DBIx...
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 37;
5
6 use MARC::Record;
7
8 use C4::Biblio qw( AddBiblio );
9 use C4::Members qw( AddMember );
10 use t::lib::Mocks;
11 use_ok('C4::Serials');
12 use_ok('C4::Budgets');
13
14 # Mock userenv
15 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
16 my $userenv;
17 *C4::Context::userenv = \&Mock_userenv;
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23
24 my $supplierlist=eval{GetSuppliersWithLateIssues()};
25 ok(length($@)==0,"No SQL problem in GetSuppliersWithLateIssues");
26
27 my $record = MARC::Record->new();
28 $record->append_fields(
29     MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
30 );
31 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
32
33 my $my_branch = 'CPL';
34 my $another_branch = 'MPL';
35 my $budgetid;
36 my $bpid = AddBudgetPeriod({
37     budget_period_startdate   => '2015-01-01',
38     budget_period_enddate     => '2015-12-31',
39     budget_period_description => "budget desc"
40 });
41
42 my $budget_id = AddBudget({
43     budget_code        => "ABCD",
44     budget_amount      => "123.132",
45     budget_name        => "Périodiques",
46     budget_notes       => "This is a note",
47     budget_period_id   => $bpid
48 });
49
50 my $subscriptionid_from_my_branch = NewSubscription(
51     undef,      $my_branch,     undef, undef, $budget_id, $biblionumber,
52     '2013-01-01', undef, undef, undef,  undef,
53     undef,      undef,  undef, undef, undef, undef,
54     1,          "notes",undef, '2013-01-01', undef, undef,
55     undef,       undef,  0,    "intnotes",  0,
56     undef, undef, 0,          undef,         '2013-12-31', 0
57 );
58 die unless $subscriptionid_from_my_branch;
59
60 my $subscriptionid_from_another_branch = NewSubscription(
61     undef,      $another_branch,     undef, undef, $budget_id, $biblionumber,
62     '2013-01-01', undef, undef, undef,  undef,
63     undef,      undef,  undef, undef, undef, undef,
64     1,          "notes",undef, '2013-01-01', undef, undef,
65     undef,       undef,  0,    "intnotes",  0,
66     undef, undef, 0,          undef,         '2013-12-31', 0
67 );
68
69
70 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
71 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
72
73 my $userid = 'my_userid';
74 my $borrowernumber = C4::Members::AddMember(
75     firstname =>  'my fistname',
76     surname => 'my surname',
77     categorycode => 'S',
78     branchcode => $my_branch,
79     userid => $userid,
80 );
81
82 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
83
84 # Can edit a subscription
85
86 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
87
88 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
89
90 $userenv->{id} = $userid;
91 $userenv->{branch} = $my_branch;
92
93 # Branches are independent
94 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
95 set_flags( 'superlibrarian', $borrowernumber );
96 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
97 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
98 );
99 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
100 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
101 );
102 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
103 "With IndependentBranches, superlibrarian can show a subscription from his branch"
104 );
105 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
106 "With IndependentBranches, superlibrarian can show a subscription from another branch"
107 );
108
109 set_flags( 'superserials', $borrowernumber );
110 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
111 "With IndependentBranches, superserials can edit a subscription from his branch"
112 );
113 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
114 "With IndependentBranches, superserials can edit a subscription from another branch"
115 );
116 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
117 "With IndependentBranches, superserials can show a subscription from his branch"
118 );
119 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
120 "With IndependentBranches, superserials can show a subscription from another branch"
121 );
122
123
124 set_flags( 'edit_subscription', $borrowernumber );
125 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
126 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
127 );
128 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
129 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
130 );
131 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
132 "With IndependentBranches, show_subscription can show a subscription from his branch"
133 );
134 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
135 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
136 );
137
138 set_flags( 'renew_subscription', $borrowernumber );
139 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
140 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
141 );
142 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
143 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
144 );
145 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
146 "With IndependentBranches, renew_subscription can show a subscription from his branch"
147 );
148 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
149 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
150 );
151
152
153 # Branches are not independent
154 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
155 set_flags( 'superlibrarian', $borrowernumber );
156 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
157 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
158 );
159 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
160 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
161 );
162 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
163 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
164 );
165 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
166 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
167 );
168
169 set_flags( 'superserials', $borrowernumber );
170 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
171 "Without IndependentBranches, superserials can edit a subscription from his branch"
172 );
173 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
174 "Without IndependentBranches, superserials can edit a subscription from another branch"
175 );
176 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
177 "Without IndependentBranches, superserials can show a subscription from his branch"
178 );
179 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
180 "Without IndependentBranches, superserials can show a subscription from another branch"
181 );
182
183 set_flags( 'edit_subscription', $borrowernumber );
184 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
185 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
186 );
187 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
188 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
189 );
190 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
191 "Without IndependentBranches, show_subscription can show a subscription from his branch"
192 );
193 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
194 "Without IndependentBranches, show_subscription can show a subscription from another branch"
195 );
196
197 set_flags( 'renew_subscription', $borrowernumber );
198 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
199 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
200 );
201 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
202 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
203 );
204 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
205 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
206 );
207 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
208 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
209 );
210
211 $dbh->rollback;
212
213 # C4::Context->userenv
214 sub Mock_userenv {
215     return $userenv;
216 }
217
218 sub set_flags {
219     my ( $flags, $borrowernumber ) = @_;
220     my $superlibrarian_flags = 1;
221     if ( $flags eq 'superlibrarian' ) {
222         $dbh->do(
223             q|
224             UPDATE borrowers SET flags=? WHERE borrowernumber=?
225         |, {}, $superlibrarian_flags, $borrowernumber
226         );
227         $userenv->{flags} = $superlibrarian_flags;
228     }
229     else {
230         $dbh->do(
231             q|
232             UPDATE borrowers SET flags=? WHERE borrowernumber=?
233         |, {}, 0, $borrowernumber
234         );
235         $userenv->{flags} = 0;
236         my ( $module_bit, $code ) = ( '15', $flags );
237         $dbh->do(
238             q|
239             DELETE FROM user_permissions where borrowernumber=?
240         |, {}, $borrowernumber
241         );
242
243         $dbh->do(
244             q|
245             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
246         |, {}, $borrowernumber, $module_bit, $code
247         );
248     }
249 }