8 use Test::More tests => 11;
11 use_ok('C4::NewsChannels');
14 my $schema = Koha::Database->new->schema;
15 $schema->storage->txn_begin;
16 my $dbh = C4::Context->dbh;
18 # Add LIB1, if it doesn't exist.
20 unless ( Koha::Libraries->find($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 $dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) },
32 undef, ( $addcat, "$addcat description") );
36 # Add a test user if not already present.
37 my $addbrwr = 'BRWR1';
41 q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
42 my $sth = $dbh->prepare( $query );
43 $sth->execute( ($addbrwr, $addbra, $addcat) );
44 $brwrnmbr = $sth->fetchrow;
46 # Not found, let us insert it.
47 if ( not defined $brwrnmbr ) {
48 $dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) },
49 undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) );
51 # Retrieve the njew borrower number.
53 q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
54 my $sth = $dbh->prepare( $query );
55 $sth->execute( ($addbrwr, $addbra, $addcat) );
56 $brwrnmbr = $sth->fetchrow;
60 # Must have valid borrower number, or tests are meaningless.
61 ok ( defined $brwrnmbr );
64 my $rv = add_opac_new(); # intentionally bad
65 is( $rv, 0, 'Correctly failed on no parameter!' );
67 my $timestamp = '2000-01-01';
68 my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
69 my $timestamp3 = '2000-01-02';
70 my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
71 ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
76 expirationdate => $expirationdate1,
77 published_on=> $timestamp1,
82 $rv = add_opac_new($href_entry1);
83 is( $rv, 1, 'Successfully added the first dummy news item!' );
85 my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
86 ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
91 expirationdate => $expirationdate2,
92 published_on=> $timestamp2,
94 borrowernumber => $brwrnmbr,
97 $rv = add_opac_new($href_entry2);
98 is( $rv, 1, 'Successfully added the second dummy news item!' );
100 my ( $title3, $new3, $lang3, $number3 ) =
101 ( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
106 published_on=> $timestamp3,
108 borrowernumber => $brwrnmbr,
109 branchcode => 'LIB1',
111 $rv = add_opac_new($href_entry3);
112 is( $rv, 1, 'Successfully added the third dummy news item without expiration date!' );
114 # We need to determine the idnew in a non-MySQLism way.
115 # This should be good enough.
117 q{ SELECT idnew from opac_news WHERE published_on='2000-01-01' AND expirationdate='2999-12-30'; };
118 my ( $idnew1 ) = $dbh->selectrow_array( $query );
120 q{ SELECT idnew from opac_news WHERE published_on='2000-01-01' AND expirationdate='2999-12-31'; };
121 my ( $idnew2 ) = $dbh->selectrow_array( $query );
124 q{ SELECT idnew from opac_news WHERE published_on='2000-01-02'; };
125 my ( $idnew3 ) = $dbh->selectrow_array( $query );
128 $rv = upd_opac_new(); # intentionally bad parmeters
129 is( $rv, 0, 'Correctly failed on no parameter!' );
131 $new2 = '<p>Update! There is no news!</p>';
132 $href_entry2->{content} = $new2;
133 $href_entry2->{idnew} = $idnew2;
134 $rv = upd_opac_new($href_entry2);
135 is( $rv, 1, 'Successfully updated second dummy news item!' );
137 # Test get_opac_news (multiple news items)
138 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
140 # using >= 2, because someone may have LIB1 news already.
141 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
143 # Test GetNewsToDisplay
144 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
145 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
147 # Regression test 14248 -- make sure author_title, author_firstname, and
148 # author_surname exist.
150 subtest 'Regression tests on author title, firstname, and surname.', sub {
151 my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
152 my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
153 ok($opac_news_count>0,'Data exists for regression testing');
154 foreach my $news_item (@$opac_news) {
155 ok(exists $news_item->{author_title}, 'Author title exists');
156 ok(exists $news_item->{author_firstname},'Author first name exists');
157 ok(exists $news_item->{author_surname}, 'Author surname exists');
158 if ($news_item->{borrowernumber}) {
159 ok(defined $news_item->{author_title} ||
160 defined $news_item->{author_firstname} ||
161 defined $news_item->{author_surname}, 'Author data defined');
162 $check = $check | 2; # bitwise flag;
165 ok(!defined $news_item->{author_title},
166 'Author title undefined as expected');
167 ok(!defined $news_item->{author_firstname},
168 'Author first name undefined as expected');
169 ok(!defined $news_item->{author_surname},
170 'Author surname undefined as expected');
171 $check = $check | 1; # bitwise flag;
174 ok($check==3,'Both with and without author data tested');