Bug 22544: Move add_opac_item to Koha namespace
[koha.git] / t / db_dependent / NewsChannels.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Koha::Database;
5 use Koha::DateUtils;
6 use Koha::Libraries;
7 use Koha::News;
8
9 use Test::More tests => 4;
10
11 BEGIN {
12     use_ok('C4::NewsChannels');
13 }
14
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $dbh = C4::Context->dbh;
18
19 # Add LIB1, if it doesn't exist.
20 my $addbra = 'LIB1';
21 unless ( Koha::Libraries->find($addbra) ) {
22     $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
23         undef, ( $addbra, "$addbra branch" ) );
24 }
25
26 # Add CAT1, if it doesn't exist.
27 my $addcat = 'CAT1';
28 {
29     my $sth = $dbh->prepare( q{ SELECT categorycode FROM categories WHERE categorycode = ? } );
30     $sth->execute ( $addcat );
31     if ( not defined $sth->fetchrow () ) {
32         $dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) },
33             undef, ( $addcat, "$addcat description") );
34     }
35 }
36
37 # Add a test user if not already present.
38 my $addbrwr = 'BRWR1';
39 my $brwrnmbr;
40 {
41     my $query =
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;
46
47     # Not found, let us insert it.
48     if ( not defined $brwrnmbr ) {
49         $dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) },
50             undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) );
51
52         # Retrieve the njew borrower number.
53         $query =
54             q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
55         my $sth = $dbh->prepare( $query );
56         $sth->execute( ($addbrwr, $addbra, $addcat) );
57         $brwrnmbr = $sth->fetchrow;
58     }
59 }
60
61 # Must have valid borrower number, or tests are meaningless.
62 ok ( defined $brwrnmbr );
63
64 # Test GetNewsToDisplay
65 my ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
66 ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
67