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

(-)a/t/db_dependent/Holidays.t (-2 / +116 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 16;
20
use Test::More tests => 17;
21
21
22
use DateTime;
22
use DateTime;
23
use DateTime::TimeZone;
23
use DateTime::TimeZone;
Lines 203-205 sub _add_exception { Link Here
203
203
204
$schema->storage->txn_rollback;
204
$schema->storage->txn_rollback;
205
205
206
- 
206
subtest 'copy_to_branch' => sub {
207
    
208
    plan tests => 8;
209
210
    $schema->storage->txn_begin;
211
212
    my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
213
    my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
214
    my $sunday = dt_from_string("2020-03-15");
215
    $calendar1->insert_week_day_holiday(
216
        weekday     => 0,
217
        title       => '',
218
        description => 'Sundays',
219
    );
220
221
    my $day_month = dt_from_string("2020-03-17");
222
    $calendar1->insert_day_month_holiday(
223
        day         => $day_month->day(),
224
        month       => $day_month->month(),
225
        year        => $day_month->year(),
226
        title       => '',
227
        description => "",
228
    );
229
230
    my $future_date = dt_from_string("9999-12-31");
231
    $calendar1->insert_single_holiday(
232
        day         => $future_date->day(),
233
        month       => $future_date->month(),
234
        year        => $future_date->year(),
235
        title       => "",
236
        description => "",
237
    );
238
239
    my $future_exception = dt_from_string("9999-12-30");
240
    $calendar1->insert_exception_holiday(
241
        day         => $future_exception->day(),
242
        month       => $future_exception->month(),
243
        year        => $future_exception->year(),
244
        title       => "",
245
        description => "",
246
    );
247
248
    my $past_date = dt_from_string("2019-11-20");
249
    $calendar1->insert_single_holiday(
250
        day         => $past_date->day(),
251
        month       => $past_date->month(),
252
        year        => $past_date->year(),
253
        title       => "",
254
        description => "",
255
    );
256
257
    my $past_exception = dt_from_string("2020-03-09");
258
    $calendar1->insert_exception_holiday(
259
        day         => $past_exception->day(),
260
        month       => $past_exception->month(),
261
        year        => $past_exception->year(),
262
        title       => "",
263
        description => "",
264
    );
265
266
    my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
267
268
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
269
270
    my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
271
    my $exceptions = $calendar2->get_exception_holidays;
272
273
    is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
274
    is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
275
    is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
276
    is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
277
    is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
278
    is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
279
280
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
281
282
    #Select all rows with same values from database
283
    my $dbh = C4::Context->dbh;
284
    my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a 
285
        JOIN (SELECT branchcode, weekday, day, month, COUNT(*) 
286
        FROM repeatable_holidays 
287
        GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b 
288
        ON a.branchcode = b.branchcode 
289
        AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month)) 
290
        ORDER BY a.branchcode;";
291
    my $sth  = $dbh->prepare($get_repeatable_holidays);
292
    $sth->execute;
293
294
    my @repeatable_holidays;
295
    while(my $row = $sth->fetchrow_hashref){
296
        push @repeatable_holidays, $row
297
    }
298
    
299
    is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
300
301
    my $get_special_holidays = "SELECT a.* FROM special_holidays a 
302
    JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*) 
303
    FROM special_holidays 
304
    GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b 
305
    ON a.branchcode = b.branchcode 
306
    AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
307
    ORDER BY a.branchcode;";
308
    $sth  = $dbh->prepare($get_special_holidays);
309
    $sth->execute;
310
311
    my @special_holidays;
312
    while(my $row = $sth->fetchrow_hashref){
313
        push @special_holidays, $row
314
    }
315
316
    is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
317
318
    $schema->storage->txn_rollback;
319
320
}

Return to bug 19475