Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Ticket.t
1 #!/usr/bin/perl
2
3 # Copyright 2023 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
22 use Test::More tests => 6;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::Database;
27
28 my $builder = t::lib::TestBuilder->new;
29 my $schema  = Koha::Database->new->schema;
30
31 subtest 'reporter() tests' => sub {
32
33     plan tests => 2;
34
35     $schema->storage->txn_begin;
36
37     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
38     my $ticket = $builder->build_object(
39         {
40             class => 'Koha::Tickets',
41             value => {
42                 reporter_id => $patron->id
43             }
44         }
45     );
46
47     my $reporter = $ticket->reporter;
48     is( ref($reporter), 'Koha::Patron', 'Koha::Ticket->reporter returns a Koha::Patron object' );
49     is( $reporter->id, $patron->id, 'Koha::Ticket->reporter returns the right Koha::Patron' );
50
51     $schema->storage->txn_rollback;
52 };
53
54 subtest 'resolver() tests' => sub {
55
56     plan tests => 2;
57
58     $schema->storage->txn_begin;
59
60     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
61     my $ticket = $builder->build_object(
62         {
63             class => 'Koha::Tickets',
64             value => {
65                 resolver_id => $patron->id
66             }
67         }
68     );
69
70     my $resolver = $ticket->resolver;
71     is( ref($resolver), 'Koha::Patron', 'Koha::Ticket->resolver returns a Koha::Patron object' );
72     is( $resolver->id, $patron->id, 'Koha::Ticket->resolver returns the right Koha::Patron' );
73
74     $schema->storage->txn_rollback;
75 };
76
77 subtest 'biblio() tests' => sub {
78
79     plan tests => 2;
80
81     $schema->storage->txn_begin;
82
83     my $biblio  = $builder->build_object({ class => 'Koha::Biblios' });
84     my $ticket = $builder->build_object(
85         {
86             class => 'Koha::Tickets',
87             value => {
88                 biblio_id => $biblio->id
89             }
90         }
91     );
92
93     my $related_biblio = $ticket->biblio;
94     is( ref($related_biblio), 'Koha::Biblio', 'Koha::Ticket->biblio returns a Koha::Biblio object' );
95     is( $related_biblio->id, $biblio->id, 'Koha::Ticket->biblio returns the right Koha::Biblio' );
96
97     $schema->storage->txn_rollback;
98 };
99
100 subtest 'updates() tests' => sub {
101
102     plan tests => 4;
103
104     $schema->storage->txn_begin;
105
106     my $ticket = $builder->build_object( { class => 'Koha::Tickets' } );
107     my $updates = $ticket->updates;
108     is( ref($updates), 'Koha::Ticket::Updates', 'Koha::Ticket->updates should return a Koha::Ticket::Updates object' );
109     is( $updates->count, 0, 'Koha::Ticket->updates should return a count of 0 when there are no related updates' );
110
111     # Add two updates
112     foreach (1..2) {
113         $builder->build_object(
114             {
115                 class => 'Koha::Ticket::Updates',
116                 value => { ticket_id => $ticket->id }
117             }
118         );
119     }
120
121     $updates = $ticket->updates;
122     is( ref($updates), 'Koha::Ticket::Updates', 'Koha::Ticket->updates should return a Koha::Ticket::Updates object' );
123     is( $updates->count, 2, 'Koha::Ticket->updates should return the correct number of updates' );
124
125     $schema->storage->txn_rollback;
126 };
127
128 subtest 'add_update() tests' => sub {
129     plan tests => 2;
130
131     $schema->storage->txn_begin;
132
133     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
134
135     my $ticket = $builder->build_object( { class => 'Koha::Tickets' } );
136     my $update = $ticket->add_update(
137         { user_id => $patron->id, public => 1, message => "Some message" } );
138     is( ref($update), 'Koha::Ticket::Update',
139         'Koha::Ticket->add_update should return a Koha::Ticket::Update object'
140     );
141
142     my $updates = $ticket->updates;
143     is( $updates->count, 1,
144         'Koha::Ticket->add_update should have added 1 update linked to this ticket'
145     );
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'store() tests' => sub {
151     plan tests => 2;
152
153     subtest 'acknowledgement notice trigger' => sub {
154         plan tests => 4;
155
156         $schema->storage->txn_begin;
157
158         my $patron = $builder->build_object( { class => "Koha::Patrons" } );
159         my $biblio = $builder->build_sample_biblio();
160
161         my $new_ticket = Koha::Ticket->new(
162             {
163                 reporter_id => $patron->id,
164                 title       => "Testing ticket",
165                 body        => "Testing ticket message",
166                 biblio_id   => $biblio->id
167             }
168         )->store();
169
170         is( ref($new_ticket), 'Koha::Ticket',
171             'Koha::Ticket->store() returned the Koha::Ticket object' );
172         my $notices =
173           Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
174         is( $notices->count, 1,
175             'One acknowledgement notice queued for the ticket reporter' );
176         my $THE_notice = $notices->next;
177         isnt( $THE_notice->status, 'pending',
178             'Acknowledgement notice is sent immediately' );
179
180         $new_ticket->set( { title => "Changed title" } )->store();
181         $notices =
182           Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
183         is( $notices->count, 1,
184             'Further acknowledgement notices are not queud on subsequent stores'
185         );
186
187         $schema->storage->txn_rollback;
188     };
189
190     subtest 'cataloger notice trigger' => sub {
191         plan tests => 4;
192
193         $schema->storage->txn_begin;
194
195         my $catemail = 'catalogers@testmail.com';
196         t::lib::Mocks::mock_preference( 'CatalogerEmails', $catemail );
197
198         my $patron = $builder->build_object( { class => "Koha::Patrons" } );
199         my $biblio = $builder->build_sample_biblio();
200
201         my $new_ticket = Koha::Ticket->new(
202             {
203                 reporter_id => $patron->id,
204                 title       => "Testing ticket",
205                 body        => "Testing ticket message",
206                 biblio_id   => $biblio->id
207             }
208         )->store();
209
210         is( ref($new_ticket), 'Koha::Ticket',
211             'Koha::Ticket->store() returned the Koha::Ticket object' );
212         my $notices =
213           Koha::Notice::Messages->search( { to_address => $catemail } );
214         is( $notices->count, 1,
215             'One notification notice queued for the catalogers when ticket reported' );
216         my $THE_notice = $notices->next;
217         isnt( $THE_notice->status, 'pending',
218             'Notification notice is sent immediately' );
219
220         $new_ticket->set( { title => "Changed title" } )->store();
221         $notices =
222           Koha::Notice::Messages->search( { to_address => $catemail } );
223         is( $notices->count, 1,
224             'Further notification notices are not queud on subsequent stores'
225         );
226
227         $schema->storage->txn_rollback;
228     };
229 };