Bug 16550: Add test to NewsChannels.t
[koha.git] / t / db_dependent / NewsChannels.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Branch qw(GetBranchName);
5 use Koha::DateUtils;
6
7 use Test::More tests => 14;
8
9 BEGIN {
10     use_ok('C4::NewsChannels');
11 }
12
13 my $dbh = C4::Context->dbh;
14
15 # Start transaction
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
18
19 # Add LIB1, if it doesn't exist.
20 my $addbra = 'LIB1';
21 if ( !GetBranchName($addbra) ) {
22     $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
23         undef, ( $addbra, "$addbra branch" ) );
24 }
25
26 # Add CAT1, if it doesn't exist.
27 my $addcat = 'CAT1';
28 {
29     my $sth = $dbh->prepare( q{ SELECT categorycode FROM categories WHERE categorycode = ? } );
30     $sth->execute ( $addcat );
31     if ( not defined $sth->fetchrow () ) {
32         diag("Category $addcat not found, inserting");
33         $dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) },
34             undef, ( $addcat, "$addcat description") );
35     }
36 }
37
38 # Add a test user if not already present.
39 my $addbrwr = 'BRWR1';
40 my $brwrnmbr;
41 {
42     my $query =
43         q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
44     my $sth = $dbh->prepare( $query );
45     $sth->execute( ($addbrwr, $addbra, $addcat) );
46     $brwrnmbr = $sth->fetchrow;
47
48     # Not found, let us insert it.
49     if ( not defined $brwrnmbr ) {
50         diag("Borrower $addbrwr not found, inserting");
51         $dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) },
52             undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) );
53
54         # Retrieve the njew borrower number.
55         $query =
56             q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
57         my $sth = $dbh->prepare( $query );
58         $sth->execute( ($addbrwr, $addbra, $addcat) );
59         $brwrnmbr = $sth->fetchrow;
60     }
61 }
62
63 # Must have valid borrower number, or tests are meaningless.
64 ok ( defined $brwrnmbr );
65
66 # Test add_opac_new
67 my $rv = add_opac_new();    # intentionally bad
68 ok( $rv == 0, 'Correctly failed on no parameter!' );
69
70 my $timestamp = '2000-01-01';
71 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
72 my $timestamp3 = '2000-01-02';
73 my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
74   ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
75 my $href_entry1 = {
76     title          => $title1,
77     new            => $new1,
78     lang           => $lang1,
79     expirationdate => $expirationdate1,
80     timestamp      => $timestamp1,
81     number         => $number1,
82     branchcode     => 'LIB1',
83 };
84
85 $rv = add_opac_new($href_entry1);
86 ok( $rv == 1, 'Successfully added the first dummy news item!' );
87
88 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
89   ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
90 my $href_entry2 = {
91     title          => $title2,
92     new            => $new2,
93     lang           => $lang2,
94     expirationdate => $expirationdate2,
95     timestamp      => $timestamp2,
96     number         => $number2,
97     borrowernumber => $brwrnmbr,
98     branchcode     => 'LIB1',
99 };
100 $rv = add_opac_new($href_entry2);
101 ok( $rv == 1, 'Successfully added the second dummy news item!' );
102
103 my ( $title3, $new3, $lang3, $number3 ) =
104   ( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
105 my $href_entry3 = {
106     title          => $title3,
107     new            => $new3,
108     lang           => $lang3,
109     timestamp      => $timestamp3,
110     number         => $number3,
111     borrowernumber => $brwrnmbr,
112     branchcode     => 'LIB1',
113 };
114 $rv = add_opac_new($href_entry3);
115 ok( $rv == 1, 'Successfully added the third dummy news item without expiration date!' );
116
117 # We need to determine the idnew in a non-MySQLism way.
118 # This should be good enough.
119 my $query =
120 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
121 my $sth = $dbh->prepare($query);
122 $sth->execute();
123 my $idnew1 = $sth->fetchrow;
124 $query =
125 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
126 $sth = $dbh->prepare($query);
127 $sth->execute();
128 my $idnew2 = $sth->fetchrow;
129
130 $query =
131 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-02'; };
132 $sth = $dbh->prepare($query);
133 $sth->execute();
134 my $idnew3 = $sth->fetchrow;
135
136 # Test upd_opac_new
137 $rv = upd_opac_new();    # intentionally bad parmeters
138 ok( $rv == 0, 'Correctly failed on no parameter!' );
139
140 $new2                 = '<p>Update! There is no news!</p>';
141 $href_entry2->{new}   = $new2;
142 $href_entry2->{idnew} = $idnew2;
143 $rv                   = upd_opac_new($href_entry2);
144 ok( $rv == 1, 'Successfully updated second dummy news item!' );
145
146 # Test get_opac_new (single news item)
147 $timestamp1      = output_pref( { dt => dt_from_string( $timestamp1 ), dateonly => 1 } );
148 $expirationdate1 = output_pref( { dt => dt_from_string( $expirationdate1 ), dateonly => 1 } );
149 $timestamp2      = output_pref( { dt => dt_from_string( $timestamp2 ), dateonly => 1 } );
150 $expirationdate2 = output_pref( { dt => dt_from_string( $expirationdate2) , dateonly => 1 } );
151
152 is_deeply(
153     get_opac_new($idnew1),
154     {
155         title          => $title1,
156         new            => $new1,
157         lang           => $lang1,
158         expirationdate => $expirationdate1,
159         timestamp      => $timestamp1,
160         number         => $number1,
161         borrowernumber => undef,
162         idnew          => $idnew1,
163         branchname     => "$addbra branch",
164         branchcode     => $addbra,
165         # this represents $lang => 1 in the hash
166         # that's returned... which seems a little
167         # redundant given that there's a perfectly
168         # good 'lang' key in the hash
169         ''             => 1,
170     },
171     'got back expected news item via get_opac_new - ID 1'
172 );
173
174 # Test get_opac_new (single news item)
175 is_deeply(
176     get_opac_new($idnew2),
177     {  
178         title          => $title2,
179         new            => $new2,
180         lang           => $lang2,
181         expirationdate => $expirationdate2,
182         timestamp      => $timestamp2,
183         number         => $number2,
184         borrowernumber => $brwrnmbr,
185         idnew          => $idnew2,
186         branchname     => "$addbra branch",
187         branchcode     => $addbra,
188         ''             => 1,
189     },
190     'got back expected news item via get_opac_new - ID 2'
191 );
192
193 # Test get_opac_new (single news item without expiration date)
194 my $news3 = get_opac_new($idnew3);
195 ok (! $news3->{ expirationdate }, "Expiration date should be empty");
196
197 # Test get_opac_news (multiple news items)
198 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
199
200 # using >= 2, because someone may have LIB1 news already.
201 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
202
203 # Test GetNewsToDisplay
204 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
205 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
206
207 # Regression test 14248 -- make sure author_title, author_firstname, and
208 # author_surname exist.
209
210 subtest 'Regression tests on author title, firstname, and surname.', sub {
211     my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
212     my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
213     ok($opac_news_count>0,'Data exists for regression testing');
214     foreach my $news_item (@$opac_news) {
215         ok(exists $news_item->{author_title},    'Author title exists');
216         ok(exists $news_item->{author_firstname},'Author first name exists');
217         ok(exists $news_item->{author_surname},  'Author surname exists');
218         if ($news_item->{borrowernumber}) {
219             ok(defined $news_item->{author_title} ||
220                defined $news_item->{author_firstname} ||
221                defined $news_item->{author_surname},  'Author data defined');
222             $check = $check | 2; # bitwise flag;
223         }
224         else {
225             ok(!defined $news_item->{author_title},
226                'Author title undefined as expected');
227             ok(!defined $news_item->{author_firstname},
228                'Author first name undefined as expected');
229             ok(!defined $news_item->{author_surname},
230                'Author surname undefined as expected');
231             $check = $check | 1; # bitwise flag;
232         }
233     }
234     ok($check==3,'Both with and without author data tested');
235     done_testing();
236 };
237
238 $dbh->rollback;