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