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

(-)a/t/Test/Dates.t (+26 lines)
Line 0 Link Here
1
use Modern::Perl;
2
use Test::More tests => 7;
3
use t::lib::Dates;
4
use Koha::DateUtils qw( dt_from_string );
5
6
my $date_1 = '2017-01-01 01:00:00';
7
my $date_2 = '2018-02-02 01:00:00';
8
my $dt_1   = dt_from_string($date_1);
9
my $dt_2   = dt_from_string($date_2);
10
11
is( t::lib::Dates::compare( $dt_1, $dt_2 ), -1, '2017 is before 2018' );
12
is( t::lib::Dates::compare( $dt_2, $dt_1 ), 1,  '2018 is after 2017' );
13
14
is( t::lib::Dates::compare( $date_1, $date_2 ), -1, '2017 is before 2018 (strings comparison)' );
15
is( t::lib::Dates::compare( $date_2, $date_1 ), 1,  '2018 is after 2017 (strings comparison)' );
16
17
my $dt_3 = $dt_1->clone->subtract( seconds => 59 );
18
is( t::lib::Dates::compare( $dt_1, $dt_3 ),
19
    0, 'If there is less than 1min, the dates are considered identicals' );
20
is( t::lib::Dates::compare( $dt_3, $dt_1 ),
21
    0, 'If there is less than 1min, the dates are considered identicals' );
22
23
$dt_1->set_time_zone('+0000');
24
$dt_3 = $dt_1->clone->set_time_zone('+0400');
25
26
is( t::lib::Dates::compare( $dt_1, $dt_3 ), -1, "Compare different timezones" );
(-)a/t/db_dependent/Patrons.t (-3 / +4 lines)
Lines 24-29 use C4::Context; Link Here
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::DateUtils;
25
use Koha::DateUtils;
26
26
27
use t::lib::Dates;
27
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
28
29
29
BEGIN {
30
BEGIN {
Lines 71-83 $b3->store(); Link Here
71
my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
72
my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
72
is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
73
is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
73
isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
74
isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
74
is( dt_from_string($b1_new->updated_on), $now, "borrowers.updated_on should have been set to now on creating" );
75
is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on creating" );
75
76
76
my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
77
my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
77
is( dt_from_string($b3_new->updated_on), $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" );
78
is( t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago), 0, "borrowers.updated_on should have been kept to what we set on creating" );
78
$b3_new->set({ firstname => 'Some first name for Test 3' })->store();
79
$b3_new->set({ firstname => 'Some first name for Test 3' })->store();
79
$b3_new = Koha::Patrons->find( $b3->borrowernumber() );
80
$b3_new = Koha::Patrons->find( $b3->borrowernumber() );
80
is( dt_from_string($b3_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on updating" );
81
is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on updating" );
81
82
82
my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
83
my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
83
is( @patrons, 3, "Found 3 patrons with Search" );
84
is( @patrons, 3, "Found 3 patrons with Search" );
(-)a/t/db_dependent/Virtualshelves.t (-4 / +5 lines)
Lines 10-15 use Koha::Virtualshelves; Link Here
10
use Koha::Virtualshelfshares;
10
use Koha::Virtualshelfshares;
11
use Koha::Virtualshelfcontents;
11
use Koha::Virtualshelfcontents;
12
12
13
use t::lib::Dates;
13
use t::lib::TestBuilder;
14
use t::lib::TestBuilder;
14
15
15
my $builder = t::lib::TestBuilder->new;
16
my $builder = t::lib::TestBuilder->new;
Lines 41-47 subtest 'CRUD' => sub { Link Here
41
    is( $number_of_shelves, 1, '1 shelf should have been inserted' );
42
    is( $number_of_shelves, 1, '1 shelf should have been inserted' );
42
    is( $shelf->allow_change_from_owner, 1, 'The default value for allow_change_from_owner should be 1' );
43
    is( $shelf->allow_change_from_owner, 1, 'The default value for allow_change_from_owner should be 1' );
43
    is( $shelf->allow_change_from_others, 0, 'The default value for allow_change_from_others should be 0' );
44
    is( $shelf->allow_change_from_others, 0, 'The default value for allow_change_from_others should be 0' );
44
    is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
45
    is( t::lib::Dates::compare( $shelf->created_on, dt_from_string), 0, 'The creation time should have been set to today' );
45
46
46
    # Test if creation date will not be overwritten by store
47
    # Test if creation date will not be overwritten by store
47
    my $created = dt_from_string->subtract( hours => 1 );
48
    my $created = dt_from_string->subtract( hours => 1 );
Lines 51-57 subtest 'CRUD' => sub { Link Here
51
    my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
52
    my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
52
53
53
    is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
54
    is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
54
    is( dt_from_string($retrieved_shelf->created_on), $created, 'Creation date is the same after update (Bug 18672)' );
55
    is( t::lib::Dates::compare( $retrieved_shelf->created_on, $created), 0, 'Creation date is the same after update (Bug 18672)' );
55
56
56
    # Insert with the same name
57
    # Insert with the same name
57
    eval {
58
    eval {
Lines 181-191 subtest 'Shelf content' => sub { Link Here
181
    )->store;
182
    )->store;
182
183
183
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
184
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
184
    is( output_pref( dt_from_string $shelf->lastmodified ), output_pref($dt_yesterday), 'The lastmodified has been set to yesterday, will be useful for another test later' );
185
    is( t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday), 0, 'The lastmodified has been set to yesterday, will be useful for another test later' );
185
    my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
186
    my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
186
    is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
187
    is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
187
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
188
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
188
    is( output_pref( dt_from_string( $shelf->lastmodified ) ), output_pref(dt_from_string), 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
189
    is( t::lib::Dates::compare( $shelf->lastmodified, dt_from_string), 0, 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
189
    my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
190
    my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
190
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
191
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
191
    is( $number_of_contents, 2, '2 biblio should have been inserted' );
192
    is( $number_of_contents, 2, '2 biblio should have been inserted' );
(-)a/t/lib/Dates.pm (-1 / +27 lines)
Line 0 Link Here
0
- 
1
package t::lib::Dates;
2
3
use Modern::Perl;
4
use Test::More;
5
use Koha::DateUtils;
6
use DateTime;
7
=head2 compare
8
9
  compare( $got_dt, $expected_dt, $test_description );
10
11
Will execute a test and compare the 2 dates given in parameters
12
The date will be compared truncated to minutes
13
14
=cut
15
16
sub compare {
17
    my ( $got, $expected, $description ) = @_;
18
    my $dt_got      = dt_from_string($got);
19
    my $dt_expected = dt_from_string($expected);
20
    $dt_got->set_time_zone('floating');
21
    $dt_expected->set_time_zone('floating');
22
    my $diff = $dt_got->epoch - $dt_expected->epoch;
23
    if ( abs($diff) < 60 ) { return 0 }
24
    return $diff > 0 ? 1 : -1;
25
}
26
27
1;

Return to bug 19176