Bug 5327 shifting database dependent modules and scripts to t/db_dependent
[koha.git] / t / db_dependent / lib / KohaTest / Items / GetItemsForInventory.pm
1 package KohaTest::Items::GetItemsForInventory;
2 use base qw( KohaTest::Items );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Items;
10
11 =head2 STARTUP METHODS
12
13 These get run once, before the main test methods in this module
14
15 =cut
16
17 =head2 startup_90_add_item_get_callnumber
18
19 =cut
20
21 sub startup_90_add_item_get_callnumber : Test( startup => 13 ) {
22     my $self = shift;
23
24     $self->add_biblios( add_items => 1 );
25
26     ok( $self->{'items'}, 'An item has been aded' )
27       or diag( Data::Dumper->Dump( [ $self->{'items'} ], ['items'] ) );
28
29     my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber( $self->{'items'}[0]{'biblionumber'} );
30     ok( $biblioitems[0]->{'biblioitemnumber'}, '...and it has a biblioitemnumber' )
31       or diag( Data::Dumper->Dump( [ \@biblioitems ], ['biblioitems'] ) );
32
33     my $items_info = GetItemsByBiblioitemnumber( $biblioitems[0]->{'biblioitemnumber'} );
34     isa_ok( $items_info, 'ARRAY', '...and we can search with that biblioitemnumber' )
35       or diag( Data::Dumper->Dump( [$items_info], ['items_info'] ) );
36     cmp_ok( scalar @$items_info, '>', 0, '...and we can find at least one item with that biblioitemnumber' );
37
38     my $item_info = $items_info->[0];
39     ok( $item_info->{'itemcallnumber'}, '...and the item we found has a call number: ' . $item_info->{'itemcallnumber'} )
40       or diag( Data::Dumper->Dump( [$item_info], ['item_info'] ) );
41
42     $self->{'callnumber'} = $item_info->{'itemcallnumber'};
43 }
44
45
46 =head2 TEST METHODS
47
48 standard test methods
49
50 =head3 missing_parameters
51
52 the minlocation and maxlocation parameters are required. If they are
53 not provided, this method should somehow complain, such as returning
54 undef or emitina warning or something.
55
56 =cut
57
58 sub missing_parameters : Test( 1 ) {
59     my $self = shift;
60     local $TODO = 'GetItemsForInventory should fail when missing required parameters';
61
62     my $items = C4::Items::GetItemsForInventory();
63     ok( ! defined $items, 'GetItemsForInventory fails when parameters are missing' )
64       or diag( Data::Dumper->Dump( [ $items ], [ 'items' ] ) );
65 }
66
67 =head3 basic_usage
68
69
70 =cut
71
72 sub basic_usage : Test( 4 ) {
73     my $self = shift;
74
75     ok( $self->{'callnumber'}, 'we have a call number to search for: ' . $self->{'callnumber'} );
76     my $items = C4::Items::GetItemsForInventory( $self->{'callnumber'}, $self->{'callnumber'} );
77     isa_ok( $items, 'ARRAY', 'We were able to call GetItemsForInventory with our call number' );
78     is( scalar @$items, 1, '...and we found only one item' );
79     my $our_item = $items->[0];
80     is( $our_item->{'itemnumber'},     $self->{'items'}[0]{'itemnumber'},                 '...and the item we found has the right itemnumber' );
81
82     # diag( Data::Dumper->Dump( [$items], ['items'] ) );
83 }
84
85 =head3 date_last_seen
86
87
88 =cut
89
90 sub date_last_seen : Test( 6 ) {
91     my $self = shift;
92
93     ok( $self->{'callnumber'}, 'we have a call number to search for: ' . $self->{'callnumber'} );
94
95     my $items = C4::Items::GetItemsForInventory(
96         $self->{'callnumber'},    # minlocation
97         $self->{'callnumber'},    # maxlocation
98         undef,                    # location
99         undef,                    # itemtype
100         C4::Dates->new( $self->tomorrow(), 'iso' )->output,    # datelastseen
101     );
102
103     isa_ok( $items, 'ARRAY', 'We were able to call GetItemsForInventory with our call number' );
104     is( scalar @$items, 1, '...and we found only one item' );
105     my $our_item = $items->[0];
106     is( $our_item->{'itemnumber'}, $self->{'items'}[0]{'itemnumber'}, '...and the item we found has the right itemnumber' );
107
108     # give a datelastseen of yesterday, and we should not get our item.
109     $items = C4::Items::GetItemsForInventory(
110         $self->{'callnumber'},    # minlocation
111         $self->{'callnumber'},    # maxlocation
112         undef,                    # location
113         undef,                    # itemtype
114         C4::Dates->new( $self->yesterday(), 'iso' )->output,    # datelastseen
115     );
116
117     isa_ok( $items, 'ARRAY', 'We were able to call GetItemsForInventory with our call number' );
118     is( scalar @$items, 0, '...and we found no items' );
119
120 }
121
122
123 1;