4 use C4::Branch qw(GetBranchName);
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 if ( !GetBranchName($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 diag("Category $addcat not found, inserting");
33 $dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) },
34 undef, ( $addcat, "$addcat description") );
38 # Add a test user if not already present.
39 my $addbrwr = 'BRWR1';
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;
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) );
54 # Retrieve the njew borrower number.
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;
63 # Must have valid borrower number, or tests are meaningless.
64 ok ( defined $brwrnmbr );
67 my $rv = add_opac_new(); # intentionally bad
68 ok( $rv == 0, 'Correctly failed on no parameter!' );
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 );
79 expirationdate => $expirationdate1,
80 timestamp => $timestamp1,
85 $rv = add_opac_new($href_entry1);
86 ok( $rv == 1, 'Successfully added the first dummy news item!' );
88 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
89 ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
94 expirationdate => $expirationdate2,
95 timestamp => $timestamp2,
97 borrowernumber => $brwrnmbr,
100 $rv = add_opac_new($href_entry2);
101 ok( $rv == 1, 'Successfully added the second dummy news item!' );
103 my ( $title3, $new3, $lang3, $number3 ) =
104 ( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
109 timestamp => $timestamp3,
111 borrowernumber => $brwrnmbr,
112 branchcode => 'LIB1',
114 $rv = add_opac_new($href_entry3);
115 ok( $rv == 1, 'Successfully added the third dummy news item without expiration date!' );
117 # We need to determine the idnew in a non-MySQLism way.
118 # This should be good enough.
120 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
121 my $sth = $dbh->prepare($query);
123 my $idnew1 = $sth->fetchrow;
125 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
126 $sth = $dbh->prepare($query);
128 my $idnew2 = $sth->fetchrow;
131 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-02'; };
132 $sth = $dbh->prepare($query);
134 my $idnew3 = $sth->fetchrow;
137 $rv = upd_opac_new(); # intentionally bad parmeters
138 ok( $rv == 0, 'Correctly failed on no parameter!' );
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!' );
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 } );
153 get_opac_new($idnew1),
158 expirationdate => $expirationdate1,
159 timestamp => $timestamp1,
161 borrowernumber => undef,
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
171 'got back expected news item via get_opac_new - ID 1'
174 # Test get_opac_new (single news item)
176 get_opac_new($idnew2),
181 expirationdate => $expirationdate2,
182 timestamp => $timestamp2,
184 borrowernumber => $brwrnmbr,
186 branchname => "$addbra branch",
187 branchcode => $addbra,
190 'got back expected news item via get_opac_new - ID 2'
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");
197 # Test get_opac_news (multiple news items)
198 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
200 # using >= 2, because someone may have LIB1 news already.
201 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
203 # Test GetNewsToDisplay
204 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
205 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
207 # Regression test 14248 -- make sure author_title, author_firstname, and
208 # author_surname exist.
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;
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;
234 ok($check==3,'Both with and without author data tested');