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