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

(-)a/C4/ImportBatch.pm (-5 / +15 lines)
Lines 1608-1613 sub _create_import_record { Link Here
1608
    return $import_record_id;
1608
    return $import_record_id;
1609
}
1609
}
1610
1610
1611
=head2 _get_import_record_timestamp
1612
1613
    my $timestamp = C4::ImportBatch::_get_import_record_timestamp($record);
1614
1615
Get the date in field 005 and return it formatted as a timestamp.
1616
1617
The argument is a C<MARC::Record> object
1618
1619
=cut
1620
1611
sub _get_import_record_timestamp {
1621
sub _get_import_record_timestamp {
1612
    my ( $marc_record ) = @_;
1622
    my ( $marc_record ) = @_;
1613
    my $upload_timestamp = DateTime->now();
1623
    my $upload_timestamp = DateTime->now();
Lines 1616-1627 sub _get_import_record_timestamp { Link Here
1616
    # tenth-of-a-second ourselves.
1626
    # tenth-of-a-second ourselves.
1617
    my $f005 = $marc_record->field('005');
1627
    my $f005 = $marc_record->field('005');
1618
    if ($f005 && $f005->data =~ /(\d{8}\d{6})\.(\d)/ ) {
1628
    if ($f005 && $f005->data =~ /(\d{8}\d{6})\.(\d)/ ) {
1619
    my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' );
1629
        my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' );
1620
    my $parsed_timestamp = $parser->parse_datetime($1);
1630
        my $parsed_timestamp = $parser->parse_datetime($1);
1621
1631
1622
    # We still check for success because we only did enough validation above to extract the
1632
        # We still check for success because we only did enough validation above to extract the
1623
    # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober.
1633
        # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober.
1624
    if ( $parsed_timestamp ) {
1634
        if ( $parsed_timestamp ) {
1625
            $parsed_timestamp->set_nanosecond( $2 * 100_000_000 );
1635
            $parsed_timestamp->set_nanosecond( $2 * 100_000_000 );
1626
            $upload_timestamp = $parsed_timestamp;
1636
            $upload_timestamp = $parsed_timestamp;
1627
        }
1637
        }
(-)a/t/db_dependent/Biblio/MarcTimestamp.t (+99 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
#
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
22
use t::lib::TestBuilder;
23
use t::lib::Mocks;
24
use MARC::Record;
25
use C4::Biblio;
26
use C4::ImportBatch;
27
use DateTime;
28
use Koha::Database;
29
30
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
32
33
my $builder = t::lib::TestBuilder->new();
34
35
t::lib::Mocks::mock_preference( 'marcflavour', 'NORMARC' );
36
37
my $record = MARC::Record->new;
38
$record->append_fields(
39
    MARC::Field->new( '100', '', '', a => 'My title' ),
40
    MARC::Field->new( '005', '20140508101202.0' ),
41
);
42
43
C4::Biblio::UpdateMarcTimestamp( $record );
44
45
my $f005 = $record->field('005')->data;
46
47
is($f005, '20140508101202.0', "Field 005 didn't change because of the normarc marc flavour.");
48
49
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
50
51
my $dt = DateTime->now;
52
my $expected = $dt->ymd('');
53
54
C4::Biblio::UpdateMarcTimestamp( $record );
55
56
$f005 = $record->field('005')->data;
57
ok($f005 =~ /^$expected\d{6}\.\d/);
58
59
$record->field('005')->update('20140508101202.0');
60
61
my $timestamp = C4::ImportBatch::_get_import_record_timestamp($record);
62
63
is($timestamp, '2014-05-08T10:12:02', "Get correct record's timestamp");
64
65
my $import_batch = $builder->build(
66
    {
67
        source => 'ImportBatch',
68
    }
69
);
70
71
my $import_record_id = C4::ImportBatch::_create_import_record(
72
    $import_batch->{import_batch_id},
73
    1, $record, 'biblio', 'UTF-8',
74
);
75
76
$timestamp = get_import_record_timestamp($import_record_id);
77
ok($f005 =~ /^$expected\d{6}\.\d/);
78
79
80
C4::ImportBatch::_update_import_record_marc($import_record_id, $record);
81
82
$timestamp = get_import_record_timestamp($import_record_id);
83
is($timestamp, '2014-05-08 10:12:02', "import_records.upload_timestamp correctly updated.");
84
85
86
sub get_import_record_timestamp {
87
    my $import_record_id = shift;
88
89
    my $dbh = C4::Context->dbh;
90
    my $timestamp = $dbh->selectrow_array(q|
91
        SELECT upload_timestamp
92
        FROM import_records
93
        WHERE import_record_id = ?
94
    |, undef, $import_record_id );
95
96
    return $timestamp;
97
}
98
99
$schema->storage->txn_rollback;
(-)a/t/db_dependent/ImportBatch.t (-2 / +34 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 14;
4
use Test::More tests => 15;
5
use File::Basename;
5
use File::Basename;
6
use File::Temp qw/tempfile/;
6
use File::Temp qw/tempfile/;
7
7
Lines 198-201 subtest "RecordsFromMarcPlugin" => sub { Link Here
198
        'Checked one field in second record' );
198
        'Checked one field in second record' );
199
};
199
};
200
200
201
subtest "add_biblio_fields to import record" => sub {
202
203
my $record = MARC::Record->new;
204
    plan tests => 4;
205
206
    $record->append_fields(
207
        MARC::Field->new( '100', '', '', a => 'My author' ),
208
        MARC::Field->new( '245', '', '', a => 'My title' ),
209
        MARC::Field->new( '001', '111' ),
210
    );
211
212
    my $import_batch = $builder->build(
213
        {
214
            source => 'ImportBatch',
215
        }
216
    );
217
218
    my $import_record_id = C4::ImportBatch::_create_import_record(
219
        $import_batch->{import_batch_id},
220
        1, $record, 'biblio', 'UTF-8',
221
    );
222
223
    my $import_biblios = GetImportBiblios($import_record_id);
224
    is(scalar(@$import_biblios), 0, 'No import_biblios yet');
225
226
    C4::ImportBatch::_add_biblio_fields($import_record_id, $record);
227
228
    $import_biblios = GetImportBiblios($import_record_id);
229
    is($import_biblios->[0]{title}, 'My title', 'Import biblio title');
230
    is($import_biblios->[0]{author}, 'My author', 'Import biblio author');
231
    is($import_biblios->[0]{control_number}, 111, 'Import biblio control number');
232
};
233
201
$schema->storage->txn_rollback;
234
$schema->storage->txn_rollback;
202
- 

Return to bug 18823