View | Details | Raw Unified | Return to bug 25113
Collapse All | Expand All

(-)a/t/db_dependent/Koha/CirculationRules.t (-143 / +81 lines)
Lines 33-39 my $schema = Koha::Database->new->schema; Link Here
33
my $builder = t::lib::TestBuilder->new;
33
my $builder = t::lib::TestBuilder->new;
34
34
35
subtest 'set_rule + get_effective_rule' => sub {
35
subtest 'set_rule + get_effective_rule' => sub {
36
    plan tests => 14;
36
    plan tests => 8;
37
37
38
    $schema->storage->txn_begin;
38
    $schema->storage->txn_begin;
39
39
Lines 91-117 subtest 'set_rule + get_effective_rule' => sub { Link Here
91
91
92
    is( $rule->rule_value, $default_rule_value, '* means default' );
92
    is( $rule->rule_value, $default_rule_value, '* means default' );
93
93
94
    Koha::CirculationRules->set_rule(
95
        {
96
            branchcode   => '*',
97
            categorycode => '*',
98
            itemtype     => $itemtype,
99
            rule_name    => $rule_name,
100
            rule_value   => 2,
101
        }
102
    );
103
104
    $rule = Koha::CirculationRules->get_effective_rule(
105
        {
106
            branchcode   => $branchcode,
107
            categorycode => $categorycode,
108
            itemtype     => $itemtype,
109
            rule_name    => $rule_name,
110
        }
111
    );
112
    is( $rule->rule_value, 2,
113
        'More specific rule is returned when itemtype is given' );
114
115
    $rule = Koha::CirculationRules->get_effective_rule(
94
    $rule = Koha::CirculationRules->get_effective_rule(
116
        {
95
        {
117
            branchcode   => $branchcode_2,
96
            branchcode   => $branchcode_2,
Lines 123-251 subtest 'set_rule + get_effective_rule' => sub { Link Here
123
    is( $rule->rule_value, 1,
102
    is( $rule->rule_value, 1,
124
        'Default rule is returned if there is no rule for this branchcode' );
103
        'Default rule is returned if there is no rule for this branchcode' );
125
104
126
    Koha::CirculationRules->set_rule(
105
    subtest 'test rule matching with different combinations of rule scopes' => sub {
127
        {
106
        my ( $tests, $order ) = prepare_tests_for_rule_scope_combinations(
128
            branchcode   => '*',
107
            {
129
            categorycode => $categorycode,
108
                branchcode   => $branchcode,
130
            itemtype     => '*',
109
                categorycode => $categorycode,
131
            rule_name    => $rule_name,
110
                itemtype     => $itemtype,
132
            rule_value   => 3,
111
            },
133
        }
112
            'maxissueqty'
134
    );
113
        );
135
114
136
    $rule = Koha::CirculationRules->get_effective_rule(
115
        plan tests => 2**scalar @$order;
137
        {
116
138
117
        foreach my $test (@$tests) {
139
            branchcode   => $branchcode,
118
            my $rule_params = {%$test};
140
            categorycode => $categorycode,
119
            $rule_params->{rule_name} = $rule_name;
141
            itemtype     => $itemtype,
120
            my $rule_value = $rule_params->{rule_value} = int( rand(10) );
142
            rule_name    => $rule_name,
121
143
        }
122
            Koha::CirculationRules->set_rule($rule_params);
144
    );
123
145
    is( $rule->rule_value, 3,
124
            my $rule = Koha::CirculationRules->get_effective_rule(
146
        'More specific rule is returned when categorycode exists' );
125
                {
147
126
                    branchcode   => $branchcode,
148
    Koha::CirculationRules->set_rule(
127
                    categorycode => $categorycode,
149
        {
128
                    itemtype     => $itemtype,
150
            branchcode   => '*',
129
                    rule_name    => $rule_name,
151
            categorycode => $categorycode,
130
                }
152
            itemtype     => $itemtype,
131
            );
153
            rule_name    => $rule_name,
132
154
            rule_value   => 4,
133
            my $scope_output = '';
155
        }
134
            foreach my $key ( values @$order ) {
156
    );
135
                $scope_output .= " $key" if $test->{$key} ne '*';
157
    $rule = Koha::CirculationRules->get_effective_rule(
136
            }
158
        {
159
            branchcode   => $branchcode,
160
            categorycode => $categorycode,
161
            itemtype     => $itemtype,
162
            rule_name    => $rule_name,
163
        }
164
    );
165
    is( $rule->rule_value, 4,
166
        'More specific rule is returned when categorycode and itemtype exist' );
167
168
    Koha::CirculationRules->set_rule(
169
        {
170
            branchcode   => $branchcode,
171
            categorycode => '*',
172
            itemtype     => '*',
173
            rule_name    => $rule_name,
174
            rule_value   => 5,
175
        }
176
    );
177
    $rule = Koha::CirculationRules->get_effective_rule(
178
        {
179
            branchcode   => $branchcode,
180
            categorycode => $categorycode,
181
            itemtype     => $itemtype,
182
            rule_name    => $rule_name,
183
        }
184
    );
185
    is( $rule->rule_value, 5,
186
        'More specific rule is returned when branchcode exists' );
187
188
    Koha::CirculationRules->set_rule(
189
        {
190
            branchcode   => $branchcode,
191
            categorycode => '*',
192
            itemtype     => $itemtype,
193
            rule_name    => $rule_name,
194
            rule_value   => 6,
195
        }
196
    );
197
    $rule = Koha::CirculationRules->get_effective_rule(
198
        {
199
            branchcode   => $branchcode,
200
            categorycode => $categorycode,
201
            itemtype     => $itemtype,
202
            rule_name    => $rule_name,
203
        }
204
    );
205
    is( $rule->rule_value, 6,
206
        'More specific rule is returned when branchcode and itemtype exists' );
207
208
    Koha::CirculationRules->set_rule(
209
        {
210
            branchcode   => $branchcode,
211
            categorycode => $categorycode,
212
            itemtype     => '*',
213
            rule_name    => $rule_name,
214
            rule_value   => 7,
215
        }
216
    );
217
    $rule = Koha::CirculationRules->get_effective_rule(
218
        {
219
            branchcode   => $branchcode,
220
            categorycode => $categorycode,
221
            itemtype     => $itemtype,
222
            rule_name    => $rule_name,
223
        }
224
    );
225
    is( $rule->rule_value, 7,
226
        'More specific rule is returned when branchcode and categorycode exist'
227
    );
228
137
229
    Koha::CirculationRules->set_rule(
138
            is( $rule->rule_value, $rule_value,
230
        {
139
                'Explicitly scoped'
231
            branchcode   => $branchcode,
140
                  . ( $scope_output ? $scope_output : ' nothing' ) );
232
            categorycode => $categorycode,
233
            itemtype     => $itemtype,
234
            rule_name    => $rule_name,
235
            rule_value   => 8,
236
        }
237
    );
238
    $rule = Koha::CirculationRules->get_effective_rule(
239
        {
240
            branchcode   => $branchcode,
241
            categorycode => $categorycode,
242
            itemtype     => $itemtype,
243
            rule_name    => $rule_name,
244
        }
141
        }
245
    );
142
    };
246
    is( $rule->rule_value, 8,
247
        'More specific rule is returned when branchcode, categorycode and itemtype exist'
248
    );
249
143
250
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
144
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
251
    is( $our_branch_rules->count, 4, "We added 8 rules");
145
    is( $our_branch_rules->count, 4, "We added 8 rules");
Lines 506-508 subtest 'get_lostreturn_policy() tests' => sub { Link Here
506
400
507
    $schema->storage->txn_rollback;
401
    $schema->storage->txn_rollback;
508
};
402
};
403
404
sub prepare_tests_for_rule_scope_combinations {
405
    my ( $scope, $rule_name ) = @_;
406
407
    # Here we create a combinations of 1s and 0s the following way
408
    #
409
    # 000...
410
    # 001...
411
    # 010...
412
    # 011...
413
    # 100...
414
    # 101...
415
    # 110...
416
    # 111...
417
    #
418
    # (the number of columns equals to the amount of rule scopes)
419
    # The ... symbolizes possible future scopes.
420
    #
421
    # - 0 equals to circulation rule scope with any value (aka. *)
422
    # - 1 equals to circulation rule scope exact value, e.g.
423
    #     "CPL" (for branchcode).
424
    #
425
    # The order is the same as the weight of scopes when sorting circulation
426
    # rules. So the first column of numbers is the scope with most weight.
427
    # This is defined by C<$order> which will be assigned next.
428
    #
429
    # We must maintain the order in order to keep the test valid. This should be
430
    # equal to Koha/CirculationRules.pm "order_by" of C<get_effective_rule> sub.
431
    # Let's explicitly define the order and fail test if we are missing a scope:
432
    my $order = [ 'branchcode', 'categorycode', 'itemtype' ];
433
    is( join(", ", sort keys %$scope),
434
       join(", ", sort @$order), 'Missing a scope!' ) if keys %$scope ne scalar @$order;
435
436
    my @tests = ();
437
    foreach my $value ( glob( "{0,1}" x keys %$scope || 1 ) ) {
438
        my $test = { %$scope };
439
        for ( my $i=0; $i < keys %$scope; $i++ ) {
440
            $test->{$order->[$i]} = '*' unless substr( $value, $i, 1 );
441
        }
442
        push @tests, $test;
443
    }
444
445
    return \@tests, $order;
446
}
(-)a/t/db_dependent/Koha/IssuingRules.t (-297 / +1 lines)
Lines 36-42 $schema->storage->txn_begin; Link Here
36
my $builder      = t::lib::TestBuilder->new;
36
my $builder      = t::lib::TestBuilder->new;
37
37
38
subtest 'get_effective_issuing_rule' => sub {
38
subtest 'get_effective_issuing_rule' => sub {
39
    plan tests => 3;
39
    plan tests => 2;
40
40
41
    my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
41
    my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
42
    my $itemtype     = $builder->build({ source => 'Itemtype' })->{'itemtype'};
42
    my $itemtype     = $builder->build({ source => 'Itemtype' })->{'itemtype'};
Lines 94-394 subtest 'get_effective_issuing_rule' => sub { Link Here
94
        );
94
        );
95
    };
95
    };
96
96
97
    subtest 'Get effective issuing rule in correct order' => sub {
98
        plan tests => 26;
99
100
        my $rule;
101
        Koha::CirculationRules->delete;
102
        is(Koha::CirculationRules->search->count, 0, 'There are no issuing rules.');
103
        $rule = Koha::CirculationRules->get_effective_rule({
104
            branchcode   => $branchcode,
105
            categorycode => $categorycode,
106
            itemtype     => $itemtype,
107
            rule_name    => 'fine',
108
        });
109
        is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
110
                        .' is returned.');
111
112
        # undef, undef, undef => 5
113
        ok(Koha::CirculationRule->new({
114
            branchcode => undef,
115
            categorycode => undef,
116
            itemtype => undef,
117
            rule_name => 'fine',
118
            rule_value   => 5,
119
        })->store, 'Given I added an issuing rule branchcode => undef, categorycode => undef, itemtype => undef,');
120
        $rule = Koha::CirculationRules->get_effective_rule({
121
            branchcode   => $branchcode,
122
            categorycode => $categorycode,
123
            itemtype     => $itemtype,
124
            rule_name    => 'fine',
125
        });
126
        _is_row_match(
127
            $rule,
128
            {
129
                branchcode   => undef,
130
                categorycode => undef,
131
                itemtype     => undef,
132
                rule_name    => 'fine',
133
                rule_value   => 5,
134
            },
135
            'When I attempt to get effective issuing rule,'
136
           .' then the above one is returned.'
137
        );
138
139
        # undef, undef, undef     => 5
140
        # undef, undef, $itemtype => 7
141
        ok(
142
            Koha::CirculationRule->new(
143
                {
144
                    branchcode   => undef,
145
                    categorycode => undef,
146
                    itemtype     => $itemtype,
147
                    rule_name    => 'fine',
148
                    rule_value   => 7,
149
                }
150
              )->store,
151
            "Given I added an issuing rule branchcode => undef, categorycode => undef, itemtype => $itemtype,"
152
        );
153
        $rule = Koha::CirculationRules->get_effective_rule({
154
            branchcode   => $branchcode,
155
            categorycode => $categorycode,
156
            itemtype     => $itemtype,
157
            rule_name    => 'fine',
158
        });
159
        _is_row_match(
160
            $rule,
161
            {
162
                branchcode   => undef,
163
                categorycode => undef,
164
                itemtype     => $itemtype,
165
                rule_name    => 'fine',
166
                rule_value   => 7,
167
            },
168
            'When I attempt to get effective issuing rule,'
169
           .' then the above one is returned.'
170
        );
171
172
        # undef, undef,         undef     => 5
173
        # undef, undef,         $itemtype => 7
174
        # undef, $categorycode, undef     => 9
175
        ok(
176
            Koha::CirculationRule->new(
177
                {
178
                    branchcode   => undef,
179
                    categorycode => $categorycode,
180
                    itemtype     => undef,
181
                    rule_name    => 'fine',
182
                    rule_value   => 9,
183
                }
184
              )->store,
185
            "Given I added an issuing rule branchcode => undef, categorycode => $categorycode, itemtype => undef,"
186
        );
187
        $rule = Koha::CirculationRules->get_effective_rule({
188
            branchcode   => $branchcode,
189
            categorycode => $categorycode,
190
            itemtype     => $itemtype,
191
            rule_name    => 'fine',
192
        });
193
        _is_row_match(
194
            $rule,
195
            {
196
                branchcode   => undef,
197
                categorycode => $categorycode,
198
                itemtype     => undef,
199
                rule_name    => 'fine',
200
                rule_value   => 9,
201
            },
202
            'When I attempt to get effective issuing rule,'
203
           .' then the above one is returned.'
204
        );
205
206
        # undef, undef,         undef     => 5
207
        # undef, undef,         $itemtype => 7
208
        # undef, $categorycode, undef     => 9
209
        # undef, $categorycode, $itemtype => 11
210
        ok(
211
            Koha::CirculationRule->new(
212
                {
213
                    branchcode   => undef,
214
                    categorycode => $categorycode,
215
                    itemtype     => $itemtype,
216
                    rule_name    => 'fine',
217
                    rule_value   => 11,
218
                }
219
              )->store,
220
            "Given I added an issuing rule branchcode => undef, categorycode => $categorycode, itemtype => $itemtype,"
221
        );
222
        $rule = Koha::CirculationRules->get_effective_rule({
223
            branchcode   => $branchcode,
224
            categorycode => $categorycode,
225
            itemtype     => $itemtype,
226
            rule_name    => 'fine',
227
        });
228
        _is_row_match(
229
            $rule,
230
            {
231
                branchcode   => undef,
232
                categorycode => $categorycode,
233
                itemtype     => $itemtype,
234
                rule_name    => 'fine',
235
                rule_value   => 11,
236
            },
237
            'When I attempt to get effective issuing rule,'
238
           .' then the above one is returned.'
239
        );
240
241
        # undef,       undef,         undef     => 5
242
        # undef,       undef,         $itemtype => 7
243
        # undef,       $categorycode, undef     => 9
244
        # undef,       $categorycode, $itemtype => 11
245
        # $branchcode, undef,         undef     => 13
246
        ok(
247
            Koha::CirculationRule->new(
248
                {
249
                    branchcode   => $branchcode,
250
                    categorycode => undef,
251
                    itemtype     => undef,
252
                    rule_name    => 'fine',
253
                    rule_value   => 13,
254
                }
255
              )->store,
256
            "Given I added an issuing rule branchcode => $branchcode, categorycode => undef, itemtype => undef,"
257
        );
258
        $rule = Koha::CirculationRules->get_effective_rule({
259
            branchcode   => $branchcode,
260
            categorycode => $categorycode,
261
            itemtype     => $itemtype,
262
            rule_name    => 'fine',
263
        });
264
        _is_row_match(
265
            $rule,
266
            {
267
                branchcode   => $branchcode,
268
                categorycode => undef,
269
                itemtype     => undef,
270
                rule_name    => 'fine',
271
                rule_value   => 13,
272
            },
273
            'When I attempt to get effective issuing rule,'
274
           .' then the above one is returned.'
275
        );
276
277
        # undef,       undef,         undef     => 5
278
        # undef,       undef,         $itemtype => 7
279
        # undef,       $categorycode, undef     => 9
280
        # undef,       $categorycode, $itemtype => 11
281
        # $branchcode, undef,         undef     => 13
282
        # $branchcode, undef,         $itemtype => 15
283
        ok(
284
            Koha::CirculationRule->new(
285
                {
286
                    branchcode   => $branchcode,
287
                    categorycode => undef,
288
                    itemtype     => $itemtype,
289
                    rule_name    => 'fine',
290
                    rule_value   => 15,
291
                }
292
              )->store,
293
            "Given I added an issuing rule branchcode => $branchcode, categorycode => undef, itemtype => $itemtype,"
294
        );
295
        $rule = Koha::CirculationRules->get_effective_rule({
296
            branchcode   => $branchcode,
297
            categorycode => $categorycode,
298
            itemtype     => $itemtype,
299
            rule_name    => 'fine',
300
        });
301
        _is_row_match(
302
            $rule,
303
            {
304
                branchcode   => $branchcode,
305
                categorycode => undef,
306
                itemtype     => $itemtype,
307
                rule_name    => 'fine',
308
                rule_value   => 15,
309
            },
310
            'When I attempt to get effective issuing rule,'
311
           .' then the above one is returned.'
312
        );
313
314
        # undef,       undef,         undef     => 5
315
        # undef,       undef,         $itemtype => 7
316
        # undef,       $categorycode, undef     => 9
317
        # undef,       $categorycode, $itemtype => 11
318
        # $branchcode, undef,         undef     => 13
319
        # $branchcode, undef,         $itemtype => 15
320
        # $branchcode, $categorycode, undef     => 17
321
        ok(
322
            Koha::CirculationRule->new(
323
                {
324
                    branchcode   => $branchcode,
325
                    categorycode => $categorycode,
326
                    itemtype     => undef,
327
                    rule_name    => 'fine',
328
                    rule_value   => 17,
329
                }
330
              )->store,
331
            "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => undef,"
332
        );
333
        $rule = Koha::CirculationRules->get_effective_rule({
334
            branchcode   => $branchcode,
335
            categorycode => $categorycode,
336
            itemtype     => $itemtype,
337
            rule_name    => 'fine',
338
        });
339
        _is_row_match(
340
            $rule,
341
            {
342
                branchcode   => $branchcode,
343
                categorycode => $categorycode,
344
                itemtype     => undef,
345
                rule_name    => 'fine',
346
                rule_value   => 17,
347
            },
348
            'When I attempt to get effective issuing rule,'
349
           .' then the above one is returned.'
350
        );
351
352
        # undef,       undef,         undef     => 5
353
        # undef,       undef,         $itemtype => 7
354
        # undef,       $categorycode, undef     => 9
355
        # undef,       $categorycode, $itemtype => 11
356
        # $branchcode, undef,         undef     => 13
357
        # $branchcode, undef,         $itemtype => 15
358
        # $branchcode, $categorycode, undef     => 17
359
        # $branchcode, $categorycode, $itemtype => 19
360
        ok(
361
            Koha::CirculationRule->new(
362
                {
363
                    branchcode   => $branchcode,
364
                    categorycode => $categorycode,
365
                    itemtype     => $itemtype,
366
                    rule_name    => 'fine',
367
                    rule_value   => 19,
368
                }
369
              )->store,
370
            "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,"
371
        );
372
        $rule = Koha::CirculationRules->get_effective_rule({
373
            branchcode   => $branchcode,
374
            categorycode => $categorycode,
375
            itemtype     => $itemtype,
376
            rule_name    => 'fine',
377
        });
378
        _is_row_match(
379
            $rule,
380
            {
381
                branchcode   => $branchcode,
382
                categorycode => $categorycode,
383
                itemtype     => $itemtype,
384
                rule_name    => 'fine',
385
                rule_value   => 19,
386
            },
387
            'When I attempt to get effective issuing rule,'
388
           .' then the above one is returned.'
389
        );
390
    };
391
392
    subtest 'Performance' => sub {
97
    subtest 'Performance' => sub {
393
        plan tests => 4;
98
        plan tests => 4;
394
99
395
- 

Return to bug 25113