Test: acts_as_list.t: Anpassung an Währungsumstellung
[kivitendo-erp.git] / t / db_helper / acts_as_list.t
1 use Test::More;
2 use Test::Exception;
3
4 use strict;
5
6 use lib 't';
7 use utf8;
8
9 use Data::Dumper;
10 use Support::TestSetup;
11
12 eval {
13   require SL::DB::RequirementSpec;
14   require SL::DB::RequirementSpecItem;
15   require SL::DB::RequirementSpecTextBlock;
16   require SL::DB::RequirementSpecType;
17   require SL::DB::RequirementSpecStatus;
18   1;
19 } or my $skip = 'RequirementSpec is not available for this test';
20
21 if ($skip) {
22   plan skip_all => $skip;
23 } else {
24   plan tests => 48;
25 }
26
27 my ($customer, $status, $type, $r_spec, @items);
28
29 sub init {
30   $customer = SL::DB::Customer->new(name => 'Test Customer', currency_id => $::instance_conf->get_currency_id)->save;
31   $status   = SL::DB::Manager::RequirementSpecStatus->find_by(name => '', description => '') ||
32               SL::DB::RequirementSpecStatus->new(name => '', description => '', position => 0)->save;
33   $type     = SL::DB::Manager::RequirementSpecType->find_by(description => '') ||
34               SL::DB::RequirementSpecType->new(description => '', position => 0)->save;
35 }
36
37 sub cleanup {
38   $customer->delete;
39   $status->delete;
40   $type->delete;
41 }
42
43 sub cleanup_req_spec {
44   SL::DB::Manager::RequirementSpec->delete_all(where => [ customer_id => $customer->id ], cascade => 1);
45 }
46
47 sub reset_state {
48   cleanup_req_spec();
49   @items = ();
50
51   $r_spec = SL::DB::RequirementSpec->new(type_id => $type->id, status_id => $status->id, customer_id => $customer->id, hourly_rate => 42.24,  title => "Azumbo")->save;
52
53   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => undef,     position => 1, fb_number => "A01",    title => "Mühköh",   item_type => 'section',            description => "The Kuh.")->save;
54   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => undef,     position => 2, fb_number => "A02",    title => "Geheim",   item_type => 'section',            description => "Kofferkombination")->save;
55   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 1, fb_number => "FB0001", title => "Yäääh",    item_type => 'function-block',     description => "Und so")->save;
56   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 2, fb_number => "FB0012", title => "Blubb",    item_type => 'function-block',     description => "blabb")->save;
57   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 3, fb_number => "FB0022", title => "Fingo",    item_type => 'function-block',     description => "fungo")->save;
58   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(4), position => 1, fb_number => "UFB002", title => "Suppi",    item_type => 'sub-function-block', description => "Suppa")->save;
59   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(4), position => 2, fb_number => "UFB000", title => "Suppa",    item_type => 'sub-function-block', description => "Suppi")->save;
60   push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(2), position => 1, fb_number => "FB0018", title => "Neuneins", item_type => 'function-block',     description => "Eins")->save;
61
62 #  SL::DB::RequirementSpec->new->db->dbh->do(qq|SELECT pg_catalog.setval('| . $_->[0] . qq|', | . $_->[1] . qq|, true)|) for (['requirement_spec_items_id_seq', 8], [ 'requirement_specs_id_seq', 2 ]);
63 }
64
65 sub values_eq {
66   my ($val1, $val2) = @_;
67   return (defined($val1) == defined($val2))
68       && (!defined($val1) || ($val1 == $val2));
69 }
70
71 # accepts a list of arefs, with positions:
72 # 1 - pseudo id of item
73 # 2 - expceted pseudo parent_id
74 # 3 - expected position
75 sub test_positions {
76   my ($message, @positions) = @_;
77
78   my $failures =
79     join ' ',
80     map  { join ':', map { $_ // 'undef' } @{ $_ } }
81     grep { !values_eq($_->[1] && get_item($_->[1]) ? get_id($_->[1]) : undef, $_->[3]) || !values_eq($_->[2], $_->[4]) }
82     map  { my $item = SL::DB::RequirementSpecItem->new(id => get_id($_->[0]))->load; [ @{ $_ }, $item->parent_id, $item->position ] }
83     @positions;
84
85   is($failures, '', $message);
86 }
87
88 sub new_item {
89   my %params = @_;
90
91   my $new_item = SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, fb_number => 'dummy', title => 'dummy', item_type => $params{parent_id} ? 'function-block' : 'section', @_);
92   push @items, $new_item;
93   $new_item;
94 }
95
96 # the first version of this used hardcoded ids.
97 # to make things a little more portable the objects are now created with the
98 # default id sequences, but retain their numbering. whenever you see get_id or
99 # get_item used it looks up the old id in the @items array
100 sub get_item {
101   return SL::DB::RequirementSpecItem->new(id => get_id($_[0]))->load;
102 }
103
104 sub get_id {
105   $items[$_[0]-1]->id
106 }
107
108 Support::TestSetup::login();
109
110 my $item;
111
112 # 1
113 # `+- 3
114 #  +- 4
115 #  |  `+- 6
116 #  |   `- 7
117 #  `- 5
118 # 2
119 # `- 8
120
121 init();
122 reset_state();
123 test_positions "reset_state", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
124
125 # Einfügen neuer Objekte: "set_position"
126 new_item(parent_id => get_id(1))->save;
127 test_positions "set_position via new with parent_id NOT NULL", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ], [ 9, 1, 4 ];
128
129 reset_state();
130 new_item()->save;
131 test_positions "set_position via new with parent_id IS NULL",  [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ], [ 9, undef, 3 ];
132
133 # Löschen von Objekten: "remove_position"
134 reset_state();
135 get_item(3)->delete;
136 test_positions "remove_position via delete with parent_id NOT NULL",  [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
137
138 reset_state();
139 get_item(1)->delete;
140 test_positions "remove_position via delete with parent_id IS NULL 1",  [ 2, undef, 1 ], [ 8, 2, 1 ];
141
142 reset_state();
143 get_item(2)->delete;
144 test_positions "remove_position via delete with parent_id IS NULL 2",  [ 1, undef, 1 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ];
145
146 # Hoch schieben
147 reset_state();
148 get_item(3)->move_position_up;
149 test_positions "move_position_up when at top of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
150
151 reset_state();
152 get_item(4)->move_position_up;
153 test_positions "move_position_up when in middle of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 3, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
154
155 reset_state();
156 get_item(5)->move_position_up;
157 test_positions "move_position_up when at end of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 5, 1, 2 ], [ 4, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
158
159 reset_state();
160 get_item(8)->move_position_up;
161 test_positions "move_position_up when only element in sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
162
163 # Herunter schieben
164 reset_state();
165 get_item(3)->move_position_down;
166 test_positions "move_position_down when at top of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 3, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
167
168 reset_state();
169 get_item(4)->move_position_down;
170 test_positions "move_position_down when in middle of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 5, 1, 2 ], [ 4, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
171
172 reset_state();
173 get_item(5)->move_position_down;
174 test_positions "move_position_down when at bottom of sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
175
176 reset_state();
177 get_item(8)->move_position_down;
178 test_positions "move_position_down when only element in sub-list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
179
180 # Listen neu anordnen
181 reset_state();
182 get_item(8)->reorder_list(map { get_id($_) } 4, 5, 3);
183 test_positions "reorder_list called as instance method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
184
185 reset_state();
186 SL::DB::RequirementSpecItem->reorder_list(map { get_id($_) } 4, 5, 3);
187 test_positions "reorder_list called as class method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
188
189 # Aus Liste entfernen
190 reset_state();
191 get_item(3)->remove_from_list;
192 test_positions "remove_from_list on top", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, -1 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
193
194 reset_state();
195 get_item(4)->remove_from_list;
196 test_positions "remove_from_list on middle", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, -1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
197
198 reset_state();
199 get_item(5)->remove_from_list;
200 test_positions "remove_from_list on bottom", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, -1 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
201
202 reset_state();
203 get_item(8)->remove_from_list;
204 test_positions "remove_from_list on only item in list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, -1 ];
205
206 reset_state();
207 $item = get_item(3); $item->remove_from_list; $item->delete;
208 test_positions "remove_from_list and delete afterwards", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
209
210 # Zu Liste hinzufügen
211 reset_state();
212 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'last');
213 test_positions "add_to_list position 'last'", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ];
214
215 reset_state();
216 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'first');
217 test_positions "add_to_list position 'first'", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ];
218
219 reset_state();
220 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_id(3));
221 test_positions "add_to_list position 'before' first by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ];
222
223 reset_state();
224 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_item(3));
225 test_positions "add_to_list position 'before' first by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ];
226
227 reset_state();
228 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_id(4));
229 test_positions "add_to_list position 'before' middle by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 2 ];
230
231 reset_state();
232 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_item(4));
233 test_positions "add_to_list position 'before' middle by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 2 ];
234
235 reset_state();
236 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_id(5));
237 test_positions "add_to_list position 'after' last by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ];
238
239 reset_state();
240 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_item(5));
241 test_positions "add_to_list position 'after' last by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ];
242
243 reset_state();
244 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_id(4));
245 test_positions "add_to_list position 'after' middle by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 3 ];
246
247 reset_state();
248 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_item(4));
249 test_positions "add_to_list position 'after' middle by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 3 ];
250
251 reset_state();
252 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(3)); $item->add_to_list(position => 'last');
253 test_positions "add_to_list position 'last' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ];
254
255 reset_state();
256 $item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(3)); $item->add_to_list(position => 'first');
257 test_positions "add_to_list position 'first' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ];
258
259 reset_state();
260 $item = get_item(5); $item->add_to_list(position => 'after', reference => get_id(3));
261 test_positions "add_to_list without prior remove_from_list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 5, 1, 2 ], [ 4, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
262
263 reset_state();
264 $item = get_item(4);
265 is($item->get_next_in_list->id,                       get_id(5), 'Next of 4 is 5');
266 is($item->get_previous_in_list->id,                   get_id(3), 'Previous of 4 is 5');
267 is($item->get_next_in_list->get_previous_in_list->id, get_id(4), 'Previous of Next of 4 is 4');
268 is($item->get_previous_in_list->get_next_in_list->id, get_id(4), 'Next of Previous of 4 is 4');
269 is($item->get_next_in_list->get_next_in_list,         undef,     'Next of Next of 4 is undef');
270 is($item->get_previous_in_list->get_previous_in_list, undef,     'Previous of Previous of 4 is undef');
271
272 # Parametervalidierung
273 throws_ok { new_item()->move_position_up   } qr/not.*been.*saved/i, 'move up not saved yet';
274 throws_ok { new_item()->move_position_down } qr/not.*been.*saved/i, 'move down not saved yet';
275
276 throws_ok { $item = get_item(8); $item->position(undef); $item->move_position_up   } qr/no.*position.*set.*yet/i, 'move up no position set';
277 throws_ok { $item = get_item(8); $item->position(undef); $item->move_position_down } qr/no.*position.*set.*yet/i, 'move down no position set';
278
279 throws_ok { get_item(8)->add_to_list;                      } qr/invalid.*parameter.*position/i,  'missing position';
280 throws_ok { get_item(8)->add_to_list(position => 'gonzo')  } qr/invalid.*parameter.*position/i,  'invalid position';
281 throws_ok { get_item(8)->add_to_list(position => 'before') } qr/missing.*parameter.*reference/i, 'missing reference for position "before"';
282 throws_ok { get_item(8)->add_to_list(position => 'after')  } qr/missing.*parameter.*reference/i, 'missing reference for position "after"';
283
284 END {
285   # safely try to clean up
286   eval {
287     cleanup_req_spec();
288     cleanup();
289   }
290 }