3 # Copyright PTFS Europe 2016
5 # This file is part of Koha.
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
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.
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.
23 use t
::lib
::TestBuilder
;
25 use Test
::More tests
=> 5;
27 my $schema = Koha
::Database
->new->schema;
29 use_ok
('Koha::StockRotationRotas');
30 use_ok
('Koha::StockRotationRota');
32 subtest
'Basic object tests' => sub {
36 $schema->storage->txn_begin;
38 my $builder = t
::lib
::TestBuilder
->new;
40 my $rota = $builder->build({ source
=> 'Stockrotationrota' });
42 my $srrota = Koha
::StockRotationRotas
->find($rota->{rota_id
});
45 'Koha::StockRotationRota',
46 "Correctly create and load a stock rotation rota."
50 source
=> 'Stockrotationstage',
51 value
=> { rota_id
=> $rota->{rota_id
} },
54 source
=> 'Stockrotationstage',
55 value
=> { rota_id
=> $rota->{rota_id
} },
58 source
=> 'Stockrotationstage',
59 value
=> { rota_id
=> $rota->{rota_id
} },
62 my $srstages = $srrota->stockrotationstages;
63 is
( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this rota');
65 isa_ok
( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
69 my $item = $builder->build({ source
=> 'Item' });
71 $srrota->add_item($item->{itemnumber
});
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."
79 my $newrota = $builder->build({ source
=> 'Stockrotationrota' });
81 my $srnewrota = Koha
::StockRotationRotas
->find($newrota->{rota_id
});
84 source
=> 'Stockrotationstage',
85 value
=> { rota_id
=> $newrota->{rota_id
} },
88 $srnewrota->add_item($item->{itemnumber
});
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."
96 $schema->storage->txn_rollback;
99 subtest
'->first_stage test' => sub {
102 $schema->storage->txn_begin;
104 my $builder = t
::lib
::TestBuilder
->new;
106 my $rota = $builder->build({ source
=> 'Stockrotationrota' });
108 my $stage1 = $builder->build({
109 source
=> 'Stockrotationstage',
110 value
=> { rota_id
=> $rota->{rota_id
} },
112 my $stage2 = $builder->build({
113 source
=> 'Stockrotationstage',
114 value
=> { rota_id
=> $rota->{rota_id
} },
116 my $stage3 = $builder->build({
117 source
=> 'Stockrotationstage',
118 value
=> { rota_id
=> $rota->{rota_id
} },
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;
125 is
( $srrota->first_stage->stage_id, $firststage->stage_id, "First stage works" );
127 $srstage2->move_first;
129 is
( Koha
::StockRotationRotas
->find($rota->{rota_id
})->first_stage->stage_id, $stage2->{stage_id
}, "Stage re-organized" );
131 $schema->storage->txn_rollback;
134 subtest
'->items test' => sub {
137 $schema->storage->txn_begin;
139 my $builder = t
::lib
::TestBuilder
->new;
141 my $rota = $builder->build({ source
=> 'Stockrotationrota' });
143 my $stage1 = $builder->build({
144 source
=> 'Stockrotationstage',
145 value
=> { rota_id
=> $rota->{rota_id
} },
147 my $stage2 = $builder->build({
148 source
=> 'Stockrotationstage',
149 value
=> { rota_id
=> $rota->{rota_id
} },
151 my $stage3 = $builder->build({
152 source
=> 'Stockrotationstage',
153 value
=> { rota_id
=> $rota->{rota_id
} },
156 map { $builder->build({
157 source
=> 'Stockrotationitem',
158 value
=> { stage_id
=> $_ },
160 $stage1->{stage_id
}, $stage1->{stage_id
},
161 $stage2->{stage_id
}, $stage2->{stage_id
},
162 $stage3->{stage_id
}, $stage3->{stage_id
},
165 my $srrota = Koha
::StockRotationRotas
->find($rota->{rota_id
});
168 $srrota->stockrotationitems->count,
169 6, "Correct number of items"
172 $schema->storage->txn_rollback;