From f0e5f11845338539d775d7b5fbf4cad295256add Mon Sep 17 00:00:00 2001
From: Mark Tompsett <mtompset@hotmail.com>
Date: Thu, 12 Dec 2013 23:19:18 -0500
Subject: [PATCH] Bug 7567 - Testing for C4/NewsChannel.pm

Testing was lacking, this tests every function call. As a
side effect, this moved the stub of a test file from
t/NewsChannels.t to t/db_dependent/NewsChannels.t, since the
table opac_news must exist.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>

Passes all 8 tests
---
 t/NewsChannels.t              | 14 -------
 t/db_dependent/NewsChannels.t | 92 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 14 deletions(-)
 delete mode 100755 t/NewsChannels.t
 create mode 100755 t/db_dependent/NewsChannels.t

diff --git a/t/NewsChannels.t b/t/NewsChannels.t
deleted file mode 100755
index bbcaab1..0000000
--- a/t/NewsChannels.t
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/usr/bin/perl
-#
-# This Koha test module is a stub!  
-# Add more tests here!!!
-
-use strict;
-use warnings;
-
-use Test::More tests => 1;
-
-BEGIN {
-        use_ok('C4::NewsChannels');
-}
-
diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t
new file mode 100755
index 0000000..261abd5
--- /dev/null
+++ b/t/db_dependent/NewsChannels.t
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+use C4::Dates qw(format_date);
+use C4::Branch qw(GetBranchName);
+use Test::More tests => 8;
+
+BEGIN {
+        use_ok('C4::NewsChannels');
+}
+
+my $dbh = C4::Context->dbh;
+
+# Start transaction
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+# Test add_opac_new
+my $timestamp = '2000-01-01';
+my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
+my ($title1,         $new1,
+    $lang1, $expirationdate1, $number1) =
+   ( 'News Title',  '<p>We have some exciting news!</p>',
+    '',    '2999-12-30',    1 );
+my $rv = add_opac_new( $title1, $new1, $lang1, $expirationdate1, $timestamp1, $number1 );
+ok($rv==1,"Successfully added the first dummy news item!");
+
+my ($title2,         $new2,
+    $lang2, $expirationdate2, $number2) =
+   ( 'News Title2', '<p>We have some exciting news!</p>',
+    '',    '2999-12-31',    1 );
+$rv = add_opac_new( $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
+ok($rv==1,"Successfully added the second dummy news item!");
+
+# We need to determine the idnew in a non-MySQLism way.
+# This should be good enough.
+my $sth = $dbh->prepare(q{
+  SELECT idnew from opac_news
+  WHERE timestamp='2000-01-01' AND
+        expirationdate='2999-12-30';
+                        });
+$sth->execute();
+my $idnew1 = $sth->fetchrow;
+$sth = $dbh->prepare(q{
+  SELECT idnew from opac_news
+  WHERE timestamp='2000-01-01' AND
+        expirationdate='2999-12-31';
+                      });
+$sth->execute();
+my $idnew2 = $sth->fetchrow;
+
+# Test upd_opac_new
+$new2 = '<p>Update! There is no news!</p>';
+$rv = upd_opac_new( $idnew2, $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
+ok($rv==1,"Successfully updated second dummy news item!");
+
+# Test get_opac_new (single news item)
+$timestamp1 = format_date( $timestamp1 );
+$expirationdate1 = format_date( $expirationdate1 );
+$timestamp2 = format_date( $timestamp2 );
+$expirationdate2 = format_date( $expirationdate2 );
+
+my $hashref_check = get_opac_new($idnew1);
+my $failure = 0;
+if ($hashref_check->{title}          ne $title1)          { $failure = 1; }
+if ($hashref_check->{new}            ne $new1)            { $failure = 1; }
+if ($hashref_check->{lang}           ne $lang1)           { $failure = 1; }
+if ($hashref_check->{expirationdate} ne $expirationdate1) { $failure = 1; }
+if ($hashref_check->{timestamp}      ne $timestamp1)      { $failure = 1; }
+if ($hashref_check->{number}         ne $number1)         { $failure = 1; }
+ok($failure==0,"Successfully tested get_opac_new id1!");
+
+# Test get_opac_new (single news item)
+$hashref_check = get_opac_new($idnew2);
+$failure = 0;
+if ($hashref_check->{title}          ne $title2)          { $failure = 1; }
+if ($hashref_check->{new}            ne $new2)            { $failure = 1; }
+if ($hashref_check->{lang}           ne $lang2)           { $failure = 1; }
+if ($hashref_check->{expirationdate} ne $expirationdate2) { $failure = 1; }
+if ($hashref_check->{timestamp}      ne $timestamp2)      { $failure = 1; }
+if ($hashref_check->{number}         ne $number2)         { $failure = 1; }
+ok($failure==0,"Successfully tested get_opac_new id2!");
+
+# Test get_opac_news (multiple news items)
+my ($opac_news_count, $arrayref_opac_news) = get_opac_news(0,'');
+ok($opac_news_count>=2,"Successfully tested get_opac_news!");
+
+# Test GetNewsToDisplay
+($opac_news_count, $arrayref_opac_news) = GetNewsToDisplay('');
+ok($opac_news_count>=2,"Successfully tested GetNewsToDisplay!");
+
+$dbh->rollback;
-- 
1.8.5.3