4 use C4::Branch qw(GetBranchName);
7 use Test::More tests => 12;
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 ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
73 ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
78 expirationdate => $expirationdate1,
79 timestamp => $timestamp1,
84 $rv = add_opac_new($href_entry1);
85 ok( $rv == 1, 'Successfully added the first dummy news item!' );
87 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
88 ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
93 expirationdate => $expirationdate2,
94 timestamp => $timestamp2,
96 borrowernumber => $brwrnmbr,
99 $rv = add_opac_new($href_entry2);
100 ok( $rv == 1, 'Successfully added the second dummy news item!' );
102 # We need to determine the idnew in a non-MySQLism way.
103 # This should be good enough.
105 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
106 my $sth = $dbh->prepare($query);
108 my $idnew1 = $sth->fetchrow;
110 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
111 $sth = $dbh->prepare($query);
113 my $idnew2 = $sth->fetchrow;
116 $rv = upd_opac_new(); # intentionally bad parmeters
117 ok( $rv == 0, 'Correctly failed on no parameter!' );
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!' );
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 } );
132 get_opac_new($idnew1),
137 expirationdate => $expirationdate1,
138 timestamp => $timestamp1,
140 borrowernumber => undef,
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
150 'got back expected news item via get_opac_new - ID 1'
153 # Test get_opac_new (single news item)
155 get_opac_new($idnew2),
160 expirationdate => $expirationdate2,
161 timestamp => $timestamp2,
163 borrowernumber => $brwrnmbr,
165 branchname => "$addbra branch",
166 branchcode => $addbra,
169 'got back expected news item via get_opac_new - ID 2'
172 # Test get_opac_news (multiple news items)
173 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
175 # using >= 2, because someone may have LIB1 news already.
176 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
178 # Test GetNewsToDisplay
179 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
180 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
182 # Regression test 14248 -- make sure author_title, author_firstname, and
183 # author_surname exist.
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;
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;
209 ok($check==3,'Both with and without author data tested');