Bug 32030: Add users to licenses - Preparation
[koha.git] / t / db_dependent / Koha / ERM / Agreements.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21
22 use Koha::ERM::Agreements;
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25
26 use t::lib::TestBuilder;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'periods' => sub {
32
33     plan tests => 4;
34
35     $schema->storage->txn_begin;
36
37     my $agreement =
38       $builder->build_object( { class => 'Koha::ERM::Agreements' } );
39     is( $agreement->periods->count, 0, "no period yet" );
40
41     my $today   = dt_from_string;
42     my $periods = [
43         {
44             started_on            => $today->ymd,
45             ended_on              => $today->clone->add( days => 1 )->ymd,
46             cancellation_deadline => $today->clone->add( years => 1 )->ymd,
47             notes                 => 'just some notes'
48         },
49         {
50             started_on            => $today->ymd,
51             ended_on              => undef,
52             cancellation_deadline => undef,
53             notes                 => undef,
54         },
55
56     ];
57     $agreement->periods($periods);
58
59     my $retrieved_periods = $agreement->periods;
60     is( ref($retrieved_periods), 'Koha::ERM::Agreement::Periods' );
61     $retrieved_periods =
62       [ map { delete $_->{agreement_id}; delete $_->{agreement_period_id}; $_ }
63           @{ $retrieved_periods->unblessed } ];
64     is_deeply( $retrieved_periods, $periods );
65     $agreement->periods( [] );
66     is( $agreement->periods->count, 0 );
67
68     $schema->storage->txn_rollback;
69 };
70
71 subtest 'user_role' => sub {
72
73     plan tests => 4;
74
75     $schema->storage->txn_begin;
76
77     my $agreement =
78       $builder->build_object( { class => 'Koha::ERM::Agreements' } );
79     is( $agreement->user_roles->count, 0, "no user yet" );
80
81     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
82
83     my $role = 'TEST_ROLE';
84     $builder->build_object(
85         {
86             class => 'Koha::AuthorisedValues',
87             value => {
88                 category         => 'ERM_USER_ROLES',
89                 authorised_value => 'TEST_ROLE',
90                 lib              => 'a role for testing'
91             }
92         }
93     );
94
95     my $user_roles = [
96         {
97             user_id => $patron->borrowernumber,
98             license_id => undef,
99             role    => $role
100         }
101     ];
102     $agreement->user_roles($user_roles);
103
104     my $retrieved_user_roles = $agreement->user_roles;
105     is( ref($retrieved_user_roles), 'Koha::ERM::UserRoles' );
106     $retrieved_user_roles =
107       [ map { delete $_->{agreement_id}; $_ }
108           @{ $retrieved_user_roles->unblessed } ];
109     is_deeply( $retrieved_user_roles, $user_roles );
110     $agreement->user_roles( [] );
111     is( $agreement->user_roles->count, 0 );
112
113     $schema->storage->txn_rollback;
114 };