From e0444e8600efbe06016ff857f23ea85893a93041 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Mon, 15 May 2017 09:58:04 +0200
Subject: [PATCH] Bug 18601: OAI/Sets.t mangles data due to truncate in
 ModOAISetsBiblios

This patch replaces the TRUNCATE statement in ModOAISetsBiblios by a
DELETE statement. A truncate will cause an implicit commit and will
therefore commit the transaction started in the test script.

Also simplifying the module load in the test script.

Test plan:
Do not apply this patch and observe that biblio records are added to your
database by running t/db_dependent/OAI/Sets.t.
Apply this patch, run the test again and verify that it does no longer
add records to your biblio table.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 C4/OAI/Sets.pm            |  4 ++--
 t/db_dependent/OAI/Sets.t | 40 ++++++++++------------------------------
 2 files changed, 12 insertions(+), 32 deletions(-)

diff --git a/C4/OAI/Sets.pm b/C4/OAI/Sets.pm
index e2d8586efe..62c98d8801 100644
--- a/C4/OAI/Sets.pm
+++ b/C4/OAI/Sets.pm
@@ -525,7 +525,7 @@ sub CalcOAISetsBiblio {
     };
     ModOAISetsBiblios($oai_sets_biblios);
 
-ModOAISetsBiblios truncate oai_sets_biblios table and call AddOAISetsBiblios.
+ModOAISetsBiblios deletes all records from oai_sets_biblios table and calls AddOAISetsBiblios.
 This table is then used in opac/oai.pl.
 
 =cut
@@ -537,7 +537,7 @@ sub ModOAISetsBiblios {
 
     my $dbh = C4::Context->dbh;
     my $query = qq{
-        TRUNCATE TABLE oai_sets_biblios
+        DELETE FROM oai_sets_biblios
     };
     my $sth = $dbh->prepare($query);
     $sth->execute;
diff --git a/t/db_dependent/OAI/Sets.t b/t/db_dependent/OAI/Sets.t
index b1ce6ab86f..33b12e0d79 100644
--- a/t/db_dependent/OAI/Sets.t
+++ b/t/db_dependent/OAI/Sets.t
@@ -17,40 +17,20 @@
 # with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use C4::Context;
-use Test::More tests => 148;
+
+use Test::More tests => 144;
 use Test::MockModule;
 use Test::Warn;
+use MARC::Record;
 
+use Koha::Database;
+use C4::Biblio;
+use C4::OAI::Sets;
 
-BEGIN {
-    use_ok('C4::OAI::Sets');
-    use_ok('MARC::Record');
-    use_ok('C4::Biblio');
-}
-can_ok(
-    'C4::OAI::Sets', qw(
-        GetOAISets
-        GetOAISet
-        GetOAISetBySpec
-        ModOAISet
-        DelOAISet
-        AddOAISet
-        GetOAISetsMappings
-        GetOAISetMappings
-        ModOAISetMappings
-        GetOAISetsBiblio
-        DelOAISetsBiblio
-        CalcOAISetsBiblio
-        ModOAISetsBiblios
-        UpdateOAISetsBiblio
-        AddOAISetsBiblios )
-);
-
-
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+
 $dbh->do('DELETE FROM oai_sets');
 $dbh->do('DELETE FROM oai_sets_descriptions');
 $dbh->do('DELETE FROM oai_sets_mappings');
@@ -632,4 +612,4 @@ sub create_helper_biblio {
     return $biblionumber;
 }
 
-$dbh->rollback;
\ No newline at end of file
+$schema->storage->txn_rollback;
-- 
2.11.0