Bug 22812: Use Koha::Subscription in NewSubscription
[koha.git] / t / db_dependent / StockRotationRotas.t
1 #!/usr/bin/perl
2
3 # Copyright PTFS Europe 2016
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Koha::Database;
23 use t::lib::TestBuilder;
24
25 use Test::More tests => 5;
26
27 my $schema = Koha::Database->new->schema;
28
29 use_ok('Koha::StockRotationRotas');
30 use_ok('Koha::StockRotationRota');
31
32 subtest 'Basic object tests' => sub {
33
34     plan tests => 5;
35
36     $schema->storage->txn_begin;
37
38     my $builder = t::lib::TestBuilder->new;
39
40     my $rota = $builder->build({ source => 'Stockrotationrota' });
41
42     my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
43     isa_ok(
44         $srrota,
45         'Koha::StockRotationRota',
46         "Correctly create and load a stock rotation rota."
47     );
48
49     $builder->build({
50         source => 'Stockrotationstage',
51         value  => { rota_id => $rota->{rota_id} },
52     });
53     $builder->build({
54         source => 'Stockrotationstage',
55         value  => { rota_id => $rota->{rota_id} },
56     });
57     $builder->build({
58         source => 'Stockrotationstage',
59         value  => { rota_id => $rota->{rota_id} },
60     });
61
62     my $srstages = $srrota->stockrotationstages;
63     is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this rota');
64
65     isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
66
67     #### Test add_item
68
69     my $item = $builder->build({ source => 'Item' });
70
71     $srrota->add_item($item->{itemnumber});
72
73     is(
74         Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
75         $srrota->first_stage->stage_id,
76         "Adding an item results in a new sritem item being assigned to the first stage."
77     );
78
79     my $newrota = $builder->build({ source => 'Stockrotationrota' });
80
81     my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
82
83     $builder->build({
84         source => 'Stockrotationstage',
85         value  => { rota_id => $newrota->{rota_id} },
86     });
87
88     $srnewrota->add_item($item->{itemnumber});
89
90     is(
91         Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
92         $srnewrota->stockrotationstages->next->stage_id,
93         "Moving an item results in that sritem being assigned to the new first stage."
94     );
95
96     $schema->storage->txn_rollback;
97 };
98
99 subtest '->first_stage test' => sub {
100     plan tests => 2;
101
102     $schema->storage->txn_begin;
103
104     my $builder = t::lib::TestBuilder->new;
105
106     my $rota = $builder->build({ source => 'Stockrotationrota' });
107
108     my $stage1 = $builder->build({
109         source => 'Stockrotationstage',
110         value  => { rota_id => $rota->{rota_id} },
111     });
112     my $stage2 = $builder->build({
113         source => 'Stockrotationstage',
114         value  => { rota_id => $rota->{rota_id} },
115     });
116     my $stage3 = $builder->build({
117         source => 'Stockrotationstage',
118         value  => { rota_id => $rota->{rota_id} },
119     });
120
121     my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
122     my $srstage2 = Koha::StockRotationStages->find($stage2->{stage_id});
123     my $firststage = $srstage2->first_sibling || $srstage2;
124
125     is( $srrota->first_stage->stage_id, $firststage->stage_id, "First stage works" );
126
127     $srstage2->move_first;
128
129     is( Koha::StockRotationRotas->find($rota->{rota_id})->first_stage->stage_id, $stage2->{stage_id}, "Stage re-organized" );
130
131     $schema->storage->txn_rollback;
132 };
133
134 subtest '->items test' => sub {
135     plan tests => 1;
136
137     $schema->storage->txn_begin;
138
139     my $builder = t::lib::TestBuilder->new;
140
141     my $rota = $builder->build({ source => 'Stockrotationrota' });
142
143     my $stage1 = $builder->build({
144         source => 'Stockrotationstage',
145         value  => { rota_id => $rota->{rota_id} },
146     });
147     my $stage2 = $builder->build({
148         source => 'Stockrotationstage',
149         value  => { rota_id => $rota->{rota_id} },
150     });
151     my $stage3 = $builder->build({
152         source => 'Stockrotationstage',
153         value  => { rota_id => $rota->{rota_id} },
154     });
155
156     map { $builder->build({
157         source => 'Stockrotationitem',
158         value => { stage_id => $_ },
159     }) } (
160         $stage1->{stage_id}, $stage1->{stage_id},
161         $stage2->{stage_id}, $stage2->{stage_id},
162         $stage3->{stage_id}, $stage3->{stage_id},
163     );
164
165     my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
166
167     is(
168         $srrota->stockrotationitems->count,
169         6, "Correct number of items"
170     );
171
172     $schema->storage->txn_rollback;
173 };
174
175 1;