Browse Source

Bug 22544: Move add_opac_item to Koha namespace

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
master
Josef Moravec 3 years ago
committed by Jonathan Druart
parent
commit
05798ec1aa
  1. 34
      C4/NewsChannels.pm
  2. 66
      t/db_dependent/NewsChannels.t
  3. 22
      tools/koha-news.pl

34
C4/NewsChannels.pm

@ -30,7 +30,6 @@ BEGIN {
@ISA = qw(Exporter);
@EXPORT = qw(
&GetNewsToDisplay
&add_opac_new
);
}
@ -46,39 +45,6 @@ This module provides the functions needed to mange OPAC and intranet news.
=cut
=head2 add_opac_new
$retval = add_opac_new($hashref);
$hashref should contains all the fields found in opac_news,
except idnew. The idnew field is auto-generated.
=cut
sub add_opac_new {
my ($href_entry) = @_;
my $retval = 0;
if ($href_entry) {
$href_entry->{number} = 0 if $href_entry->{number} !~ /^\d+$/;
my @fields = keys %{$href_entry};
my @values = values %{$href_entry};
my $field_string = join ',', @fields;
$field_string = $field_string // q{};
my $values_string = join(',', map { '?' } @fields);
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
$sth->execute(@values);
$retval = 1;
#Log new news entry
if (C4::Context->preference("NewsLog")) {
logaction('NEWS', 'ADD' , undef, $href_entry->{lang} . ' | ' . $href_entry->{content});
}
}
return $retval;
}
=head2 GetNewsToDisplay
$news = &GetNewsToDisplay($lang,$branch);

66
t/db_dependent/NewsChannels.t

@ -6,7 +6,7 @@ use Koha::DateUtils;
use Koha::Libraries;
use Koha::News;
use Test::More tests => 7;
use Test::More tests => 4;
BEGIN {
use_ok('C4::NewsChannels');
@ -61,70 +61,6 @@ my $brwrnmbr;
# Must have valid borrower number, or tests are meaningless.
ok ( defined $brwrnmbr );
# Test add_opac_new
my $rv = add_opac_new(); # intentionally bad
is( $rv, 0, 'Correctly failed on no parameter!' );
my $timestamp = '2000-01-01';
my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
my $timestamp3 = '2000-01-02';
my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
my $href_entry1 = {
title => $title1,
content => $new1,
lang => $lang1,
expirationdate => $expirationdate1,
published_on=> $timestamp1,
number => $number1,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry1);
is( $rv, 1, 'Successfully added the first dummy news item!' );
my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
my $href_entry2 = {
title => $title2,
content => $new2,
lang => $lang2,
expirationdate => $expirationdate2,
published_on=> $timestamp2,
number => $number2,
borrowernumber => $brwrnmbr,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry2);
is( $rv, 1, 'Successfully added the second dummy news item!' );
my ( $title3, $new3, $lang3, $number3 ) =
( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
my $href_entry3 = {
title => $title3,
content => $new3,
lang => $lang3,
published_on=> $timestamp3,
number => $number3,
borrowernumber => $brwrnmbr,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry3);
is( $rv, 1, 'Successfully added the third dummy news item without expiration date!' );
# We need to determine the idnew in a non-MySQLism way.
# This should be good enough.
my $query =
q{ SELECT idnew from opac_news WHERE published_on='2000-01-01' AND expirationdate='2999-12-30'; };
my ( $idnew1 ) = $dbh->selectrow_array( $query );
$query =
q{ SELECT idnew from opac_news WHERE published_on='2000-01-01' AND expirationdate='2999-12-31'; };
my ( $idnew2 ) = $dbh->selectrow_array( $query );
$query =
q{ SELECT idnew from opac_news WHERE published_on='2000-01-02'; };
my ( $idnew3 ) = $dbh->selectrow_array( $query );
# Test GetNewsToDisplay
my ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );

22
tools/koha-news.pl

@ -108,18 +108,16 @@ if ( $op eq 'add_form' ) {
}
elsif ( $op eq 'add' ) {
if ($title) {
add_opac_new(
{
title => $title,
content => $content,
lang => $lang,
expirationdate => $expirationdate,
published_on=> $published_on,
number => $number,
branchcode => $branchcode,
borrowernumber => $borrowernumber,
}
);
my $new = Koha::NewsItem->new({
title => $title,
content => $content,
lang => $lang,
expirationdate => $expirationdate,
timestamp => $timestamp,
number => $number,
branchcode => $branchcode,
borrowernumber => $borrowernumber,
})->store;
print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
}
else {

Loading…
Cancel
Save