Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / AuthoritiesMarc.t
index 57fcc88..f72b0c3 100755 (executable)
@@ -5,21 +5,24 @@
 
 use Modern::Perl;
 
-use Test::More tests => 9;
+use Test::More tests => 12;
 use Test::MockModule;
 use Test::Warn;
+use MARC::Field;
 use MARC::Record;
 
 use t::lib::Mocks;
+use t::lib::TestBuilder;
 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
 # therefore need to pretend that we have consistent data in our database
-my $module = new Test::MockModule('C4::AuthoritiesMarc');
+my $module = Test::MockModule->new('C4::AuthoritiesMarc');
 $module->mock('GetHeaderAuthority', sub {
     return {'authtrees' => ''};
 });
@@ -52,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;
     }
@@ -61,9 +69,15 @@ $module->mock('GetAuthority', sub {
 my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
+my $builder = t::lib::TestBuilder->new;
 
 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
 
+# Authority type GEOGR_NAME is hardcoded here
+if( ! Koha::Authority::Types->find('GEOGR_NAME') ) {
+    $builder->build({ source => 'AuthType', value => { authtypecode => 'GEOGR_NAME' }});
+};
+
 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
 
 my $expectedhierarchy = [ [ {
@@ -197,7 +211,7 @@ subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
     my $record = C4::AuthoritiesMarc::GetAuthority(1);
     my $id1 = AddAuthority( $record, undef, 'GEOGR_NAME' );
-    DelAuthority( $id1 );
+    DelAuthority({ authid => $id1 });
     my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
     isnt( $id1, $id2, 'Do not return the same id again' );
     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
@@ -206,4 +220,90 @@ 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');
+
+subtest 'ModAuthority() 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
+        { ModAuthority( $auth_id, $record, $auth_type ); }
+        'merge called',
+        'No param, merge called';
+
+    warning_is
+        { ModAuthority( $auth_id, $record, $auth_type, { skip_merge => 1 } ); }
+        undef,
+        'skip_merge passed, merge not called';
+
+    $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;
+};