Bug 30719: (QA follow-up) Pick better column names and cleanup
[koha.git] / t / db_dependent / IllbatchStatuses.t
1 #s!/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 File::Basename qw/basename/;
21 use Koha::Database;
22 use Koha::IllbatchStatus;
23 use Koha::IllbatchStatuses;
24 use Koha::Patrons;
25 use Koha::Libraries;
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28 use Test::MockObject;
29 use Test::MockModule;
30
31 use Test::More tests => 13;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::IllbatchStatus');
36 use_ok('Koha::IllbatchStatuses');
37
38 $schema->storage->txn_begin;
39
40 Koha::IllbatchStatuses->search->delete;
41
42 # Keep track of whether our CRUD logging side-effects are happening
43 my $effects = {
44     batch_status_create => 0,
45     batch_status_update => 0,
46     batch_status_delete => 0
47 };
48
49 # Mock a logger so we can check it is called
50 my $logger = Test::MockModule->new('Koha::Illrequest::Logger');
51 $logger->mock(
52     'log_something',
53     sub {
54         my ( $self, $to_log ) = @_;
55         $effects->{ $to_log->{actionname} }++;
56     }
57 );
58
59 # Create a batch status
60 my $status = $builder->build(
61     {
62         source => 'IllbatchStatus',
63         value  => {
64             name      => "Feeling the call to the Dark Side",
65             code      => "OH_NO",
66             is_system => 1
67         }
68     }
69 );
70
71 my $status_obj = Koha::IllbatchStatuses->find( { code => $status->{code} } );
72 isa_ok( $status_obj, 'Koha::IllbatchStatus' );
73
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' );
78
79 ## Status create
80
81 # Try creating a duplicate status
82 my $status2 = Koha::IllbatchStatus->new(
83     {
84         name      => "Obi-wan",
85         code      => $status->{code},
86         is_system => 0
87     }
88 );
89 is_deeply(
90     $status2->create_and_log,
91     { error => "Duplicate status found" },
92     "Creation of statuses with duplicate codes prevented"
93 );
94
95 # Create a non-duplicate status and ensure that the logger is called
96 my $status3 = Koha::IllbatchStatus->new(
97     {
98         name      => "Kylo",
99         code      => "DARK_SIDE",
100         is_system => 0
101     }
102 );
103 $status3->create_and_log;
104 is(
105     $effects->{'batch_status_create'},
106     1,
107     "Creation of status calls log_something"
108 );
109
110 # Try creating a system status and ensure it's not created
111 my $cannot_create_system = Koha::IllbatchStatus->new(
112     {
113         name      => "Jar Jar Binks",
114         code      => "GUNGAN",
115         is_system => 1
116     }
117 );
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" );
121
122 ## Status update
123
124 # Ensure only name can be updated
125 $status3->update_and_log(
126     {
127         name      => "Rey",
128         code      => "LIGHT_SIDE",
129         is_system => 1
130     }
131 );
132
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" );
137
138 # Ensure the logger is called
139 is(
140     $effects->{'batch_status_update'},
141     1,
142     "Update of status calls log_something"
143 );
144
145 ## Status delete
146 my $cannot_delete = Koha::IllbatchStatus->new(
147     {
148         name      => "Palapatine",
149         code      => "SITH",
150         is_system => 1
151     }
152 )->store;
153 my $can_delete = Koha::IllbatchStatus->new(
154     {
155         name      => "Windu",
156         code      => "JEDI",
157         is_system => 0
158     }
159 );
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;
165
166 # Ensure the logger is called following a successful delete
167 is(
168     $effects->{'batch_status_delete'},
169     1,
170     "Delete of status calls log_something"
171 );
172
173 # Create a system "UNKNOWN" status
174 my $status_unknown = Koha::IllbatchStatus->new(
175     {
176         name      => "Unknown",
177         code      => "UNKNOWN",
178         is_system => 1
179     }
180 );
181 $status_unknown->create_and_log;
182
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(
187     {
188         name      => "Plagueis",
189         code      => "DEAD_SITH",
190         is_system => 0
191     }
192 );
193 $status5->create_and_log;
194 my $batch = Koha::Illbatch->new(
195     {
196         name       => "My test batch",
197         patron_id  => $patron->borrowernumber,
198         library_id => $library->branchcode,
199         backend    => "TEST",
200         statuscode => $status5->code
201     }
202 );
203 $batch->create_and_log;
204
205 # Delete the batch status and ensure the batch's status has been changed
206 # to UNKNOWN
207 $status5->delete_and_log;
208 my $updated_code = Koha::Illbatches->find( { statuscode => "UNKNOWN" } );
209 is( $updated_code->statuscode, "UNKNOWN", "batches attached to deleted status have status changed to UNKNOWN" );
210
211 $schema->storage->txn_rollback;