Bug 30982: Add Koha::BackgroundJobs->filter_by_current
[koha-ffzg.git] / t / db_dependent / AuthoritiesMarc.t
index c55474c..f72b0c3 100755 (executable)
@@ -5,9 +5,10 @@
 
 use Modern::Perl;
 
-use Test::More tests => 10;
+use Test::More tests => 12;
 use Test::MockModule;
 use Test::Warn;
+use MARC::Field;
 use MARC::Record;
 
 use t::lib::Mocks;
@@ -16,7 +17,7 @@ use Koha::Database;
 use Koha::Authority::Types;
 
 BEGIN {
-        use_ok('C4::AuthoritiesMarc');
+        use_ok('C4::AuthoritiesMarc', qw( GetHeaderAuthority AddAuthority AddAuthorityTrees GetAuthority BuildAuthHierarchies GenerateHierarchy BuildSummary DelAuthority CompareFieldWithAuthority ModAuthority merge ));
 }
 
 # We are now going to be testing the authorities hierarchy code, and
@@ -54,6 +55,11 @@ $module->mock('GetAuthority', sub {
             [ '151', ' ', ' ', a => 'New York (City)' ],
             [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
             );
+    } elsif ($authid eq '5') {
+        $record->add_fields(
+            [ '001', '5' ],
+            [ '100', ' ', ' ', a => 'Lastname, Firstname', b => 'b', c => 'c', i => 'i' ]
+            );
     } else {
         undef $record;
     }
@@ -214,6 +220,26 @@ subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
     is( $record->field('001')->data, $id3, 'Check updated 001' );
 };
 
+subtest 'CompareFieldWithAuthority tests' => sub {
+    plan tests => 3;
+
+    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
+
+    $builder->build({ source => 'AuthType', value => { authtypecode => 'PERSO_NAME' }});
+
+    my $field = MARC::Field->new('100', 0, 0, a => 'Lastname, Firstname', b => 'b', c => 'c');
+
+    ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority matches');
+
+    $field->add_subfields(i => 'X');
+
+    ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Compare ignores unlisted subfields');
+
+    $field->add_subfields(d => 'd');
+
+    ok(!C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority does not match');
+};
+
 $schema->storage->txn_rollback;
 
 $module->unmock('GetAuthority');
@@ -248,3 +274,36 @@ subtest 'ModAuthority() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'DelAuthority() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $auth_type = 'GEOGR_NAME';
+    my $record  = MARC::Record->new;
+    $record->add_fields(
+            [ '001', '1' ],
+            [ '151', ' ', ' ', a => 'United States' ]
+            );
+;
+    my $auth_id = AddAuthority( $record, undef, $auth_type );
+
+    my $mocked_authorities_marc = Test::MockModule->new('C4::AuthoritiesMarc');
+    $mocked_authorities_marc->mock( 'merge', sub { warn 'merge called'; } );
+
+    warning_is
+        { DelAuthority({ authid => $auth_id }); }
+        'merge called',
+        'No param, merge called';
+
+    $auth_id = AddAuthority( $record, undef, $auth_type );
+
+    warning_is
+        { DelAuthority({ authid => $auth_id, skip_merge => 1 }); }
+        undef,
+        'skip_merge passed, merge not called';
+
+    $schema->storage->txn_rollback;
+};