| Line 0
          
      
      
        Link Here | 
          
            
              | 0 | -  | 1 | #!/usr/bin/perl | 
            
              |  |  | 2 |  | 
            
              | 3 | # This file is part of Koha. | 
            
              | 4 | # | 
            
              | 5 | # Koha is free software; you can redistribute it and/or modify it | 
            
              | 6 | # under the terms of the GNU General Public License as published by | 
            
              | 7 | # the Free Software Foundation; either version 3 of the License, or | 
            
              | 8 | # (at your option) any later version. | 
            
              | 9 | # | 
            
              | 10 | # Koha is distributed in the hope that it will be useful, but | 
            
              | 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
              | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
              | 13 | # GNU General Public License for more details. | 
            
              | 14 | # | 
            
              | 15 | # You should have received a copy of the GNU General Public License | 
            
              | 16 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | 
            
              | 17 |  | 
            
              | 18 | use Modern::Perl; | 
            
              | 19 | use utf8; | 
            
              | 20 |  | 
            
              | 21 | use Test::More tests => 4; | 
            
              | 22 | use Test::MockModule; | 
            
              | 23 |  | 
            
              | 24 | use t::lib::Mocks; | 
            
              | 25 | use t::lib::TestBuilder; | 
            
              | 26 |  | 
            
              | 27 | use MARC::Record; | 
            
              | 28 |  | 
            
              | 29 | use_ok('Koha::Acquisition::Utils'); | 
            
              | 30 |  | 
            
              | 31 | my $schema = Koha::Database->schema; | 
            
              | 32 | $schema->storage->txn_begin; | 
            
              | 33 | my $builder = t::lib::TestBuilder->new; | 
            
              | 34 | my $dbh = C4::Context->dbh; | 
            
              | 35 |  | 
            
              | 36 | subtest "get_infos_syspref" => sub { | 
            
              | 37 |     plan tests => 4; | 
            
              | 38 |  | 
            
              | 39 |     my $record = MARC::Record->new; | 
            
              | 40 |     $record->append_fields( | 
            
              | 41 |         MARC::Field->new( '500', '', '', a => 'Test 1' ), | 
            
              | 42 |         MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), | 
            
              | 43 |         MARC::Field->new( '520', '', '', a => 'Test 3' ), | 
            
              | 44 |         MARC::Field->new( '541', '', '', a => 'Test 4' ), | 
            
              | 45 |     ); | 
            
              | 46 |  | 
            
              | 47 |     my $MarcFieldsToOrder = q{ | 
            
              | 48 | test1: 500$a | 
            
              | 49 | test2: 505$a | 
            
              | 50 | test3: 520$a | 
            
              | 51 | test4: 541$a | 
            
              | 52 |     }; | 
            
              | 53 |     t::lib::Mocks::mock_preference('MarcFieldsToOrder', $MarcFieldsToOrder); | 
            
              | 54 |     my $data = Koha::Acquisition::Utils::get_infos_syspref( | 
            
              | 55 |         'MarcFieldsToOrder', | 
            
              | 56 |         $record, | 
            
              | 57 |         [ 'test1', 'test2', 'test3', 'test4' ] | 
            
              | 58 |     ); | 
            
              | 59 |  | 
            
              | 60 |     is( $data->{test1}, "Test 1", "Got test 1 correctly" ); | 
            
              | 61 |     is( $data->{test2}, "Test 2", "Got test 2 correctly" ); | 
            
              | 62 |     is( $data->{test3}, "Test 3", "Got test 3 correctly" ); | 
            
              | 63 |     is( $data->{test4}, "Test 4", "Got test 4 correctly" ); | 
            
              | 64 | }; | 
            
              | 65 |  | 
            
              | 66 | subtest "get_infos_syspref_on_item" => sub { | 
            
              | 67 |     plan tests => 13; | 
            
              | 68 |  | 
            
              | 69 |     my $record = MARC::Record->new; | 
            
              | 70 |     $record->append_fields( | 
            
              | 71 |         MARC::Field->new( '500', '', '', a => 'Test 1' ), | 
            
              | 72 |         MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), | 
            
              | 73 |         MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ), | 
            
              | 74 |         MARC::Field->new( '975', '', '', a => 'Test 5', b => "Test 6" ), | 
            
              | 75 |         MARC::Field->new( '975', '', '', a => 'Test 7', b => "Test 8" ), | 
            
              | 76 |         MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ), | 
            
              | 77 |         MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ), | 
            
              | 78 |         MARC::Field->new( '976', '', '', a => 'Test 13', b => "Test 14" ), | 
            
              | 79 |     ); | 
            
              | 80 |  | 
            
              | 81 |     my $MarcItemFieldsToOrder = q{ | 
            
              | 82 | testA: 975$a | 
            
              | 83 | testB: 975$b | 
            
              | 84 | testC: 976$a | 
            
              | 85 | testD: 976$b | 
            
              | 86 |     }; | 
            
              | 87 |     t::lib::Mocks::mock_preference('MarcItemFieldsToOrder', $MarcItemFieldsToOrder); | 
            
              | 88 |     my $data = Koha::Acquisition::Utils::get_infos_syspref_on_item( | 
            
              | 89 |         'MarcItemFieldsToOrder', | 
            
              | 90 |         $record, | 
            
              | 91 |         [ 'testA', 'testB', 'testC', 'testD' ] | 
            
              | 92 |     ); | 
            
              | 93 |  | 
            
              | 94 |     is( $data->[0]->{testA}, "Test 3", 'Got first 975$a correctly' ); | 
            
              | 95 |     is( $data->[0]->{testB}, "Test 4", 'Got first 975$b correctly' ); | 
            
              | 96 |     is( $data->[1]->{testA}, "Test 5", 'Got second 975$a correctly' ); | 
            
              | 97 |     is( $data->[1]->{testB}, "Test 6", 'Got second 975$b correctly' ); | 
            
              | 98 |     is( $data->[2]->{testA}, "Test 7", 'Got third 975$a correctly' ); | 
            
              | 99 |     is( $data->[2]->{testB}, "Test 8", 'Got third 975$b correctly' ); | 
            
              | 100 |  | 
            
              | 101 |     is( $data->[0]->{testC}, "Test 9", 'Got first 976$a correctly' ); | 
            
              | 102 |     is( $data->[0]->{testD}, "Test 10", 'Got first 976$b correctly' ); | 
            
              | 103 |     is( $data->[1]->{testC}, "Test 11", 'Got second 976$a correctly' ); | 
            
              | 104 |     is( $data->[1]->{testD}, "Test 12", 'Got second 976$b correctly' ); | 
            
              | 105 |     is( $data->[2]->{testC}, "Test 13", 'Got third 976$a correctly' ); | 
            
              | 106 |     is( $data->[2]->{testD}, "Test 14", 'Got third 976$b correctly' ); | 
            
              | 107 |  | 
            
              | 108 |     # Test with bad record where fields are not one-to-one | 
            
              | 109 |     $record->append_fields( | 
            
              | 110 |         MARC::Field->new( '500', '', '', a => 'Test 1' ), | 
            
              | 111 |         MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), | 
            
              | 112 |         MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ), | 
            
              | 113 |         MARC::Field->new( '975', '', '', b => "Test 6" ), | 
            
              | 114 |         MARC::Field->new( '975', '', '', b => 'Test 7' ), | 
            
              | 115 |         MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ), | 
            
              | 116 |         MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ), | 
            
              | 117 |     ); | 
            
              | 118 |  | 
            
              | 119 |     $data = Koha::Acquisition::Utils::get_infos_syspref_on_item( | 
            
              | 120 |         'MarcItemFieldsToOrder', | 
            
              | 121 |         $record, | 
            
              | 122 |         [ 'testA', 'testB', 'testC', 'testD' ] | 
            
              | 123 |     ); | 
            
              | 124 |     is( $data, -1, "Got -1 if record fields are not one-to-one"); | 
            
              | 125 | }; | 
            
              | 126 |  | 
            
              | 127 | subtest "equal_number_of_fields" => sub { | 
            
              | 128 |     plan tests => 2; | 
            
              | 129 |  | 
            
              | 130 |     my $record = MARC::Record->new; | 
            
              | 131 |     $record->append_fields( | 
            
              | 132 |         MARC::Field->new( '500', '', '', a => 'Test 1' ), | 
            
              | 133 |         MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), | 
            
              | 134 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 135 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 136 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 137 |         MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 138 |         MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 139 |         MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 140 |     ); | 
            
              | 141 |  | 
            
              | 142 |     my $data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record ); | 
            
              | 143 |     is( $data, '3', "Got correct number of fields in return value" ); | 
            
              | 144 |  | 
            
              | 145 |     # Test with non-matching field sets | 
            
              | 146 |     $record->append_fields( | 
            
              | 147 |         MARC::Field->new( '500', '', '', a => 'Test 1' ), | 
            
              | 148 |         MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), | 
            
              | 149 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 150 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 151 |         MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 152 |         MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 153 |         MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), | 
            
              | 154 |     ); | 
            
              | 155 |  | 
            
              | 156 |     $data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record ); | 
            
              | 157 |     is( $data, '-1', "Got -1 in return value" ); | 
            
              | 158 | }; | 
            
              | 159 |  | 
            
              | 160 | $schema->storage->txn_rollback; | 
            
              | 161 | C4::Context->clear_syspref_cache(); |