From 05798ec1aaf38546da6153e435de02c2db14d6c9 Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Tue, 19 Mar 2019 10:35:23 +0000 Subject: [PATCH] Bug 22544: Move add_opac_item to Koha namespace Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- C4/NewsChannels.pm | 34 ------------------ t/db_dependent/NewsChannels.t | 66 +---------------------------------- tools/koha-news.pl | 22 ++++++------ 3 files changed, 11 insertions(+), 111 deletions(-) diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm index 7d1a91a1e1..2a50a2a28c 100644 --- a/C4/NewsChannels.pm +++ b/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); diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t index 3a3b66315a..20e8ae12e9 100755 --- a/t/db_dependent/NewsChannels.t +++ b/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', '

We have some exciting news!

', 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', '

We have some exciting news!

', 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', '

News without expiration date

', 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!' ); diff --git a/tools/koha-news.pl b/tools/koha-news.pl index ce41b5419d..e63030995f 100755 --- a/tools/koha-news.pl +++ b/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 { -- 2.39.5