7 use Test::More tests => 14;
10 use_ok('C4::NewsChannels');
13 my $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
19 # Add LIB1, if it doesn't exist.
21 unless ( Koha::Libraries->find($addbra) ) {
22 $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
23 undef, ( $addbra, "$addbra branch" ) );
26 # Add CAT1, if it doesn't exist.
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") );
37 # Add a test user if not already present.
38 my $addbrwr = 'BRWR1';
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;
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) );
52 # Retrieve the njew borrower number.
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;
61 # Must have valid borrower number, or tests are meaningless.
62 ok ( defined $brwrnmbr );
65 my $rv = add_opac_new(); # intentionally bad
66 is( $rv, 0, 'Correctly failed on no parameter!' );
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 );
77 expirationdate => $expirationdate1,
78 timestamp => $timestamp1,
83 $rv = add_opac_new($href_entry1);
84 is( $rv, 1, 'Successfully added the first dummy news item!' );
86 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
87 ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
92 expirationdate => $expirationdate2,
93 timestamp => $timestamp2,
95 borrowernumber => $brwrnmbr,
98 $rv = add_opac_new($href_entry2);
99 is( $rv, 1, 'Successfully added the second dummy news item!' );
101 my ( $title3, $new3, $lang3, $number3 ) =
102 ( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
107 timestamp => $timestamp3,
109 borrowernumber => $brwrnmbr,
110 branchcode => 'LIB1',
112 $rv = add_opac_new($href_entry3);
113 is( $rv, 1, 'Successfully added the third dummy news item without expiration date!' );
115 # We need to determine the idnew in a non-MySQLism way.
116 # This should be good enough.
118 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
119 my ( $idnew1 ) = $dbh->selectrow_array( $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 );
125 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-02'; };
126 my ( $idnew3 ) = $dbh->selectrow_array( $query );
129 $rv = upd_opac_new(); # intentionally bad parmeters
130 is( $rv, 0, 'Correctly failed on no parameter!' );
132 $new2 = '<p>Update! There is no news!</p>';
133 $href_entry2->{content} = $new2;
134 $href_entry2->{idnew} = $idnew2;
135 $rv = upd_opac_new($href_entry2);
136 is( $rv, 1, 'Successfully updated second dummy news item!' );
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 } );
145 get_opac_new($idnew1),
150 expirationdate => $expirationdate1,
151 timestamp => $timestamp1,
153 borrowernumber => undef,
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
163 'got back expected news item via get_opac_new - ID 1'
166 # Test get_opac_new (single news item)
168 get_opac_new($idnew2),
173 expirationdate => $expirationdate2,
174 timestamp => $timestamp2,
176 borrowernumber => $brwrnmbr,
178 branchname => "$addbra branch",
179 branchcode => $addbra,
182 'got back expected news item via get_opac_new - ID 2'
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");
189 # Test get_opac_news (multiple news items)
190 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
192 # using >= 2, because someone may have LIB1 news already.
193 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
195 # Test GetNewsToDisplay
196 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
197 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
199 # Regression test 14248 -- make sure author_title, author_firstname, and
200 # author_surname exist.
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;
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;
226 ok($check==3,'Both with and without author data tested');