From c6d5b75d6d16bc75b23ef4c7bdf82bfb3eea87db Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Tue, 13 May 2008 12:00:54 -0500 Subject: [PATCH] Bug 1953: adding tests and 2 corrections for C4::Calendar In the course of writing tests for C4::Calendar, I found two bugs and am fixing them here. One is a documentation bug. The other prevented C4::Calendar::insert_exception_holiday from working. I was unable to find anywhere else in the code that depended on the broken behaviour. Signed-off-by: Joshua Ferraro --- C4/Calendar.pm | 6 +- t/lib/KohaTest/Calendar.pm | 36 +++++++ t/lib/KohaTest/Calendar/New.pm | 186 +++++++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+), 3 deletions(-) create mode 100644 t/lib/KohaTest/Calendar.pm create mode 100644 t/lib/KohaTest/Calendar/New.pm diff --git a/C4/Calendar.pm b/C4/Calendar.pm index 239e60344a..e3219d0de0 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -59,7 +59,7 @@ This package is used to deal with holidays. Through this package, you can set al =item new - $calendar = C4::Calendar::Calendar->new(branchcode => $branchcode); + $calendar = C4::Calendar->new(branchcode => $branchcode); C<$branchcode> Is the branch code wich you want to use calendar. @@ -339,8 +339,8 @@ sub insert_exception_holiday { $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); $insertException->finish; - $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; - $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; + $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; return $self; } diff --git a/t/lib/KohaTest/Calendar.pm b/t/lib/KohaTest/Calendar.pm new file mode 100644 index 0000000000..f1825afaa2 --- /dev/null +++ b/t/lib/KohaTest/Calendar.pm @@ -0,0 +1,36 @@ +package KohaTest::Calendar; +use base qw( KohaTest ); + +use strict; +use warnings; + +use Test::More; + +use C4::Calendar; +sub testing_class { 'C4::Calendar' }; + + +sub methods : Test( 1 ) { + my $self = shift; + my @methods = qw( new + _init + change_branchcode + get_week_days_holidays + get_day_month_holidays + get_exception_holidays + get_single_holidays + insert_week_day_holiday + insert_day_month_holiday + insert_single_holiday + insert_exception_holiday + delete_holiday + isHoliday + addDate + daysBetween + ); + + can_ok( $self->testing_class, @methods ); +} + +1; + diff --git a/t/lib/KohaTest/Calendar/New.pm b/t/lib/KohaTest/Calendar/New.pm new file mode 100644 index 0000000000..745366b434 --- /dev/null +++ b/t/lib/KohaTest/Calendar/New.pm @@ -0,0 +1,186 @@ +package KohaTest::Calendar::New; +use base qw( KohaTest ); + +use strict; +use warnings; + +use Test::More; + +use C4::Calendar; +sub testing_class { 'C4::Calendar' }; + + +=head2 STARTUP METHODS + +These get run once, before the main test methods in this module + +=cut + +=head2 TEST METHODS + +standard test methods + +=head3 instantiation + + just test to see if I can instantiate an object + +=cut + +sub instantiation : Test( 14 ) { + my $self = shift; + + my $calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + + ok( exists $calendar->{'day_month_holidays'}, 'day_month_holidays' ); + ok( exists $calendar->{'single_holidays'}, 'single_holidays' ); + ok( exists $calendar->{'week_days_holidays'}, 'week_days_holidays' ); + ok( exists $calendar->{'exception_holidays'}, 'exception_holidays' ); + + # sample data has Sundays as a holiday + ok( exists $calendar->{'week_days_holidays'}->{'0'} ); + is( $calendar->{'week_days_holidays'}->{'0'}->{'title'}, '', 'Sunday title' ); + is( $calendar->{'week_days_holidays'}->{'0'}->{'description'}, 'Sundays', 'Sunday description' ); + + # sample data has Christmas as a holiday + ok( exists $calendar->{'day_month_holidays'}->{'12/25'} ); + is( $calendar->{'day_month_holidays'}->{'12/25'}->{'title'}, '', 'Christmas title' ); + is( $calendar->{'day_month_holidays'}->{'12/25'}->{'description'}, 'Christmas', 'Christmas description' ); + + # sample data has New Year's Day as a holiday + ok( exists $calendar->{'day_month_holidays'}->{'1/1'} ); + is( $calendar->{'day_month_holidays'}->{'1/1'}->{'title'}, '', 'New Year title' ); + is( $calendar->{'day_month_holidays'}->{'1/1'}->{'description'}, q(New Year's Day), 'New Year description' ); + +} + +sub week_day_holidays : Test( 8 ) { + my $self = shift; + + my $calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + + ok( exists $calendar->{'week_days_holidays'}, 'week_days_holidays' ); + + my %new_holiday = ( weekday => 1, + title => 'example week_day_holiday', + description => 'This is an example week_day_holiday used for testing' ); + my $new_calendar = $calendar->insert_week_day_holiday( %new_holiday ); + + # the calendar object returned from insert_week_day_holiday should be updated + isa_ok( $new_calendar, 'C4::Calendar' ); + is( $new_calendar->{'week_days_holidays'}->{ $new_holiday{'weekday'} }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'week_days_holidays'}->{ $new_holiday{'weekday'} }->{'description'}, $new_holiday{'description'}, 'description' ); + + # new calendar objects should have the newly inserted holiday. + my $refreshed_calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $refreshed_calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + is( $new_calendar->{'week_days_holidays'}->{ $new_holiday{'weekday'} }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'week_days_holidays'}->{ $new_holiday{'weekday'} }->{'description'}, $new_holiday{'description'}, 'description' ); + +} + + +sub day_month_holidays : Test( 8 ) { + my $self = shift; + + my $calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + + ok( exists $calendar->{'day_month_holidays'}, 'day_month_holidays' ); + + my %new_holiday = ( day => 4, + month => 5, + title => 'example day_month_holiday', + description => 'This is an example day_month_holiday used for testing' ); + my $new_calendar = $calendar->insert_day_month_holiday( %new_holiday ); + + # the calendar object returned from insert_week_day_holiday should be updated + isa_ok( $new_calendar, 'C4::Calendar' ); + my $mmdd = sprintf('%s/%s', $new_holiday{'month'}, $new_holiday{'day'} ) ; + is( $new_calendar->{'day_month_holidays'}->{ $mmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'day_month_holidays'}->{ $mmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + + # new calendar objects should have the newly inserted holiday. + my $refreshed_calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $refreshed_calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + is( $new_calendar->{'day_month_holidays'}->{ $mmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'day_month_holidays'}->{ $mmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + +} + + + +sub exception_holidays : Test( 8 ) { + my $self = shift; + + my $calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + + ok( exists $calendar->{'exception_holidays'}, 'exception_holidays' ); + + my %new_holiday = ( day => 4, + month => 5, + year => 2010, + title => 'example exception_holiday', + description => 'This is an example exception_holiday used for testing' ); + my $new_calendar = $calendar->insert_exception_holiday( %new_holiday ); + # diag( Data::Dumper->Dump( [ $new_calendar ], [ 'newcalendar' ] ) ); + + # the calendar object returned from insert_week_day_holiday should be updated + isa_ok( $new_calendar, 'C4::Calendar' ); + my $yyyymmdd = sprintf('%s/%s/%s', $new_holiday{'year'}, $new_holiday{'month'}, $new_holiday{'day'} ) ; + is( $new_calendar->{'exception_holidays'}->{ $yyyymmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'exception_holidays'}->{ $yyyymmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + + # new calendar objects should have the newly inserted holiday. + my $refreshed_calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $refreshed_calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + is( $new_calendar->{'exception_holidays'}->{ $yyyymmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'exception_holidays'}->{ $yyyymmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + +} + + +sub single_holidays : Test( 8 ) { + my $self = shift; + + my $calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + + ok( exists $calendar->{'single_holidays'}, 'single_holidays' ); + + my %new_holiday = ( day => 4, + month => 5, + year => 2011, + title => 'example single_holiday', + description => 'This is an example single_holiday used for testing' ); + my $new_calendar = $calendar->insert_single_holiday( %new_holiday ); + # diag( Data::Dumper->Dump( [ $new_calendar ], [ 'newcalendar' ] ) ); + + # the calendar object returned from insert_week_day_holiday should be updated + isa_ok( $new_calendar, 'C4::Calendar' ); + my $yyyymmdd = sprintf('%s/%s/%s', $new_holiday{'year'}, $new_holiday{'month'}, $new_holiday{'day'} ) ; + is( $new_calendar->{'single_holidays'}->{ $yyyymmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'single_holidays'}->{ $yyyymmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + + # new calendar objects should have the newly inserted holiday. + my $refreshed_calendar = C4::Calendar->new( branchcode => '' ); + isa_ok( $refreshed_calendar, 'C4::Calendar' ); + # diag( Data::Dumper->Dump( [ $calendar ], [ 'calendar' ] ) ); + is( $new_calendar->{'single_holidays'}->{ $yyyymmdd }->{'title'}, $new_holiday{'title'}, 'title' ); + is( $new_calendar->{'single_holidays'}->{ $yyyymmdd }->{'description'}, $new_holiday{'description'}, 'description' ); + +} + + +1; + -- 2.39.5