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

(-)a/t/db_dependent/Koha/CirculationRules.t (-137 / +66 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( $rule_params );
207
        {
208
            branchcode   => $branchcode,
209
            categorycode => $categorycode,
210
            itemtype     => '*',
211
            rule_name    => $rule_name,
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
121
227
    Koha::CirculationRules->set_rule(
122
            my $scope_output = '';
228
        {
123
            foreach my $key ( values @$order ) {
229
            branchcode   => $branchcode,
124
                $scope_output .= " $key" if $test->{$key} ne '*';
230
            categorycode => $categorycode,
125
            }
231
            itemtype     => $itemtype,
126
232
            rule_name    => $rule_name,
127
            is( $rule->rule_value, $rule_value,
233
            rule_value   => 8,
128
                'Explicitly scoped' . ( $scope_output
234
        }
129
                                       ? $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
        }
130
        }
243
    );
131
    };
244
    is( $rule->rule_value, 8,
245
        'More specific rule is returned when branchcode, categorycode and itemtype exist'
246
    );
247
132
248
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
133
    my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
249
    is( $our_branch_rules->count, 4, "We added 8 rules");
134
    is( $our_branch_rules->count, 4, "We added 8 rules");
Lines 282-284 subtest 'get_onshelfholds_policy() tests' => sub { Link Here
282
167
283
    $schema->storage->txn_rollback;
168
    $schema->storage->txn_rollback;
284
};
169
};
170
171
sub prepare_tests_for_rule_scope_combinations {
172
    my ( $scope, $rule_name ) = @_;
173
174
    # Here we create a combinations of 1s and 0s the following way
175
    #
176
    # 000...
177
    # 001...
178
    # 010...
179
    # 011...
180
    # 100...
181
    # 101...
182
    # 110...
183
    # 111...
184
    #
185
    # (the number of columns equals to the amount of rule scopes)
186
    # The ... symbolizes possible future scopes.
187
    #
188
    # - 0 equals to circulation rule scope with any value (aka. *)
189
    # - 1 equals to circulation rule scope exact value, e.g.
190
    #     "CPL" (for branchcode).
191
    #
192
    # The order is the same as the weight of scopes when sorting circulation
193
    # rules. So the first column of numbers is the scope with most weight.
194
    # This is defined by C<$order> which will be assigned next.
195
    #
196
    # We must maintain the order in order to keep the test valid. This should be
197
    # equal to Koha/CirculationRules.pm "order_by" of C<get_effective_rule> sub.
198
    # Let's explicitly define the order and fail test if we are missing a scope:
199
    my $order = [ 'branchcode', 'categorycode', 'itemtype' ];
200
    is( join(", ", sort keys %$scope),
201
       join(", ", sort @$order), 'Missing a scope!' ) if keys %$scope ne scalar @$order;
202
203
    my @tests = ();
204
    foreach my $value ( glob( "{0,1}" x keys %$scope || 1 ) ) {
205
        my $test = { %$scope };
206
        for ( my $i=0; $i < keys %$scope; $i++ ) {
207
            $test->{$order->[$i]} = '*' unless substr( $value, $i, 1 );
208
        }
209
        push @tests, $test;
210
    }
211
212
    return \@tests, $order;
213
}
(-)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