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; |