3 # Copyright 2023 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 6;
23 use t::lib::TestBuilder;
28 my $builder = t::lib::TestBuilder->new;
29 my $schema = Koha::Database->new->schema;
31 subtest 'reporter() tests' => sub {
35 $schema->storage->txn_begin;
37 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
38 my $ticket = $builder->build_object(
40 class => 'Koha::Tickets',
42 reporter_id => $patron->id
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' );
51 $schema->storage->txn_rollback;
54 subtest 'resolver() tests' => sub {
58 $schema->storage->txn_begin;
60 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
61 my $ticket = $builder->build_object(
63 class => 'Koha::Tickets',
65 resolver_id => $patron->id
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' );
74 $schema->storage->txn_rollback;
77 subtest 'biblio() tests' => sub {
81 $schema->storage->txn_begin;
83 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
84 my $ticket = $builder->build_object(
86 class => 'Koha::Tickets',
88 biblio_id => $biblio->id
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' );
97 $schema->storage->txn_rollback;
100 subtest 'updates() tests' => sub {
104 $schema->storage->txn_begin;
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' );
113 $builder->build_object(
115 class => 'Koha::Ticket::Updates',
116 value => { ticket_id => $ticket->id }
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' );
125 $schema->storage->txn_rollback;
128 subtest 'add_update() tests' => sub {
131 $schema->storage->txn_begin;
133 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
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'
142 my $updates = $ticket->updates;
143 is( $updates->count, 1,
144 'Koha::Ticket->add_update should have added 1 update linked to this ticket'
147 $schema->storage->txn_rollback;
150 subtest 'store() tests' => sub {
153 subtest 'acknowledgement notice trigger' => sub {
156 $schema->storage->txn_begin;
158 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
159 my $biblio = $builder->build_sample_biblio();
161 my $new_ticket = Koha::Ticket->new(
163 reporter_id => $patron->id,
164 title => "Testing ticket",
165 body => "Testing ticket message",
166 biblio_id => $biblio->id
170 is( ref($new_ticket), 'Koha::Ticket',
171 'Koha::Ticket->store() returned the Koha::Ticket object' );
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' );
180 $new_ticket->set( { title => "Changed title" } )->store();
182 Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
183 is( $notices->count, 1,
184 'Further acknowledgement notices are not queud on subsequent stores'
187 $schema->storage->txn_rollback;
190 subtest 'cataloger notice trigger' => sub {
193 $schema->storage->txn_begin;
195 my $catemail = 'catalogers@testmail.com';
196 t::lib::Mocks::mock_preference( 'CatalogerEmails', $catemail );
198 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
199 my $biblio = $builder->build_sample_biblio();
201 my $new_ticket = Koha::Ticket->new(
203 reporter_id => $patron->id,
204 title => "Testing ticket",
205 body => "Testing ticket message",
206 biblio_id => $biblio->id
210 is( ref($new_ticket), 'Koha::Ticket',
211 'Koha::Ticket->store() returned the Koha::Ticket object' );
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' );
220 $new_ticket->set( { title => "Changed title" } )->store();
222 Koha::Notice::Messages->search( { to_address => $catemail } );
223 is( $notices->count, 1,
224 'Further notification notices are not queud on subsequent stores'
227 $schema->storage->txn_rollback;