Browse Source

Bug 30717: (QA follow-up) Move to module

We will probably use this a bit more :)
Let's put it in a module (with a trivial test).

Test plan:
Repeat item edit.
Run  t/DateUtils.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
master
Marcel de Rooy 6 days ago
committed by Fridolin Somers
parent
commit
e0814f2cc0
  1. 23
      Koha/DateUtils.pm
  2. 10
      cataloguing/value_builder/dateaccessioned.pl
  3. 14
      t/DateUtils.t

23
Koha/DateUtils.pm

@ -30,6 +30,7 @@ BEGIN {
dt_from_string
output_pref
format_sqldatetime
flatpickr_date_format
);
}
@ -376,4 +377,26 @@ sub format_sqldatetime {
return q{};
}
=head2 flatpickr_date_format
$date_format = flatpickr_date_format( $koha_date_format );
Converts Koha's date format to Flatpickr's. E.g. 'us' returns 'm/d/Y'.
If no argument is given, the dateformat preference is assumed.
Returns undef if format is unknown.
=cut
sub flatpickr_date_format {
my $arg = shift // C4::Context->preference('dateformat');
return {
us => 'm/d/Y',
metric => 'd/m/Y',
dmydot => 'd.m.Y',
iso => 'Y-m-d',
}->{$arg};
}
1;

10
cataloguing/value_builder/dateaccessioned.pl

@ -21,20 +21,14 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::DateUtils qw( dt_from_string output_pref );
use Koha::DateUtils qw( dt_from_string output_pref flatpickr_date_format );
my $builder = sub {
my ( $params ) = @_;
my $function_name = $params->{id};
my $date = output_pref({ dt => dt_from_string, dateonly => 1 });
my $dateformat_pref = C4::Context->preference('dateformat');
my $dateformat =
$dateformat_pref eq 'us' ? 'm/d/Y'
: $dateformat_pref eq 'metric' ? 'd/m/Y'
: $dateformat_pref eq 'dmydot' ? 'd.m.Y'
: 'Y-m-d';
my $dateformat = flatpickr_date_format();
my $res = <<END_OF_JS;
<script>

14
t/DateUtils.t

@ -4,14 +4,14 @@ use DateTime::TimeZone;
use C4::Context;
use Test::More tests => 82;
use Test::More tests => 83;
use Test::MockModule;
use Test::Warn;
use Time::HiRes qw/ gettimeofday /;
use Try::Tiny;
use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime );
use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime flatpickr_date_format );
use t::lib::Mocks;
@ -389,4 +389,14 @@ try {
is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' );
};
subtest 'flatpickr_date_format' => sub {
plan tests => 4;
t::lib::Mocks::mock_preference('dateformat', 'iso');
is( flatpickr_date_format(), 'Y-m-d', 'check fallback to pref' );
is( flatpickr_date_format(q{}), undef, 'empty string' );
is( flatpickr_date_format('metric'), 'd/m/Y', 'check valid arg' );
is( flatpickr_date_format('zz'), undef, 'wrong arg' );
};
# End of tests

Loading…
Cancel
Save