Bug 24786: Update borroaccount to use session register
[koha.git] / t / db_dependent / NewsChannels.t
blob9eb1489f3d7fdf739809a75d71f3305ceccd0f0f
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Koha::Database;
5 use Koha::DateUtils;
6 use Koha::Libraries;
8 use Test::More tests => 14;
10 BEGIN {
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.
19 my $addbra = 'LIB1';
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.
26 my $addcat = 'CAT1';
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';
38 my $brwrnmbr;
40 my $query =
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.
52 $query =
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 );
63 # Test add_opac_new
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 );
72 my $href_entry1 = {
73 title => $title1,
74 content => $new1,
75 lang => $lang1,
76 expirationdate => $expirationdate1,
77 published_on=> $timestamp1,
78 number => $number1,
79 branchcode => 'LIB1',
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 );
87 my $href_entry2 = {
88 title => $title2,
89 content => $new2,
90 lang => $lang2,
91 expirationdate => $expirationdate2,
92 published_on=> $timestamp2,
93 number => $number2,
94 borrowernumber => $brwrnmbr,
95 branchcode => 'LIB1',
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 );
102 my $href_entry3 = {
103 title => $title3,
104 content => $new3,
105 lang => $lang3,
106 published_on=> $timestamp3,
107 number => $number3,
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.
116 my $query =
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 );
119 $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 );
123 $query =
124 q{ SELECT idnew from opac_news WHERE published_on='2000-01-02'; };
125 my ( $idnew3 ) = $dbh->selectrow_array( $query );
127 # Test upd_opac_new
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_new (single news item)
138 $timestamp1 = output_pref( { dt => dt_from_string( $timestamp1 ), dateonly => 1 } );
139 $expirationdate1 = output_pref( { dt => dt_from_string( $expirationdate1 ), dateonly => 1 } );
140 $timestamp2 = output_pref( { dt => dt_from_string( $timestamp2 ), dateonly => 1 } );
141 $expirationdate2 = output_pref( { dt => dt_from_string( $expirationdate2) , dateonly => 1 } );
143 my $updated_on = %{get_opac_new($idnew1)}{updated_on};
144 is_deeply(
145 get_opac_new($idnew1),
147 title => $title1,
148 content => $new1,
149 lang => $lang1,
150 expirationdate => $expirationdate1,
151 published_on=> $timestamp1,
152 number => $number1,
153 borrowernumber => undef,
154 idnew => $idnew1,
155 branchname => "$addbra branch",
156 branchcode => $addbra,
157 updated_on => $updated_on,
159 'got back expected news item via get_opac_new - ID 1'
162 # Test get_opac_new (single news item)
163 $updated_on = %{get_opac_new($idnew2)}{updated_on};
164 is_deeply(
165 get_opac_new($idnew2),
167 title => $title2,
168 content => $new2,
169 lang => $lang2,
170 expirationdate => $expirationdate2,
171 published_on=> $timestamp2,
172 number => $number2,
173 borrowernumber => $brwrnmbr,
174 idnew => $idnew2,
175 branchname => "$addbra branch",
176 branchcode => $addbra,
177 updated_on => $updated_on,
179 'got back expected news item via get_opac_new - ID 2'
182 # Test get_opac_new (single news item without expiration date)
183 my $news3 = get_opac_new($idnew3);
184 is($news3->{ expirationdate }, undef, "Expiration date should be empty");
186 # Test get_opac_news (multiple news items)
187 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
189 # using >= 2, because someone may have LIB1 news already.
190 ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
192 # Test GetNewsToDisplay
193 ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
194 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
196 # Regression test 14248 -- make sure author_title, author_firstname, and
197 # author_surname exist.
199 subtest 'Regression tests on author title, firstname, and surname.', sub {
200 my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
201 my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
202 ok($opac_news_count>0,'Data exists for regression testing');
203 foreach my $news_item (@$opac_news) {
204 ok(exists $news_item->{author_title}, 'Author title exists');
205 ok(exists $news_item->{author_firstname},'Author first name exists');
206 ok(exists $news_item->{author_surname}, 'Author surname exists');
207 if ($news_item->{borrowernumber}) {
208 ok(defined $news_item->{author_title} ||
209 defined $news_item->{author_firstname} ||
210 defined $news_item->{author_surname}, 'Author data defined');
211 $check = $check | 2; # bitwise flag;
213 else {
214 ok(!defined $news_item->{author_title},
215 'Author title undefined as expected');
216 ok(!defined $news_item->{author_firstname},
217 'Author first name undefined as expected');
218 ok(!defined $news_item->{author_surname},
219 'Author surname undefined as expected');
220 $check = $check | 1; # bitwise flag;
223 ok($check==3,'Both with and without author data tested');
224 done_testing();