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

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