Bug 19243: Fix tests for patron categories
[koha.git] / t / db_dependent / UsageStats.t
index d11cbd4..2b33165 100644 (file)
@@ -17,6 +17,7 @@
 use Modern::Perl;
 use Test::More tests => 57;
 use t::lib::Mocks qw(mock_preference);
+use t::lib::TestBuilder;
 use POSIX qw(strftime);
 use Data::Dumper;
 use Koha::Biblios;
@@ -30,7 +31,7 @@ BEGIN {
     use_ok( 'C4::AuthoritiesMarc', qw(AddAuthority) );
     use_ok('C4::Reserves');
     use_ok('MARC::Record');
-    use_ok('Koha::Acquisition::Order');
+    use_ok('Koha::Acquisition::Orders');
 }
 
 can_ok(
@@ -41,9 +42,10 @@ can_ok(
       _count )
 );
 
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
 
 $dbh->do('DELETE FROM issues');
 $dbh->do('DELETE FROM biblio');
@@ -213,8 +215,8 @@ sub construct_objects_needed {
     my $cardnumber1  = 'test_card1';
     my $cardnumber2  = 'test_card2';
     my $cardnumber3  = 'test_card3';
-    my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
-    my $branchcode   = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
 
     my $query = '
     INSERT INTO borrowers
@@ -238,8 +240,8 @@ sub construct_objects_needed {
 
     $query = '
     INSERT INTO biblio
-      (title, author)
-    VALUES (?,?)';
+      (title, author, datecreated)
+    VALUES (?,?, NOW())';
     $insert_sth = $dbh->prepare($query);
     $insert_sth->execute( $title1, $author1 );
     my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
@@ -281,19 +283,19 @@ sub construct_objects_needed {
     # ---------- Add 2 auth_header
     $query = '
     INSERT INTO auth_header
-      (authtypecode)
-    VALUES (?)';
+      (authtypecode, marcxml)
+    VALUES (?, "")';
     $insert_sth = $dbh->prepare($query);
-    $insert_sth->execute('authtypecode1');
+    $insert_sth->execute('atc1');
     my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
-    $insert_sth->execute('authtypecode2');
+    $insert_sth->execute('atc2');
     my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
 
     # ---------- Add 1 old_issues
     $query = '
     INSERT INTO old_issues
-      (borrowernumber, branchcode, itemnumber)
-    VALUES (?,?,?)';
+      (issue_id, borrowernumber, branchcode, itemnumber)
+    VALUES ((select coalesce(max(issue_id), 0)+1 from issues),?,?,?)';
     $insert_sth = $dbh->prepare($query);
     $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
     my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
@@ -302,7 +304,7 @@ sub construct_objects_needed {
     AddReserve( $branchcode, $borrowernumber1, $biblionumber1, '', 1, undef, undef, '', 'Title', undef, undef );
     my $biblio = Koha::Biblios->find( $biblionumber1 );
     my $holds = $biblio->holds;
-    CancelReserve( { reserve_id => $holds->next->reserve_id } );
+    $holds->next->cancel if $holds->count;
 
     # ---------- Add 1 aqbudgets
     $query = '
@@ -378,6 +380,7 @@ sub mocking_systempreferences_to_a_set_value {
         z3950NormalizeAuthor
         SpineLabelAutoPrint
         SpineLabelShowPrintOnBibDetails
+        BlockReturnOfLostItems
         BlockReturnOfWithdrawnItems
         CalculateFinesOnReturn
         AgeRestrictionOverride
@@ -494,11 +497,9 @@ sub mocking_systempreferences_to_a_set_value {
         OpacMaintenance
         OpacPublic
         OpacSeparateHoldings
-        OPACShowBarcode
         OPACShowCheckoutName
         OpacShowFiltersPulldownMobile
         OPACShowHoldQueueDetails
-        OpacShowLibrariesPulldownMobile
         OpacShowRecentComments
         OPACShowUnusedAuthorities
         OpacStarRatings
@@ -549,7 +550,6 @@ sub mocking_systempreferences_to_a_set_value {
         EnhancedMessagingPreferences
         ExtendedPatronAttributes
         intranetreadinghistory
-        memberofinstitution
         patronimages
         TalkingTechItivaPhoneNotification
         uppercasesurnames
@@ -613,4 +613,4 @@ sub verif_systempreferences_values {
     }
 }
 
-$dbh->rollback;
+$schema->storage->txn_rollback;