Bug 7804 - Add Koha Plugin System - Unit Tests
[koha.git] / t / db_dependent / AuthoritiesMarc.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!  
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8
9 use Test::More tests => 5;
10 use Test::MockModule;
11 use MARC::Record;
12
13 BEGIN {
14         use_ok('C4::AuthoritiesMarc');
15 }
16
17 # We are now going to be testing the authorities hierarchy code, and
18 # therefore need to pretend that we have consistent data in our database
19 my $module = new Test::MockModule('C4::AuthoritiesMarc');
20 $module->mock('GetHeaderAuthority', sub {
21     return {'authtrees' => ''};
22 });
23 $module->mock('AddAuthorityTrees', sub {
24     return;
25 });
26 $module->mock('GetAuthority', sub {
27     my ($authid) = @_;
28     my $record = MARC::Record->new();
29     if ($authid eq '1') {
30         $record->add_fields(
31             [ '001', '1' ],
32             [ '151', ' ', ' ', a => 'United States' ]
33             );
34     } elsif ($authid eq '2') {
35         $record->add_fields(
36             [ '001', '2' ],
37             [ '151', ' ', ' ', a => 'New York (State)' ],
38             [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
39             );
40     } elsif ($authid eq '3') {
41         $record->add_fields(
42             [ '001', '3' ],
43             [ '151', ' ', ' ', a => 'New York (City)' ],
44             [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
45             );
46     } elsif ($authid eq '4') {
47         $record->add_fields(
48             [ '001', '4' ],
49             [ '151', ' ', ' ', a => 'New York (City)' ],
50             [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
51             );
52     } else {
53         undef $record;
54     }
55     return $record;
56 });
57
58 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
59
60 my $expectedhierarchy = [ [ {
61         'authid' => '1',
62         'value' => 'United States',
63         'class' => 'child0',
64         'children' => [ {
65             'authid' => '2',
66             'value' => 'New York (State)',
67             'class' => 'child1',
68             'children' => [ {
69                 'authid' => '3',
70                 'current_value' => 1,
71                 'value' => 'New York (City)',
72                 'class' => 'child2',
73                 'children' => [],
74                 'parents' => [ {
75                     'authid' => '2',
76                     'value' => 'New York (State)'
77                 } ]
78             } ],
79             'parents' => [ {
80                 'authid' => '1',
81                 'value' => 'United States'
82             } ]
83         } ],
84         'parents' => []
85 } ] ];
86
87 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
88
89 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
90 $expectedhierarchy = [ [ {
91     'authid' => '4',
92     'current_value' => 1,
93     'value' => 'New York (City)',
94     'class' => 'child0',
95     'children' => [],
96     'parents' => []
97 } ] ];
98 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");