4 use C4::Dates qw(format_date);
5 use C4::Branch qw(GetBranchName);
6 use Test::More tests => 10;
17 use_ok('C4::NewsChannels');
20 my $dbh = C4::Context->dbh;
23 $dbh->{AutoCommit} = 0;
24 $dbh->{RaiseError} = 1;
26 # Add LIB1, if it doesn't exist.
28 if ( !GetBranchName($addbra) ) {
29 $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
30 undef, ( $addbra, "$addbra branch" ) );
34 my $rv = add_opac_new(); # intentionally bad
35 ok( $rv == 0, 'Correctly failed on no parameter!' );
37 my $timestamp = '2000-01-01';
38 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
39 my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
40 ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
45 expirationdate => $expirationdate1,
46 timestamp => $timestamp1,
51 $rv = add_opac_new($href_entry1);
52 ok( $rv == 1, 'Successfully added the first dummy news item!' );
54 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
55 ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
60 expirationdate => $expirationdate2,
61 timestamp => $timestamp2,
65 $rv = add_opac_new($href_entry2);
66 ok( $rv == 1, 'Successfully added the second dummy news item!' );
68 # We need to determine the idnew in a non-MySQLism way.
69 # This should be good enough.
71 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
72 my $sth = $dbh->prepare($query);
74 my $idnew1 = $sth->fetchrow;
76 q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
77 $sth = $dbh->prepare($query);
79 my $idnew2 = $sth->fetchrow;
82 $rv = upd_opac_new(); # intentionally bad parmeters
83 ok( $rv == 0, 'Correctly failed on no parameter!' );
85 $new2 = '<p>Update! There is no news!</p>';
86 $href_entry2->{new} = $new2;
87 $href_entry2->{idnew} = $idnew2;
88 $rv = upd_opac_new($href_entry2);
89 ok( $rv == 1, 'Successfully updated second dummy news item!' );
91 # Test get_opac_new (single news item)
92 $timestamp1 = format_date($timestamp1);
93 $expirationdate1 = format_date($expirationdate1);
94 $timestamp2 = format_date($timestamp2);
95 $expirationdate2 = format_date($expirationdate2);
97 my $hashref_check = get_opac_new($idnew1);
99 if ( $hashref_check->{title} ne $title1 ) { $failure = $F1; }
100 if ( $hashref_check->{new} ne $new1 ) { $failure = $F2; }
101 if ( $hashref_check->{lang} ne $lang1 ) { $failure = $F3; }
102 if ( $hashref_check->{expirationdate} ne $expirationdate1 ) { $failure = $F4; }
103 if ( $hashref_check->{timestamp} ne $timestamp1 ) { $failure = $F5; }
104 if ( $hashref_check->{number} ne $number1 ) { $failure = $F6; }
105 ok( $failure == 0, "Successfully tested get_opac_new id1 ($failure)!" );
107 # Test get_opac_new (single news item)
108 $hashref_check = get_opac_new($idnew2);
110 if ( $hashref_check->{title} ne $title2 ) { $failure = $F1; }
111 if ( $hashref_check->{new} ne $new2 ) { $failure = $F2; }
112 if ( $hashref_check->{lang} ne $lang2 ) { $failure = $F3; }
113 if ( $hashref_check->{expirationdate} ne $expirationdate2 ) { $failure = $F4; }
114 if ( $hashref_check->{timestamp} ne $timestamp2 ) { $failure = $F5; }
115 if ( $hashref_check->{number} ne $number2 ) { $failure = $F6; }
116 ok( $failure == 0, "Successfully tested get_opac_new id2 ($failure)!" );
118 # Test get_opac_news (multiple news items)
119 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
121 # using >= 2, because someone may have LIB1 news already.
122 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
124 # Test GetNewsToDisplay
125 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
126 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );