Bug 9016: (follow-up) fix unit tests
[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 => 10;
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 # Add LIB1, if it doesn't exist.
19 my $addbra = 'LIB1';
20 if ( !GetBranchName($addbra) ) {
21     $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
22         undef, ( $addbra, "$addbra branch" ) );
23 }
24
25 # Test add_opac_new
26 my $rv = add_opac_new();    # intentionally bad
27 ok( $rv == 0, 'Correctly failed on no parameter!' );
28
29 my $timestamp = '2000-01-01';
30 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
31 my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
32   ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
33 my $href_entry1 = {
34     title          => $title1,
35     new            => $new1,
36     lang           => $lang1,
37     expirationdate => $expirationdate1,
38     timestamp      => $timestamp1,
39     number         => $number1,
40     branchcode     => 'LIB1',
41 };
42
43 $rv = add_opac_new($href_entry1);
44 ok( $rv == 1, 'Successfully added the first dummy news item!' );
45
46 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
47   ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
48 my $href_entry2 = {
49     title          => $title2,
50     new            => $new2,
51     lang           => $lang2,
52     expirationdate => $expirationdate2,
53     timestamp      => $timestamp2,
54     number         => $number2,
55     branchcode     => 'LIB1',
56 };
57 $rv = add_opac_new($href_entry2);
58 ok( $rv == 1, 'Successfully added the second dummy news item!' );
59
60 # We need to determine the idnew in a non-MySQLism way.
61 # This should be good enough.
62 my $query =
63 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
64 my $sth = $dbh->prepare($query);
65 $sth->execute();
66 my $idnew1 = $sth->fetchrow;
67 $query =
68 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
69 $sth = $dbh->prepare($query);
70 $sth->execute();
71 my $idnew2 = $sth->fetchrow;
72
73 # Test upd_opac_new
74 $rv = upd_opac_new();    # intentionally bad parmeters
75 ok( $rv == 0, 'Correctly failed on no parameter!' );
76
77 $new2                 = '<p>Update! There is no news!</p>';
78 $href_entry2->{new}   = $new2;
79 $href_entry2->{idnew} = $idnew2;
80 $rv                   = upd_opac_new($href_entry2);
81 ok( $rv == 1, 'Successfully updated second dummy news item!' );
82
83 # Test get_opac_new (single news item)
84 $timestamp1      = format_date($timestamp1);
85 $expirationdate1 = format_date($expirationdate1);
86 $timestamp2      = format_date($timestamp2);
87 $expirationdate2 = format_date($expirationdate2);
88
89 is_deeply(
90     get_opac_new($idnew1),
91     {
92         title          => $title1,
93         new            => $new1,
94         lang           => $lang1,
95         expirationdate => $expirationdate1,
96         timestamp      => $timestamp1,
97         number         => $number1,
98         idnew          => $idnew1,
99         branchname     => "$addbra branch",
100         branchcode     => $addbra,
101         # this represents $lang => 1 in the hash
102         # that's returned... which seems a little
103         # redundant given that there's a perfectly
104         # good 'lang' key in the hash
105         ''             => 1,
106     },
107     'got back expected news item via get_opac_new - ID 1'
108 );
109
110 # Test get_opac_new (single news item)
111 is_deeply(
112     get_opac_new($idnew2),
113     {  
114         title          => $title2,
115         new            => $new2,
116         lang           => $lang2,
117         expirationdate => $expirationdate2,
118         timestamp      => $timestamp2,
119         number         => $number2,
120         idnew          => $idnew2,
121         branchname     => "$addbra branch",
122         branchcode     => $addbra,
123         ''             => 1,
124     },
125     'got back expected news item via get_opac_new - ID 2'
126 );
127
128 # Test get_opac_news (multiple news items)
129 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
130
131 # using >= 2, because someone may have LIB1 news already.
132 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
133
134 # Test GetNewsToDisplay
135 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
136 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
137
138 $dbh->rollback;