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

(-)a/t/db_dependent/Koha/CirculationRules.t (-137 / +71 lines)
Lines 32-38 my $schema = Koha::Database->new->schema; Link Here
32
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
33
33
34
subtest 'set_rule + get_effective_rule' => sub {
34
subtest 'set_rule + get_effective_rule' => sub {
35
    plan tests => 14;
35
    plan tests => 8;
36
36
37
    $schema->storage->txn_begin;
37
    $schema->storage->txn_begin;
38
38
Lines 90-116 subtest 'set_rule + get_effective_rule' => sub { Link Here
90
90
91
    is( $rule->rule_value, $default_rule_value, '* means default' );
91
    is( $rule->rule_value, $default_rule_value, '* means default' );
92
92
93
    Koha::CirculationRules->set_rule(
94
        {
95
            branchcode   => '*',
96
            categorycode => '*',
97
            itemtype     => $itemtype,
98
            rule_name    => $rule_name,
99
            rule_value   => 2,
100
        }
101
    );
102
103
    $rule = Koha::CirculationRules->get_effective_rule(
104
        {
105
            branchcode   => $branchcode,
106
            categorycode => $categorycode,
107
            itemtype     => $itemtype,
108
            rule_name    => $rule_name,
109
        }
110
    );
111
    is( $rule->rule_value, 2,
112
        'More specific rule is returned when itemtype is given' );
113
114
    $rule = Koha::CirculationRules->get_effective_rule(
93
    $rule = Koha::CirculationRules->get_effective_rule(
115
        {
94
        {
116
            branchcode   => $branchcode_2,
95
            branchcode   => $branchcode_2,
Lines 122-250 subtest 'set_rule + get_effective_rule' => sub { Link Here
122
    is( $rule->rule_value, 1,
101
    is( $rule->rule_value, 1,
123
        'Default rule is returned if there is no rule for this branchcode' );
102
        'Default rule is returned if there is no rule for this branchcode' );
124
103
125
    Koha::CirculationRules->set_rule(
104
    subtest 'test rule matching with different combinations of rule scopes' => sub {
126
        {
127
            branchcode   => '*',
128
            categorycode => $categorycode,
129
            itemtype     => '*',
130
            rule_name    => $rule_name,
131
            rule_value   => 3,
132
        }
133
    );
134
105
135
    $rule = Koha::CirculationRules->get_effective_rule(
106
        my ( $tests, $order ) = prepare_tests_for_rule_scope_combinations( {
136
        {
107
            branchcode => $branchcode,
137
138
            branchcode   => $branchcode,
139
            categorycode => $categorycode,
108
            categorycode => $categorycode,
140
            itemtype     => $itemtype,
109
            itemtype => $itemtype,
141
            rule_name    => $rule_name,
110
        }, 'maxissueqty' );
142
        }
143
    );
144
    is( $rule->rule_value, 3,
145
        'More specific rule is returned when categorycode exists' );
146
111
147
    Koha::CirculationRules->set_rule(
112
        plan tests => 2**scalar @$order;
148
        {
149
            branchcode   => '*',
150
            categorycode => $categorycode,
151
            itemtype     => $itemtype,
152
            rule_name    => $rule_name,
153
            rule_value   => 4,
154
        }
155
    );
156
    $rule = Koha::CirculationRules->get_effective_rule(
157
        {
158
            branchcode   => $branchcode,
159
            categorycode => $categorycode,
160
            itemtype     => $itemtype,
161
            rule_name    => $rule_name,
162
        }
163
    );
164
    is( $rule->rule_value, 4,
165
        'More specific rule is returned when categorycode and itemtype exist' );
166
113
167
    Koha::CirculationRules->set_rule(
114
        foreach my $test ( @$tests ) {
168
        {
115
            my $rule_params = { %$test };
169
            branchcode   => $branchcode,
116
            $rule_params->{rule_name} = $rule_name;
170
            categorycode => '*',
117
            my $rule_value = $rule_params->{rule_value} = int( rand( 10 ) );
171
            itemtype     => '*',
172
            rule_name    => $rule_name,
173
            rule_value   => 5,
174
        }
175
    );
176
    $rule = Koha::CirculationRules->get_effective_rule(
177
        {
178
            branchcode   => $branchcode,
179
            categorycode => $categorycode,
180
            itemtype     => $itemtype,
181
            rule_name    => $rule_name,
182
        }
183
    );
184
    is( $rule->rule_value, 5,
185
        'More specific rule is returned when branchcode exists' );
186
118
187
    Koha::CirculationRules->set_rule(
119
            Koha::CirculationRules->set_rule( $rule_params );
188
        {
189
            branchcode   => $branchcode,
190
            categorycode => '*',
191
            itemtype     => $itemtype,
192
            rule_name    => $rule_name,
193
            rule_value   => 6,
194
        }
195
    );
196
    $rule = Koha::CirculationRules->get_effective_rule(
197
        {
198
            branchcode   => $branchcode,
199
            categorycode => $categorycode,
200
            itemtype     => $itemtype,
201
            rule_name    => $rule_name,
202
        }
203
    );
204
    is( $rule->rule_value, 6,
205
        'More specific rule is returned when branchcode and itemtype exists' );
206
120
207
    Koha::CirculationRules->set_rule(
121
            my $rule = Koha::CirculationRules->get_effective_rule( {
208
        {
122
                branchcode => $branchcode,
209
            branchcode   => $branchcode,
123
                categorycode => $categorycode,
210
            categorycode => $categorycode,
124
                itemtype => $itemtype,
211
            itemtype     => '*',
125
                rule_name => $rule_name,
212
            rule_name    => $rule_name,
126
            } );
213
            rule_value   => 7,
214
        }
215
    );
216
    $rule = Koha::CirculationRules->get_effective_rule(
217
        {
218
            branchcode   => $branchcode,
219
            categorycode => $categorycode,
220
            itemtype     => $itemtype,
221
            rule_name    => $rule_name,
222
        }
223
    );
224
    is( $rule->rule_value, 7,
225
        'More specific rule is returned when branchcode and categorycode exist'
226
    );
227
127
228
    Koha::CirculationRules->set_rule(
128
            my $scope_output = '';
229
        {
129
            foreach my $key ( values @$order ) {
230
            branchcode   => $branchcode,
130
                $scope_output .= " $key" if $test->{$key} ne '*';
231
            categorycode => $categorycode,
131
            }
232
            itemtype     => $itemtype,
132
233
            rule_name    => $rule_name,
133
            is( $rule->rule_value, $rule_value,
234
            rule_value   => 8,
134
                'Explicitly scoped' . ( $scope_output
235
        }
135
                                       ? $scope_output : ' nothing' ) );
236
    );
237
    $rule = Koha::CirculationRules->get_effective_rule(
238
        {
239
            branchcode   => $branchcode,
240
            categorycode => $categorycode,
241
            itemtype     => $itemtype,
242
            rule_name    => $rule_name,
243
        }
136
        }
244
    );
137
    };
245
    is( $rule->rule_value, 8,
246
        'More specific rule is returned when branchcode, categorycode and itemtype exist'
247
    );
248
138
249
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
139
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
250
    is( $our_branch_rules->count, 4, "We added 8 rules");
140
    is( $our_branch_rules->count, 4, "We added 8 rules");
Lines 376-378 subtest 'get_effective_daysmode' => sub { Link Here
376
266
377
    $schema->storage->txn_rollback;
267
    $schema->storage->txn_rollback;
378
};
268
};
269
270
sub prepare_tests_for_rule_scope_combinations {
271
    my ( $scope, $rule_name ) = @_;
272
273
    # Here we create a combinations of 1s and 0s the following way
274
    #
275
    # 000...
276
    # 001...
277
    # 010...
278
    # 011...
279
    # 100...
280
    # 101...
281
    # 110...
282
    # 111...
283
    #
284
    # (the number of columns equals to the amount of rule scopes)
285
    # The ... symbolizes possible future scopes.
286
    #
287
    # - 0 equals to circulation rule scope with any value (aka. *)
288
    # - 1 equals to circulation rule scope exact value, e.g.
289
    #     "CPL" (for branchcode).
290
    #
291
    # The order is the same as the weight of scopes when sorting circulation
292
    # rules. So the first column of numbers is the scope with most weight.
293
    # This is defined by C<$order> which will be assigned next.
294
    #
295
    # We must maintain the order in order to keep the test valid. This should be
296
    # equal to Koha/CirculationRules.pm "order_by" of C<get_effective_rule> sub.
297
    # Let's explicitly define the order and fail test if we are missing a scope:
298
    my $order = [ 'branchcode', 'categorycode', 'itemtype' ];
299
    is( join(", ", sort keys %$scope),
300
       join(", ", sort @$order), 'Missing a scope!' ) if keys %$scope ne scalar @$order;
301
302
    my @tests = ();
303
    foreach my $value ( glob( "{0,1}" x keys %$scope || 1 ) ) {
304
        my $test = { %$scope };
305
        for ( my $i=0; $i < keys %$scope; $i++ ) {
306
            $test->{$order->[$i]} = '*' unless substr( $value, $i, 1 );
307
        }
308
        push @tests, $test;
309
    }
310
311
    return \@tests, $order;
312
}
(-)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