Bug 7567: add tests for C4::NewsChannel
authorMark Tompsett <mtompset@hotmail.com>
Fri, 13 Dec 2013 04:19:18 +0000 (23:19 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Mon, 7 Apr 2014 18:02:15 +0000 (18:02 +0000)
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

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
t/NewsChannels.t [deleted file]
t/db_dependent/NewsChannels.t [new file with mode: 0755]

diff --git a/t/NewsChannels.t b/t/NewsChannels.t
deleted file mode 100755 (executable)
index bbcaab1..0000000
+++ /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 (executable)
index 0000000..261abd5
--- /dev/null
@@ -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;