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