7794268e99d308ce1daea8319a426fdec75b4e98
[koha-ffzg.git] / t / lib / KohaTest / Koha / displayServers.pm
1 package KohaTest::Koha::displayServers;
2 use base qw( KohaTest::Koha );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Koha;
10
11 =head2 basic_usage
12
13 call displayServers with no parameters and investigate the things that
14 it returns. This depends on there being at least one server defined,
15 as do some other tests in this module.
16
17 =cut
18
19 sub basic_usage : Test( 12 ) {
20     my $self = shift;
21
22     my $servers = C4::Koha::displayServers();
23     isa_ok( $servers, 'ARRAY' );
24     my $firstserver = $servers->[0];
25     isa_ok( $firstserver, 'HASH' );
26
27     my @keys = qw( opensearch icon value name checked zed label id encoding );
28     is( scalar keys %$firstserver, scalar @keys, 'the hash has the right number of keys' );
29     foreach my $key ( @keys ) {
30         ok( exists $firstserver->{$key}, "There is a $key key" );
31     }
32
33     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
34 }
35
36 =head2 position_does_not_exist
37
38 call displayServers with a position that does not exist and make sure
39 that we get none back.
40
41 =cut
42
43 sub position_does_not_exist : Test( 2 ) {
44     my $self = shift;
45
46     my $servers = C4::Koha::displayServers( 'this does not exist' );
47     isa_ok( $servers, 'ARRAY' );
48     is( scalar @$servers, 0, 'received no servers' );
49
50     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
51 }
52
53 =head2 position_does_exist
54
55 call displayServers with a position that does exist and make sure that
56 we get at least one back.
57
58 =cut
59
60 sub position_does_exist : Test( 3 ) {
61     my $self = shift;
62
63     my $position = $self->_get_a_position();
64     ok( $position, 'We have a position that exists' );
65     
66     my $servers = C4::Koha::displayServers( $position );
67     isa_ok( $servers, 'ARRAY' );
68     ok( scalar @$servers, 'received at least one server' );
69
70     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
71 }
72
73 =head2 type_does_not_exist
74
75 call displayServers with a type that does not exist and make sure
76 that we get none back.
77
78 =cut
79
80 sub type_does_not_exist : Test( 2 ) {
81     my $self = shift;
82
83     my $servers = C4::Koha::displayServers( undef, 'this does not exist' );
84     isa_ok( $servers, 'ARRAY' );
85     is( scalar @$servers, 0, 'received no servers' );
86
87     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
88 }
89
90 =head2 type_does_exist
91
92 call displayServers with a type that does exist and make sure
93 that we get at least one back.
94
95 =cut
96
97 sub type_does_exist : Test( 3 ) {
98     my $self = shift;
99
100     my $type = $self->_get_a_type();
101     ok( $type, 'We have a type that exists' );
102     
103     my $servers = C4::Koha::displayServers( undef, $type );
104     isa_ok( $servers, 'ARRAY' );
105     ok( scalar @$servers, 'received at least one server' );
106
107     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
108 }
109
110 =head2 position_and_type
111
112 call displayServers with a variety of both positions and types and
113 verify that we get either something or nothing back.
114
115
116 =cut
117
118 sub position_and_type : Test( 8 ) {
119     my $self = shift;
120
121     my ( $position, $type ) = $self->_get_a_position_and_type();
122     ok( $position, 'We have a type that exists' );
123     ok( $type, 'We have a type that exists' );
124     
125     my $servers = C4::Koha::displayServers( $position, 'type does not exist' );
126     isa_ok( $servers, 'ARRAY' );
127     is( scalar @$servers, 0, 'received no servers' );
128
129     $servers = C4::Koha::displayServers( 'position does not exist', $type );
130     isa_ok( $servers, 'ARRAY' );
131     is( scalar @$servers, 0, 'received no servers' );
132
133     $servers = C4::Koha::displayServers( $position, $type );
134     isa_ok( $servers, 'ARRAY' );
135     ok( scalar @$servers, 'received at least one server' );
136
137     # diag( Data::Dumper->Dump( [ $servers ], [ 'servers' ] ) );
138 }
139
140 =head1 INTERNAL METHODS
141
142 these are not test methods, but they help me write them.
143
144 =head2 _get_a_position
145
146 returns a position value for which at least one server exists
147
148 =cut
149
150 sub _get_a_position {
151     my $self = shift;
152
153     my ( $position, $type ) = $self->_get_a_position_and_type();
154     return $position;
155
156 }
157
158 =head2 _get_a_type
159
160 returns a type value for which at least one server exists
161
162 =cut
163
164 sub _get_a_type {
165     my $self = shift;
166
167     my ( $position, $type ) = $self->_get_a_position_and_type();
168     return $type;
169
170 }
171
172 =head2 _get_a_position_and_type
173
174 returns a position and type for a server
175
176 =cut
177
178 sub _get_a_position_and_type {
179     my $self = shift;
180
181     my $dbh    = C4::Context->dbh;
182     my $sql = 'SELECT position, type FROM z3950servers';
183     my $sth = $dbh->prepare($sql) or return;
184     $sth->execute or return;
185
186     my @row = $sth->fetchrow_array;
187     return ( $row[0], $row[1] );
188
189 }
190
191   
192 1;