Bug 7567: add tests for C4::NewsChannel
[koha.git] / t / db_dependent / NewsChannels.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Dates qw(format_date);
5 use C4::Branch qw(GetBranchName);
6 use Test::More tests => 8;
7
8 BEGIN {
9         use_ok('C4::NewsChannels');
10 }
11
12 my $dbh = C4::Context->dbh;
13
14 # Start transaction
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
17
18 # Test add_opac_new
19 my $timestamp = '2000-01-01';
20 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
21 my ($title1,         $new1,
22     $lang1, $expirationdate1, $number1) =
23    ( 'News Title',  '<p>We have some exciting news!</p>',
24     '',    '2999-12-30',    1 );
25 my $rv = add_opac_new( $title1, $new1, $lang1, $expirationdate1, $timestamp1, $number1 );
26 ok($rv==1,"Successfully added the first dummy news item!");
27
28 my ($title2,         $new2,
29     $lang2, $expirationdate2, $number2) =
30    ( 'News Title2', '<p>We have some exciting news!</p>',
31     '',    '2999-12-31',    1 );
32 $rv = add_opac_new( $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
33 ok($rv==1,"Successfully added the second dummy news item!");
34
35 # We need to determine the idnew in a non-MySQLism way.
36 # This should be good enough.
37 my $sth = $dbh->prepare(q{
38   SELECT idnew from opac_news
39   WHERE timestamp='2000-01-01' AND
40         expirationdate='2999-12-30';
41                         });
42 $sth->execute();
43 my $idnew1 = $sth->fetchrow;
44 $sth = $dbh->prepare(q{
45   SELECT idnew from opac_news
46   WHERE timestamp='2000-01-01' AND
47         expirationdate='2999-12-31';
48                       });
49 $sth->execute();
50 my $idnew2 = $sth->fetchrow;
51
52 # Test upd_opac_new
53 $new2 = '<p>Update! There is no news!</p>';
54 $rv = upd_opac_new( $idnew2, $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
55 ok($rv==1,"Successfully updated second dummy news item!");
56
57 # Test get_opac_new (single news item)
58 $timestamp1 = format_date( $timestamp1 );
59 $expirationdate1 = format_date( $expirationdate1 );
60 $timestamp2 = format_date( $timestamp2 );
61 $expirationdate2 = format_date( $expirationdate2 );
62
63 my $hashref_check = get_opac_new($idnew1);
64 my $failure = 0;
65 if ($hashref_check->{title}          ne $title1)          { $failure = 1; }
66 if ($hashref_check->{new}            ne $new1)            { $failure = 1; }
67 if ($hashref_check->{lang}           ne $lang1)           { $failure = 1; }
68 if ($hashref_check->{expirationdate} ne $expirationdate1) { $failure = 1; }
69 if ($hashref_check->{timestamp}      ne $timestamp1)      { $failure = 1; }
70 if ($hashref_check->{number}         ne $number1)         { $failure = 1; }
71 ok($failure==0,"Successfully tested get_opac_new id1!");
72
73 # Test get_opac_new (single news item)
74 $hashref_check = get_opac_new($idnew2);
75 $failure = 0;
76 if ($hashref_check->{title}          ne $title2)          { $failure = 1; }
77 if ($hashref_check->{new}            ne $new2)            { $failure = 1; }
78 if ($hashref_check->{lang}           ne $lang2)           { $failure = 1; }
79 if ($hashref_check->{expirationdate} ne $expirationdate2) { $failure = 1; }
80 if ($hashref_check->{timestamp}      ne $timestamp2)      { $failure = 1; }
81 if ($hashref_check->{number}         ne $number2)         { $failure = 1; }
82 ok($failure==0,"Successfully tested get_opac_new id2!");
83
84 # Test get_opac_news (multiple news items)
85 my ($opac_news_count, $arrayref_opac_news) = get_opac_news(0,'');
86 ok($opac_news_count>=2,"Successfully tested get_opac_news!");
87
88 # Test GetNewsToDisplay
89 ($opac_news_count, $arrayref_opac_news) = GetNewsToDisplay('');
90 ok($opac_news_count>=2,"Successfully tested GetNewsToDisplay!");
91
92 $dbh->rollback;