Bug 18226: Perltidy + license
[koha-ffzg.git] / t / db_dependent / Biblio.t
index e6427f4..08e09a1 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 use Test::MockModule;
 
 use List::MoreUtils qw( uniq );
 use MARC::Record;
 use t::lib::Mocks qw( mock_preference );
 
+use Koha::Database;
+
 BEGIN {
     use_ok('C4::Biblio');
 }
 
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
 
 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
     plan tests => 23;
@@ -108,8 +109,7 @@ sub run_tests {
 
     # Generate a record with just the ISBN
     my $marc_record = MARC::Record->new;
-    my $isbn_field  = create_isbn_field( $isbn, $marcflavour );
-    $marc_record->append_fields( $isbn_field );
+    $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
 
     # Add the record to the DB
     my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
@@ -119,6 +119,10 @@ sub run_tests {
     is( $data->{ title }, undef,
         '(GetBiblioData) Title field is empty in fresh biblio.');
 
+    my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
+    my $marc = GetMarcBiblio( $biblionumber );
+    is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
+
     # Add title
     my $field = create_title_field( $title, $marcflavour );
     $marc_record->append_fields( $field );
@@ -127,6 +131,9 @@ sub run_tests {
     is( $data->{ title }, $title,
         'ModBiblio correctly added the title field, and GetBiblioData.');
     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
+    $marc = GetMarcBiblio( $biblionumber );
+    my ( $title_field, $title_subfield ) = get_title_field();
+    is( $marc->subfield( $title_field, $title_subfield ), $title, );
 
     my $itemdata = GetBiblioItemData( $biblioitemnumber );
     is( $itemdata->{ title }, $title,
@@ -239,7 +246,7 @@ sub run_tests {
     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
     for my $i (0 .. $#more_isbns) {
         is( $isbns->[$i], $more_isbns[$i],
-            "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
+            "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
     }
 
     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
@@ -266,6 +273,17 @@ sub run_tests {
     is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
         ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
         'Check the number of returned notes of GetMarcNotes' );
+
+    # test for GetMarcUrls
+    $marc_record->append_fields(
+        MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
+        MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
+    );
+    my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
+    is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
+    like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
+    ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
+        'GetMarcUrls prefixed a MARC21 URL with http://' );
 }
 
 sub get_title_field {
@@ -320,21 +338,24 @@ sub create_issn_field {
 }
 
 subtest 'MARC21' => sub {
-    plan tests => 29;
+    plan tests => 34;
     run_tests('MARC21');
-    $dbh->rollback;
+    $schema->storage->txn_rollback;
+    $schema->storage->txn_begin;
 };
 
 subtest 'UNIMARC' => sub {
-    plan tests => 29;
+    plan tests => 34;
     run_tests('UNIMARC');
-    $dbh->rollback;
+    $schema->storage->txn_rollback;
+    $schema->storage->txn_begin;
 };
 
 subtest 'NORMARC' => sub {
-    plan tests => 29;
+    plan tests => 34;
     run_tests('NORMARC');
-    $dbh->rollback;
+    $schema->storage->txn_rollback;
+    $schema->storage->txn_begin;
 };
 
 subtest 'IsMarcStructureInternal' => sub {
@@ -356,4 +377,16 @@ subtest 'IsMarcStructureInternal' => sub {
     is( grep( /^a$/, @internals ), 0, 'no subfield a' );
 };
 
+subtest 'deletedbiblio_metadata' => sub {
+    plan tests => 2;
+
+    my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
+    my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
+    C4::Biblio::DelBiblio( $biblionumber );
+    my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
+    is( $moved, $biblionumber, 'Found in deletedbiblio' );
+    ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
+    is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
+};
+
 1;