Bug 11826: Use XSLT handler object in showmarc, Record.pm
[koha.git] / t / ItemType.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Test::More tests => 26;
7 use Test::MockModule;
8
9 BEGIN {
10     use_ok('C4::ItemType');
11 }
12
13 my $module = new Test::MockModule('C4::Context');
14 $module->mock(
15     '_new_dbh',
16     sub {
17         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
18           || die "Cannot create handle: $DBI::errstr\n";
19         return $dbh;
20     }
21 );
22
23 # Mock data
24 my $itemtypes = [
25     [
26         'itemtype', 'description', 'rentalcharge', 'notforloan',
27         'imageurl', 'summary', 'checkinmsg'
28     ],
29     [ 'BK', 'Books', 0, 0, '', '', 'foo' ],
30     [ 'CD', 'CDRom', 0, 0, '', '', 'bar' ]
31 ];
32
33 my $itemtypes_empty = [
34     [
35         'itemtype', 'description', 'rentalcharge', 'notforloan',
36         'imageurl', 'summary', 'checkinmsg'
37     ],
38 ];
39
40 my $dbh = C4::Context->dbh();
41 $dbh->{mock_add_resultset} = $itemtypes_empty;
42
43 my @itemtypes = C4::ItemType->all();
44 is( @itemtypes, 0, 'Testing all itemtypes is empty' );
45
46 # This should run exactly one query so we can test
47 my $history = $dbh->{mock_all_history};
48 is( scalar( @{$history} ), 1, 'Correct number of statements executed' );
49
50 # Now lets mock some data
51 $dbh->{mock_add_resultset} = $itemtypes;
52
53 @itemtypes = C4::ItemType->all();
54
55 $history = $dbh->{mock_all_history};
56 is( scalar( @{$history} ), 2, 'Correct number of statements executed' );
57
58 is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' );
59
60 is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' );
61
62 is( $itemtypes[0]->itemtype, 'BK', 'First itemtype is bk' );
63
64 is( $itemtypes[1]->itemtype, 'CD', 'second itemtype is cd' );
65
66 is( $itemtypes[0]->description, 'Books', 'First description is books' );
67
68 is( $itemtypes[1]->description, 'CDRom', 'second description is CDRom' );
69
70 is( $itemtypes[0]->rentalcharge, '0', 'first rental charge is 0' );
71
72 is( $itemtypes[1]->rentalcharge, '0', 'second rental charge is 0' );
73
74 is( $itemtypes[0]->notforloan, '0', 'first not for loan is 0' );
75
76 is( $itemtypes[1]->notforloan, '0', 'second not for loan is 0' );
77
78 is( $itemtypes[0]->imageurl, '', 'first imageurl is undef' );
79
80 is( $itemtypes[1]->imageurl, '', 'second imageurl is undef' );
81
82 is( $itemtypes[0]->checkinmsg, 'foo', 'first checkinmsg is foo' );
83
84 is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' );
85
86 # Mock the data again
87 $dbh->{mock_add_resultset} = $itemtypes;
88
89 # Test get(), which should return one itemtype
90 my $itemtype = C4::ItemType->get( 'BK' );
91
92 $history = $dbh->{mock_all_history};
93 is( scalar( @{$history} ), 3, 'Correct number of statements executed' );
94
95 is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' );
96
97 is( $itemtype->itemtype, 'BK', 'itemtype is bk' );
98
99 is( $itemtype->description, 'Books', 'description is books' );
100
101 is( $itemtype->rentalcharge, '0', 'rental charge is 0' );
102
103 is( $itemtype->notforloan, '0', 'not for loan is 0' );
104
105 is( $itemtype->imageurl, '', ' not for loan is undef' );
106
107 is( $itemtype->checkinmsg, 'foo', 'checkinmsg is foo' );