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

(-)a/t/db_dependent/UsageStats.t (-72 / +56 lines)
Lines 15-21 Link Here
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
use Test::More tests => 552;
18
use Test::More tests => 551;
19
use t::lib::Mocks qw(mock_preference);
19
use t::lib::Mocks qw(mock_preference);
20
use POSIX qw(strftime);
20
use POSIX qw(strftime);
21
21
Lines 27-33 BEGIN { Link Here
27
    use_ok('C4::Reserves');
27
    use_ok('C4::Reserves');
28
    use_ok('MARC::Record');
28
    use_ok('MARC::Record');
29
    use_ok('Koha::Acquisition::Order');
29
    use_ok('Koha::Acquisition::Order');
30
    use_ok('t::lib::TestBuilder');
31
}
30
}
32
31
33
can_ok(
32
can_ok(
Lines 112-118 is( $report->{library}->{type}, 'TYPE', "UsageStatsLibraryType is Link Here
112
is( $report->{library}->{country},        'COUNTRY', "UsageStatsCountry       is good" );
111
is( $report->{library}->{country},        'COUNTRY', "UsageStatsCountry       is good" );
113
112
114
#Test report->volumetry ---------------
113
#Test report->volumetry ---------------
115
#without objects
114
#with original values
116
$report = C4::UsageStats->BuildReport();
115
$report = C4::UsageStats->BuildReport();
117
116
118
isa_ok( $report,              'HASH', '$report is a HASH' );
117
isa_ok( $report,              'HASH', '$report is a HASH' );
Lines 127-148 is( $report->{volumetry}->{borrowers}, 0, "There is no borrowers" ); Link Here
127
is( $report->{volumetry}->{aqorders},       0, "There is no aqorders" );
126
is( $report->{volumetry}->{aqorders},       0, "There is no aqorders" );
128
is( $report->{volumetry}->{subscription},   0, "There is no subscription" );
127
is( $report->{volumetry}->{subscription},   0, "There is no subscription" );
129
128
129
#after adding objects
130
construct_objects_needed();
130
construct_objects_needed();
131
131
132
#with objects
133
$report = C4::UsageStats->BuildReport();
132
$report = C4::UsageStats->BuildReport();
134
133
135
isa_ok( $report,              'HASH', '$report is a HASH' );
134
isa_ok( $report,              'HASH', '$report is a HASH' );
136
isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
135
isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
137
is( scalar( keys( $report->{volumetry} ) ), 8, "There are 8 fields in $report->{volumetry}" );
136
is( scalar( keys( $report->{volumetry} ) ), 8, "There are 8 fields in $report->{volumetry}" );
138
is( $report->{volumetry}->{biblio},         4, "There are 4 biblio" );
137
is( $report->{volumetry}->{biblio},         3, "There are 3 biblio" );
139
is( $report->{volumetry}->{items},          3, "There are 3 items" );
138
is( $report->{volumetry}->{items},          3, "There are 3 items" );
140
is( $report->{volumetry}->{auth_header},    2, "There are 2 auth_header" );
139
is( $report->{volumetry}->{auth_header},    2, "There are 2 auth_header" );
141
is( $report->{volumetry}->{old_issues},     1, "There is 1 old_issues" );
140
is( $report->{volumetry}->{old_issues},     1, "There is  1 old_issues" );
142
is( $report->{volumetry}->{old_reserves},   1, "There is 1 old_reserves" );
141
is( $report->{volumetry}->{old_reserves},   1, "There is  1 old_reserves" );
143
is( $report->{volumetry}->{borrowers},      3, "There are 3 borrowers" );
142
is( $report->{volumetry}->{borrowers},      3, "There are 3 borrowers" );
144
is( $report->{volumetry}->{aqorders},       1, "There is 1 aqorders" );
143
is( $report->{volumetry}->{aqorders},       1, "There is  1 aqorders" );
145
is( $report->{volumetry}->{subscription},   1, "There is 1 subscription" );
144
is( $report->{volumetry}->{subscription},   1, "There is  1 subscription" );
146
145
147
#Test report->systempreferences -------
146
#Test report->systempreferences -------
148
#mock to 0
147
#mock to 0
Lines 166-185 verif_systempreferences_values( $report, 1 ); Link Here
166
# ---------- Testing ReportToCommunity ----------
165
# ---------- Testing ReportToCommunity ----------
167
166
168
# ---------- Testing _count ---------------------
167
# ---------- Testing _count ---------------------
169
170
$dbh->do('DROP TABLE IF EXISTS _exmpl_tbl');
171
$dbh->do('CREATE TABLE _exmpl_tbl (id INT, val VARCHAR(10))');
172
$dbh->do( 'INSERT INTO _exmpl_tbl VALUES(1, ?)', undef, 'Hello' );
173
$dbh->do( 'INSERT INTO _exmpl_tbl VALUES(2, ?)', undef, 'World' );
174
175
my $query = '
168
my $query = '
176
  SELECT count(*)
169
  SELECT count(*)
177
  FROM   _exmpl_tbl
170
  FROM   borrowers
178
  ';
171
  ';
179
my $count = $dbh->selectrow_array($query);
172
my $count = $dbh->selectrow_array($query);
180
173
181
my $nb_fields = C4::UsageStats::_count('_exmpl_tbl');
174
my $nb_fields = C4::UsageStats::_count('borrowers');
182
is( $nb_fields, $count, "_exmpl_tbl has 2 fields" );
175
is( $nb_fields, $count, "_count return the good number of fields" );
183
176
184
#################################################
177
#################################################
185
#             Subs
178
#             Subs
Lines 204-231 sub construct_objects_needed { Link Here
204
    my $firstname1   = 'firstname 1';
197
    my $firstname1   = 'firstname 1';
205
    my $firstname2   = 'firstname 2';
198
    my $firstname2   = 'firstname 2';
206
    my $firstname3   = 'firstname 3';
199
    my $firstname3   = 'firstname 3';
207
    my $cardnumber1  = '00001';
200
    my $cardnumber1  = 'test_card1';
208
    my $cardnumber2  = '00002';
201
    my $cardnumber2  = 'test_card2';
209
    my $cardnumber3  = '00003';
202
    my $cardnumber3  = 'test_card3';
210
    my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
203
    my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
211
    my $branchcode   = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
204
    my $branchcode   = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
212
205
213
    my $query = '
206
    my $query = '
214
   INSERT INTO borrowers
207
    INSERT INTO borrowers
215
      (surname, firstname, cardnumber, branchcode, categorycode)
208
      (surname, firstname, cardnumber, branchcode, categorycode)
216
    VALUES (?,?,?,?,?)';
209
    VALUES (?,?,?,?,?)';
217
    my $insert_sth = $dbh->prepare($query);
210
    my $insert_sth = $dbh->prepare($query);
218
    $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
211
    $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
212
    my $borrowernumber1 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
219
    $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
213
    $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
214
    my $borrowernumber2 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
220
    $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
215
    $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
221
216
    my $borrowernumber3 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
222
    $query = '
223
    SELECT borrowernumber
224
    FROM   borrowers
225
    WHERE  surname = ?';
226
    my $borrowernumber1 = $dbh->selectrow_array( $query, {}, $surname1 );
227
    my $borrowernumber2 = $dbh->selectrow_array( $query, {}, $surname2 );
228
    my $borrowernumber3 = $dbh->selectrow_array( $query, {}, $surname3 );
229
217
230
    # ---------- 3 biblios -----------------------
218
    # ---------- 3 biblios -----------------------
231
    my $title1  = 'Title 1';
219
    my $title1  = 'Title 1';
Lines 253-269 sub construct_objects_needed { Link Here
253
      (biblionumber, itemtype, marcxml)
241
      (biblionumber, itemtype, marcxml)
254
    VALUES (?,?,?)';
242
    VALUES (?,?,?)';
255
    $insert_sth = $dbh->prepare($query);
243
    $insert_sth = $dbh->prepare($query);
256
    $insert_sth->execute( $biblionumber1, 'Book',  '' );
244
    $insert_sth->execute( $biblionumber1, 'Book', '' );
245
    my $biblioitemnumber1 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
257
    $insert_sth->execute( $biblionumber2, 'Music', '' );
246
    $insert_sth->execute( $biblionumber2, 'Music', '' );
258
    $insert_sth->execute( $biblionumber3, 'Book',  '' );
247
    my $biblioitemnumber2 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
259
248
    $insert_sth->execute( $biblionumber3, 'Book', '' );
260
    $query = '
249
    my $biblioitemnumber3 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
261
    SELECT biblioitemnumber
262
    FROM   biblioitems
263
    WHERE  biblionumber = ?';
264
    my $biblioitemnumber1 = $dbh->selectrow_array( $query, {}, $biblionumber1 );
265
    my $biblioitemnumber2 = $dbh->selectrow_array( $query, {}, $biblionumber2 );
266
    my $biblioitemnumber3 = $dbh->selectrow_array( $query, {}, $biblionumber3 );
267
250
268
    # ---------- 3 items  -------------------------
251
    # ---------- 3 items  -------------------------
269
    my $barcode1 = '111111';
252
    my $barcode1 = '111111';
Lines 276-291 sub construct_objects_needed { Link Here
276
    VALUES (?,?,?,?)';
259
    VALUES (?,?,?,?)';
277
    $insert_sth = $dbh->prepare($query);
260
    $insert_sth = $dbh->prepare($query);
278
    $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
261
    $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
262
    my $item_number1 = $dbh->last_insert_id( undef, undef, 'items', undef );
279
    $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
263
    $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
264
    my $item_number2 = $dbh->last_insert_id( undef, undef, 'items', undef );
280
    $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
265
    $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
281
266
    my $item_number3 = $dbh->last_insert_id( undef, undef, 'items', undef );
282
    $query = '
283
    SELECT itemnumber
284
    FROM   items
285
    WHERE  barcode = ?';
286
    my $item_number1 = $dbh->selectrow_array( $query, {}, $barcode1 );
287
    my $item_number2 = $dbh->selectrow_array( $query, {}, $barcode2 );
288
    my $item_number3 = $dbh->selectrow_array( $query, {}, $barcode3 );
289
267
290
    # ---------- Add 2 auth_header
268
    # ---------- Add 2 auth_header
291
    $query = '
269
    $query = '
Lines 294-307 sub construct_objects_needed { Link Here
294
    VALUES (?)';
272
    VALUES (?)';
295
    $insert_sth = $dbh->prepare($query);
273
    $insert_sth = $dbh->prepare($query);
296
    $insert_sth->execute('authtypecode1');
274
    $insert_sth->execute('authtypecode1');
275
    my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
297
    $insert_sth->execute('authtypecode2');
276
    $insert_sth->execute('authtypecode2');
298
277
    my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
299
    $query = '
300
    SELECT authid
301
    FROM   auth_header
302
    WHERE  authtypecode = ?';
303
    my $authid1 = $dbh->selectrow_array( $query, {}, 'authtypecode1' );
304
    my $authid2 = $dbh->selectrow_array( $query, {}, 'authtypecode2' );
305
278
306
    # ---------- Add 1 old_issues
279
    # ---------- Add 1 old_issues
307
    $query = '
280
    $query = '
Lines 310-321 sub construct_objects_needed { Link Here
310
    VALUES (?,?,?)';
283
    VALUES (?,?,?)';
311
    $insert_sth = $dbh->prepare($query);
284
    $insert_sth = $dbh->prepare($query);
312
    $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
285
    $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
313
286
    my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
314
    $query = '
315
    SELECT issue_id
316
    FROM   old_issues
317
    WHERE  borrowernumber = ?';
318
    my $issue_id1 = $dbh->selectrow_array( $query, {}, $borrowernumber1 );
319
287
320
    # ---------- Add 1 old_reserves
288
    # ---------- Add 1 old_reserves
321
    AddReserve( $branchcode, $borrowernumber1, $biblionumber1, 'a', '', 1, undef, undef, '', 'Title', undef, undef );
289
    AddReserve( $branchcode, $borrowernumber1, $biblionumber1, 'a', '', 1, undef, undef, '', 'Title', undef, undef );
Lines 323-338 sub construct_objects_needed { Link Here
323
    my $reserve_id1 = $reserves1->[0]->{reserve_id};
291
    my $reserve_id1 = $reserves1->[0]->{reserve_id};
324
    my $reserve1    = CancelReserve( { reserve_id => $reserve_id1 } );
292
    my $reserve1    = CancelReserve( { reserve_id => $reserve_id1 } );
325
293
326
    # ---------- Add 1 biblio, 1 subscription and 1 aqorder
294
    # ---------- Add 1 aqbudgets
327
    my $builder = t::lib::TestBuilder->new();
295
    $query = '
328
    $builder->clear( { source => 'Aqorder' } );
296
    INSERT INTO aqbudgets
329
    my $order1 = $builder->build(
297
      (budget_amount)
330
        {   source  => 'Aqorder',
298
    VALUES (?)';
331
            value   => { datecancellationprinted => undef, },
299
    $insert_sth = $dbh->prepare($query);
332
            only_fk => 1,
300
    $insert_sth->execute("20.0");
333
        }
301
    my $aqbudgets1 = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
334
    );
302
335
    my $newordernumber = Koha::Acquisition::Order->new($order1)->insert->{ordernumber};
303
    # ---------- Add 1 aqorders
304
    $query = '
305
    INSERT INTO aqorders
306
      (budget_id, basketno, biblionumber, invoiceid, subscriptionid)
307
    VALUES (?,?,?,?,?)';
308
    $insert_sth = $dbh->prepare($query);
309
    $insert_sth->execute( $aqbudgets1, undef, undef, undef, undef );
310
    my $aqorders1 = $dbh->last_insert_id( undef, undef, 'aqorders', undef );
311
312
    # --------- Add 1 subscription
313
    $query = '
314
    INSERT INTO subscription
315
      (biblionumber)
316
    VALUES (?)';
317
    $insert_sth = $dbh->prepare($query);
318
    $insert_sth->execute($biblionumber1);
319
    my $subscription1 = $dbh->last_insert_id( undef, undef, 'subscription', undef );
320
336
}
321
}
337
322
338
#Change systempreferences values to $set_value
323
#Change systempreferences values to $set_value
339
- 

Return to bug 14097