Bug 15947: move SIPILS.t to DB-dependent test directory
[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 => 12;
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 ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
73   ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
74 my $href_entry1 = {
75     title          => $title1,
76     new            => $new1,
77     lang           => $lang1,
78     expirationdate => $expirationdate1,
79     timestamp      => $timestamp1,
80     number         => $number1,
81     branchcode     => 'LIB1',
82 };
83
84 $rv = add_opac_new($href_entry1);
85 ok( $rv == 1, 'Successfully added the first dummy news item!' );
86
87 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
88   ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
89 my $href_entry2 = {
90     title          => $title2,
91     new            => $new2,
92     lang           => $lang2,
93     expirationdate => $expirationdate2,
94     timestamp      => $timestamp2,
95     number         => $number2,
96     borrowernumber => $brwrnmbr,
97     branchcode     => 'LIB1',
98 };
99 $rv = add_opac_new($href_entry2);
100 ok( $rv == 1, 'Successfully added the second dummy news item!' );
101
102 # We need to determine the idnew in a non-MySQLism way.
103 # This should be good enough.
104 my $query =
105 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
106 my $sth = $dbh->prepare($query);
107 $sth->execute();
108 my $idnew1 = $sth->fetchrow;
109 $query =
110 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
111 $sth = $dbh->prepare($query);
112 $sth->execute();
113 my $idnew2 = $sth->fetchrow;
114
115 # Test upd_opac_new
116 $rv = upd_opac_new();    # intentionally bad parmeters
117 ok( $rv == 0, 'Correctly failed on no parameter!' );
118
119 $new2                 = '<p>Update! There is no news!</p>';
120 $href_entry2->{new}   = $new2;
121 $href_entry2->{idnew} = $idnew2;
122 $rv                   = upd_opac_new($href_entry2);
123 ok( $rv == 1, 'Successfully updated second dummy news item!' );
124
125 # Test get_opac_new (single news item)
126 $timestamp1      = output_pref( { dt => dt_from_string( $timestamp1 ), dateonly => 1 } );
127 $expirationdate1 = output_pref( { dt => dt_from_string( $expirationdate1 ), dateonly => 1 } );
128 $timestamp2      = output_pref( { dt => dt_from_string( $timestamp2 ), dateonly => 1 } );
129 $expirationdate2 = output_pref( { dt => dt_from_string( $expirationdate2) , dateonly => 1 } );
130
131 is_deeply(
132     get_opac_new($idnew1),
133     {
134         title          => $title1,
135         new            => $new1,
136         lang           => $lang1,
137         expirationdate => $expirationdate1,
138         timestamp      => $timestamp1,
139         number         => $number1,
140         borrowernumber => undef,
141         idnew          => $idnew1,
142         branchname     => "$addbra branch",
143         branchcode     => $addbra,
144         # this represents $lang => 1 in the hash
145         # that's returned... which seems a little
146         # redundant given that there's a perfectly
147         # good 'lang' key in the hash
148         ''             => 1,
149     },
150     'got back expected news item via get_opac_new - ID 1'
151 );
152
153 # Test get_opac_new (single news item)
154 is_deeply(
155     get_opac_new($idnew2),
156     {  
157         title          => $title2,
158         new            => $new2,
159         lang           => $lang2,
160         expirationdate => $expirationdate2,
161         timestamp      => $timestamp2,
162         number         => $number2,
163         borrowernumber => $brwrnmbr,
164         idnew          => $idnew2,
165         branchname     => "$addbra branch",
166         branchcode     => $addbra,
167         ''             => 1,
168     },
169     'got back expected news item via get_opac_new - ID 2'
170 );
171
172 # Test get_opac_news (multiple news items)
173 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
174
175 # using >= 2, because someone may have LIB1 news already.
176 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
177
178 # Test GetNewsToDisplay
179 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
180 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
181
182 # Regression test 14248 -- make sure author_title, author_firstname, and
183 # author_surname exist.
184
185 subtest 'Regression tests on author title, firstname, and surname.', sub {
186     my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
187     my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
188     ok($opac_news_count>0,'Data exists for regression testing');
189     foreach my $news_item (@$opac_news) {
190         ok(exists $news_item->{author_title},    'Author title exists');
191         ok(exists $news_item->{author_firstname},'Author first name exists');
192         ok(exists $news_item->{author_surname},  'Author surname exists');
193         if ($news_item->{borrowernumber}) {
194             ok(defined $news_item->{author_title} ||
195                defined $news_item->{author_firstname} ||
196                defined $news_item->{author_surname},  'Author data defined');
197             $check = $check | 2; # bitwise flag;
198         }
199         else {
200             ok(!defined $news_item->{author_title},
201                'Author title undefined as expected');
202             ok(!defined $news_item->{author_firstname},
203                'Author first name undefined as expected');
204             ok(!defined $news_item->{author_surname},
205                'Author surname undefined as expected');
206             $check = $check | 1; # bitwise flag;
207         }
208     }
209     ok($check==3,'Both with and without author data tested');
210     done_testing();
211 };
212
213 $dbh->rollback;