3 # This file is part of Koha.
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.
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.
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>.
20 use File::Basename qw/basename/;
22 use Koha::IllbatchStatus;
23 use Koha::IllbatchStatuses;
27 use t::lib::TestBuilder;
31 use Test::More tests => 13;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::IllbatchStatus');
36 use_ok('Koha::IllbatchStatuses');
38 $schema->storage->txn_begin;
40 Koha::IllbatchStatuses->search->delete;
42 # Keep track of whether our CRUD logging side-effects are happening
44 batch_status_create => 0,
45 batch_status_update => 0,
46 batch_status_delete => 0
49 # Mock a logger so we can check it is called
50 my $logger = Test::MockModule->new('Koha::Illrequest::Logger');
54 my ( $self, $to_log ) = @_;
55 $effects->{ $to_log->{actionname} }++;
59 # Create a batch status
60 my $status = $builder->build(
62 source => 'IllbatchStatus',
64 name => "Feeling the call to the Dark Side",
71 my $status_obj = Koha::IllbatchStatuses->find( { code => $status->{code} } );
72 isa_ok( $status_obj, 'Koha::IllbatchStatus' );
74 # Try to delete the status, it's a system status, so this should fail
75 $status_obj->delete_and_log;
76 my $status_obj_del = Koha::IllbatchStatuses->find( { code => $status->{code} } );
77 isa_ok( $status_obj_del, 'Koha::IllbatchStatus' );
81 # Try creating a duplicate status
82 my $status2 = Koha::IllbatchStatus->new(
85 code => $status->{code},
90 $status2->create_and_log,
91 { error => "Duplicate status found" },
92 "Creation of statuses with duplicate codes prevented"
95 # Create a non-duplicate status and ensure that the logger is called
96 my $status3 = Koha::IllbatchStatus->new(
103 $status3->create_and_log;
105 $effects->{'batch_status_create'},
107 "Creation of status calls log_something"
110 # Try creating a system status and ensure it's not created
111 my $cannot_create_system = Koha::IllbatchStatus->new(
113 name => "Jar Jar Binks",
118 $cannot_create_system->create_and_log;
119 my $created_but_not_system = Koha::IllbatchStatuses->find( { code => "GUNGAN" } );
120 is( $created_but_not_system->{is_system}, undef, "is_system statuses cannot be created" );
124 # Ensure only name can be updated
125 $status3->update_and_log(
128 code => "LIGHT_SIDE",
133 # Get our updated status, if we can get it by it's code, we know that hasn't changed
134 my $not_updated = Koha::IllbatchStatuses->find( { code => "DARK_SIDE" } )->unblessed;
135 is( $not_updated->{is_system}, 0, "is_system cannot be changed" );
136 is( $not_updated->{name}, "Rey", "name can be changed" );
138 # Ensure the logger is called
140 $effects->{'batch_status_update'},
142 "Update of status calls log_something"
146 my $cannot_delete = Koha::IllbatchStatus->new(
148 name => "Palapatine",
153 my $can_delete = Koha::IllbatchStatus->new(
160 $cannot_delete->delete_and_log;
161 my $not_deleted = Koha::IllbatchStatuses->find( { code => "SITH" } );
162 isa_ok( $not_deleted, 'Koha::IllbatchStatus', "is_system statuses cannot be deleted" );
163 $can_delete->create_and_log;
164 $can_delete->delete_and_log;
166 # Ensure the logger is called following a successful delete
168 $effects->{'batch_status_delete'},
170 "Delete of status calls log_something"
173 # Create a system "UNKNOWN" status
174 my $status_unknown = Koha::IllbatchStatus->new(
181 $status_unknown->create_and_log;
183 # Create a batch and assign it a status
184 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
185 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
186 my $status5 = Koha::IllbatchStatus->new(
193 $status5->create_and_log;
194 my $batch = Koha::Illbatch->new(
196 name => "My test batch",
197 patron_id => $patron->borrowernumber,
198 library_id => $library->branchcode,
200 status_code => $status5->code
203 $batch->create_and_log;
205 # Delete the batch status and ensure the batch's status has been changed
207 $status5->delete_and_log;
208 my $updated_code = Koha::Illbatches->find( { status_code => "UNKNOWN" } );
209 is( $updated_code->status_code, "UNKNOWN", "batches attached to deleted status have status changed to UNKNOWN" );
211 $schema->storage->txn_rollback;