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

(-)a/C4/ImportBatch.pm (-5 / +15 lines)
Lines 1606-1611 sub _create_import_record { Link Here
1606
    return $import_record_id;
1606
    return $import_record_id;
1607
}
1607
}
1608
1608
1609
=head2 _get_import_record_timestamp
1610
1611
    my $timestamp = C4::ImportBatch::_get_import_record_timestamp($record);
1612
1613
Get the date in field 005 and return it formatted as a timestamp.
1614
1615
The argument is a C<MARC::Record> object
1616
1617
=cut
1618
1609
sub _get_import_record_timestamp {
1619
sub _get_import_record_timestamp {
1610
    my ( $marc_record ) = @_;
1620
    my ( $marc_record ) = @_;
1611
    my $upload_timestamp = DateTime->now();
1621
    my $upload_timestamp = DateTime->now();
Lines 1614-1625 sub _get_import_record_timestamp { Link Here
1614
    # tenth-of-a-second ourselves.
1624
    # tenth-of-a-second ourselves.
1615
    my $f005 = $marc_record->field('005');
1625
    my $f005 = $marc_record->field('005');
1616
    if ($f005 && $f005->data =~ /(\d{8}\d{6})\.(\d)/ ) {
1626
    if ($f005 && $f005->data =~ /(\d{8}\d{6})\.(\d)/ ) {
1617
    my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' );
1627
        my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' );
1618
    my $parsed_timestamp = $parser->parse_datetime($1);
1628
        my $parsed_timestamp = $parser->parse_datetime($1);
1619
1629
1620
    # We still check for success because we only did enough validation above to extract the
1630
        # We still check for success because we only did enough validation above to extract the
1621
    # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober.
1631
        # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober.
1622
    if ( $parsed_timestamp ) {
1632
        if ( $parsed_timestamp ) {
1623
            $parsed_timestamp->set_nanosecond( $2 * 100_000_000 );
1633
            $parsed_timestamp->set_nanosecond( $2 * 100_000_000 );
1624
            $upload_timestamp = $parsed_timestamp;
1634
            $upload_timestamp = $parsed_timestamp;
1625
        }
1635
        }
(-)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 202-205 subtest "RecordsFromMarcPlugin" => sub { Link Here
202
        'Checked one field in second record' );
202
        'Checked one field in second record' );
203
};
203
};
204
204
205
subtest "add_biblio_fields to import record" => sub {
206
207
my $record = MARC::Record->new;
208
    plan tests => 4;
209
210
    $record->append_fields(
211
        MARC::Field->new( '100', '', '', a => 'My author' ),
212
        MARC::Field->new( '245', '', '', a => 'My title' ),
213
        MARC::Field->new( '001', '111' ),
214
    );
215
216
    my $import_batch = $builder->build(
217
        {
218
            source => 'ImportBatch',
219
        }
220
    );
221
222
    my $import_record_id = C4::ImportBatch::_create_import_record(
223
        $import_batch->{import_batch_id},
224
        1, $record, 'biblio', 'UTF-8',
225
    );
226
227
    my $import_biblios = GetImportBiblios($import_record_id);
228
    is(scalar(@$import_biblios), 0, 'No import_biblios yet');
229
230
    C4::ImportBatch::_add_biblio_fields($import_record_id, $record);
231
232
    $import_biblios = GetImportBiblios($import_record_id);
233
    is($import_biblios->[0]{title}, 'My title', 'Import biblio title');
234
    is($import_biblios->[0]{author}, 'My author', 'Import biblio author');
235
    is($import_biblios->[0]{control_number}, 111, 'Import biblio control number');
236
};
237
205
$schema->storage->txn_rollback;
238
$schema->storage->txn_rollback;
206
- 

Return to bug 18823