4 use C4::Dates qw(format_date);
5 use C4::Branch qw(GetBranchName);
6 use Test::More tests => 11;
9 use_ok('C4::NewsChannels');
12 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
18 # Add LIB1, if it doesn't exist.
20 if ( !GetBranchName($addbra) ) {
21 $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
22 undef, ( $addbra, "$addbra branch" ) );
25 # Add CAT1, if it doesn't exist.
28 my $sth = $dbh->prepare( q{ SELECT categorycode FROM categories WHERE categorycode = ? } );
29 $sth->execute ( $addcat );
30 if ( not defined $sth->fetchrow () ) {
31 diag("Category $addcat not found, inserting");
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 diag("Borrower $addbrwr not found, inserting");
50 $dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) },
51 undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) );
53 # Retrieve the njew borrower number.
55 q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
56 my $sth = $dbh->prepare( $query );
57 $sth->execute( ($addbrwr, $addbra, $addcat) );
58 $brwrnmbr = $sth->fetchrow;
62 # Must have valid borrower number, or tests are meaningless.
63 ok ( defined $brwrnmbr );
66 my $rv = add_opac_new(); # intentionally bad
67 ok( $rv == 0, 'Correctly failed on no parameter!' );
69 my $timestamp = '2000-01-01';
70 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
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 ok( $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 ok( $rv == 1, 'Successfully added the second dummy news item!' );
101 # We need to determine the idnew in a non-MySQLism way.
102 # This should be good enough.
104 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
105 my $sth = $dbh->prepare($query);
107 my $idnew1 = $sth->fetchrow;
109 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
110 $sth = $dbh->prepare($query);
112 my $idnew2 = $sth->fetchrow;
115 $rv = upd_opac_new(); # intentionally bad parmeters
116 ok( $rv == 0, 'Correctly failed on no parameter!' );
118 $new2 = '<p>Update! There is no news!</p>';
119 $href_entry2->{new} = $new2;
120 $href_entry2->{idnew} = $idnew2;
121 $rv = upd_opac_new($href_entry2);
122 ok( $rv == 1, 'Successfully updated second dummy news item!' );
124 # Test get_opac_new (single news item)
125 $timestamp1 = format_date($timestamp1);
126 $expirationdate1 = format_date($expirationdate1);
127 $timestamp2 = format_date($timestamp2);
128 $expirationdate2 = format_date($expirationdate2);
131 get_opac_new($idnew1),
136 expirationdate => $expirationdate1,
137 timestamp => $timestamp1,
139 borrowernumber => undef,
141 branchname => "$addbra branch",
142 branchcode => $addbra,
143 # this represents $lang => 1 in the hash
144 # that's returned... which seems a little
145 # redundant given that there's a perfectly
146 # good 'lang' key in the hash
149 'got back expected news item via get_opac_new - ID 1'
152 # Test get_opac_new (single news item)
154 get_opac_new($idnew2),
159 expirationdate => $expirationdate2,
160 timestamp => $timestamp2,
162 borrowernumber => $brwrnmbr,
164 branchname => "$addbra branch",
165 branchcode => $addbra,
168 'got back expected news item via get_opac_new - ID 2'
171 # Test get_opac_news (multiple news items)
172 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
174 # using >= 2, because someone may have LIB1 news already.
175 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
177 # Test GetNewsToDisplay
178 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
179 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );