Bug 23321: Allow setting of branch default
[koha.git] / t / db_dependent / Koha / Cash / Register.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 => 2;
23
24 use Test::Exception;
25
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 my $builder = t::lib::TestBuilder->new;
31 my $schema  = Koha::Database->new->schema;
32
33 subtest 'library' => sub {
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
39     my $register = $builder->build_object(
40         {
41             class => 'Koha::Cash::Registers',
42             value => { branch => $library->branchcode },
43         }
44     );
45
46     is( ref( $register->library ),
47         'Koha::Library',
48         'Koha::Cash::Register->library should return a Koha::Library' );
49
50     is( $register->library->id,
51         $library->id,
52         'Koha::Cash::Register->library returns the correct Koha::Library' );
53
54     $schema->storage->txn_rollback;
55 };
56
57 subtest 'branch_default' => sub {
58     plan tests => 3;
59
60     $schema->storage->txn_begin;
61     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
62     my $register1 = $builder->build_object(
63         {
64             class => 'Koha::Cash::Registers',
65             value => { branch => $library->branchcode, branch_default => 1 },
66         }
67     );
68     my $register2 = $builder->build_object(
69         {
70             class => 'Koha::Cash::Registers',
71             value => { branch => $library->branchcode, branch_default => 0 },
72         }
73     );
74
75     subtest 'store' => sub {
76         plan tests => 2;
77
78         $register1->name('Test till 1');
79         ok( $register1->store(),
80             "Store works as expected when branch_default is not changed" );
81
82         $register1->branch_default(0);
83         throws_ok { $register1->store(); }
84         'Koha::Exceptions::Object::ReadOnlyProperty',
85           'Exception thrown if direct update to branch_default is attempted';
86
87     };
88
89     subtest 'make_default' => sub {
90         plan tests => 3;
91
92         ok($register2->make_default,'Koha::Register->make_default ran');
93
94         $register1 = $register1->get_from_storage;
95         $register2 = $register2->get_from_storage;
96         is($register1->branch_default, 0, 'register1 was unset as expected');
97         is($register2->branch_default, 1, 'register2 was set as expected');
98     };
99
100     subtest 'drop_default' => sub {
101         plan tests => 2;
102
103         ok($register2->drop_default,'Koha::Register->drop_default ran');
104
105         $register2 = $register2->get_from_storage;
106         is($register2->branch_default, 0, 'register2 was unset as expected');
107     };
108
109     $schema->storage->txn_rollback;
110 };