Bug 27920: Add ability to update patron expiration dates when importing patrons
[koha.git] / t / db_dependent / Koha / Z3950Servers.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Test::More tests => 2;
22 use Test::Exception;
23
24 use t::lib::TestBuilder;
25 use Koha::Database;
26 use Koha::Z3950Server;
27 use Koha::Z3950Servers;
28
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'new, find and delete tests' => sub {
33     plan tests => 4;
34     $schema->storage->txn_begin;
35     my $nb_of_z39s = Koha::Z3950Servers->search->count;
36     my $new_z39_1 = Koha::Z3950Server->new({
37         host => 'my_host1.org',
38         port => '32',
39         db => 'db1',
40         servername => 'my_test_1',
41         servertype => 'zed',
42         recordtype => 'biblio',
43         syntax     => 'USMARC',
44         encoding   => 'MARC-8',
45     })->store;
46     my $new_z39_2 = Koha::Z3950Server->new({
47         host => 'my_host2.org',
48         port => '64',
49         db => 'db2',
50         servername => 'my_test_2',
51         servertype => 'zed',
52         recordtype => 'authority',
53         syntax     => 'USMARC',
54         encoding   => 'MARC-8',
55     })->store;
56
57     like( $new_z39_1->id, qr|^\d+$|, 'Adding a new z39 server should have set the id');
58     is( Koha::Z3950Servers->search->count, $nb_of_z39s + 2, 'The 2 z39 servers should have been added' );
59
60     my $retrieved_z39_1 = Koha::Z3950Servers->find( $new_z39_1->id );
61     is( $retrieved_z39_1->servername, $new_z39_1->servername, 'Find a z39 server by id should return the correct z39 server' );
62
63     $retrieved_z39_1->delete;
64     is( Koha::Z3950Servers->search->count, $nb_of_z39s + 1, 'Delete should have deleted the z39 server' );
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'Host, syntax and encoding are NOT NULL now (BZ 30571)' => sub {
70     plan tests => 7;
71     $schema->storage->txn_begin;
72     local $SIG{__WARN__} = sub {}; # TODO Needed it for suppressing DBIx warns
73
74     my $server = Koha::Z3950Server->new({
75         port => '80',
76         db => 'db',
77         servername => 'my_test_3',
78         servertype => 'zed',
79         recordtype => 'biblio',
80     });
81
82     throws_ok { $server->store } 'DBIx::Class::Exception', 'Exception on empty host';
83     like( $@->{msg}, qr/'host' doesn't have a default value/, 'Verified that DBIx blamed host' );
84
85     $server->host('host_added.nl');
86     throws_ok { $server->store } 'DBIx::Class::Exception', 'Exception on empty syntax';
87     like( $@->{msg}, qr/'syntax' doesn't have a default value/, 'Verified that DBIx blamed syntax' );
88
89     $server->syntax('USMARC');
90     throws_ok { $server->store } 'DBIx::Class::Exception', 'Exception on empty encoding';
91     like( $@->{msg}, qr/'encoding' doesn't have a default value/, 'Verified that DBIx blamed encoding' );
92
93     $server->encoding('utf8');
94     lives_ok { $server->store } 'No exceptions anymore';
95
96     $schema->storage->txn_rollback;
97 };