Bug 29234: Further clean Z3950 Tests
[koha-ffzg.git] / t / db_dependent / Koha / Z3950Responder / Session2.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 3;
5 use t::lib::TestBuilder;
6 use C4::Items qw( GetMarcItem );
7
8 use Koha::AuthorisedValues;
9 use Koha::Caches;
10
11 BEGIN {
12     use_ok('Koha::Z3950Responder');
13     use_ok('Koha::Z3950Responder::Session');
14 }
15
16 my $builder = t::lib::TestBuilder->new;
17 my $schema  = Koha::Database->new->schema;
18
19 $schema->storage->txn_begin;
20
21 # Clear the cache, before and after
22 Koha::Caches->get_instance->flush_all;
23
24 subtest 'add_item_status' => sub {
25
26     plan tests => 2;
27
28     # This time we are sustituting some values
29
30     my $available = Koha::AuthorisedValues->find({ category => 'Z3950_STATUS', authorised_value => 'AVAILABLE' });
31     unless( $available ){
32         $available = $builder->build_object({
33             class => 'Koha::AuthorisedValues',
34             value => {
35                 category => 'Z3950_STATUS',
36                 authorised_value => 'AVAILABLE',
37                 lib => "Free as a bird"
38             }
39         });
40     }
41     my $available_status = $available->lib;
42
43     my $damaged = Koha::AuthorisedValues->find({ category => 'Z3950_STATUS', authorised_value => 'DAMAGED' });
44     unless( $damaged ){
45         $damaged = $builder->build_object({
46             class => 'Koha::AuthorisedValues',
47             value => {
48                 category => 'Z3950_STATUS',
49                 authorised_value => 'DAMAGED',
50                 lib => "Borked completely"
51             }
52         });
53     }
54     my $damaged_status = $damaged->lib;
55
56     ## FIRST ITEM HAS ALL THE STATUSES ##
57     my $item_1 = $builder->build_sample_item(
58         {
59             onloan     => '2017-07-07',
60             itemlost   => 1,
61             notforloan => 1,
62             damaged    => 1,
63             withdrawn  => 1,
64         }
65     );
66     my $item_marc_1 = C4::Items::GetMarcItem( $item_1->biblionumber, $item_1->itemnumber );
67     my $item_field_1 = scalar $item_marc_1->field('952');
68     $builder->build({ source => 'Reserve', value=> { itemnumber => $item_1->itemnumber } });
69     $builder->build(
70         {
71             source => 'Branchtransfer',
72             value  => {
73                 itemnumber    => $item_1->itemnumber,
74                 datearrived   => undef,
75                 datecancelled => undef,
76                 datesent      => \'NOW()',
77             }
78         }
79     );
80     ## END FIRST ITEM ##
81
82     ## SECOND ITEM HAS NO STATUSES ##
83     my $item_2 = $builder->build_sample_item;
84     my $item_marc_2 = C4::Items::GetMarcItem( $item_2->biblionumber, $item_2->itemnumber );
85     my $item_field_2 = scalar $item_marc_2->field('952');
86     ## END SECOND ITEM ##
87
88     # Create the responder
89     my $args={ PEER_NAME => 'PEER'};
90     my $zR = Koha::Z3950Responder->new({add_item_status_subfield => 'k'});
91     $zR->init_handler($args);
92
93     $args->{HANDLE}->add_item_status($item_field_1);
94     is($item_field_1->subfield('k'),"Checked Out, Lost, Not for Loan, $damaged_status, Withdrawn, In Transit, On Hold","All statuses added in one field as expected");
95
96     $args->{HANDLE}->add_item_status($item_field_2);
97     is($item_field_2->subfield('k'),"$available_status","Available status is '$available_status' added as expected");
98
99 };
100
101 # Clear the cache, before and after
102 Koha::Caches->get_instance->flush_all;
103
104 $schema->storage->txn_rollback;