Koha/t/db_dependent/NewsChannels.t
Jonathan Druart 19a977dc7b Bug 15758: Koha::Libraries - Remove GetBranchName
This is the fourth and last patch set to remove C4::Branch.
The real purpose of this patch is to standardise and refactor some code
which is related to the libraries selection/display.
Its unconfessed purpose is to remove the C4::Branch package.

Before this patch set, only 6 subroutines still existed in the C4::Branch
package:
- GetBranchName
- GetBranchesLoop
- mybranch
- onlymine
- GetBranches
- GetBranch

GetBranchName basically returns the branchname for a given branchcode.
The branchname is only used for a display purpose and we don't need to
retrieve it in package or pl scripts (unless for a few exceptions).
We have a `Branches` template plugin with a `GetName` method which does
exactly this job.
To achieve this removal, we will use this template plugin and delete the
GetBranchName from pl and pm files.
The `Branches.all()` will now select the library of the logged in user
if no `selected` parameter has been passed.
This new behavior could cause regressions, for instance there are some
places where we do not want an option preselected (batch item
modification for instance), keep that in mind when testing.

GetBranchesLoop took 3 parameters: $branch and $onlymine.
The first one was used to set a "selected" flag, for a display purpose:
select an option in the libraries dropdown lists.
The second one was useless: If not passed or set to 0, the
`C4::Branch::onlymine` subroutine was called.
This onlymine flag was use to know if the logged in user was able to see
other libraries infos.
A patron can see the infos from other libraries if IndependentBranches
is not set OR if he has the superlibrarian permission.
Prior to this patch set, the "onlymine test" was done on different
places (neworderempty.pl, additem.pl, holidays.pl, etc.), including the
Branches TT plugin. In this patch set, this test is only done on one
place (C4::Context::only_my_library, code moved from
C4::Branch::onlymine).
To accomplish the same job as this subroutine, we just need to call the
`Branches.all()` method from the `Branches` TT plugin. It already
accepts a `selected` parameter to set a flag on the option to select.
To avoid the repetitive
  [% IF selected %]<option selected="selected">[% ELSE %]<option>[% END %]
pattern, a new `html_helpers` TT include file has been created, it
defines an `options_for_libraries` block, which takes a `selected`
parameter. We could imagine to use this include file for other
selects.

The 'mybranch` and `onlymine` subroutines of the C4::Branch package have
been moved to C4::Context. onlymine has been renamed with
only_my_library. There are only 4 occurrences of it, against 11 before
this patch set.
There 2 subroutines are Context-centric and it makes sense to put them
in `C4::Context` (at least it's the least worst place!)

GetBranches is the tricky part of this patch set: It retrieves all the
libraries, independently of the value of IndependentBranches.
To keep the same way as the existing calls of `Branches.all()`, I have
added a `unfiltered` parameter. If set, the `Branches.all()` will call
a usual Koha::Libraries->search method, otherwise
Koha::Libraries->search_filtered will be called. This new method will
check if the logged in user is allowed to see other libraries or only
its library.
Note that this `GetBranches` subroutine also created a `category` key:
it allowed to get the list of groups (of libraries) where this library
existed. Thanks to a previous patch set (bug 15295), this value was
not used anymore (I may have missed something!).

Note that the only use of `GetBranch` was buggy (see bug 15746).

Test plan (for the whole patch set):
The best way to test this whole patch set is to test with 2 instances: 1
with the patch set applied, 1 using master, to be sure there is no
regression.
It would be good to test the same with `IndependentBranches` and the
without `IndependentBranches`.
No difference should be found.
The tester must focus on the library dropdowns on as many forms as
possible.
You will notice changes in the order of the options: the libraries will
now be ordered by branchname (instead of branchcode in some places).
A special attention will be given to the following page:
- acqui/neworderempty.pl
- catalogue/search.pl
- members/members-home.pl (header?)
- opac/opac-topissues.pl
- tools/holidays.pl
- admin/branch_transfer_limits.pl
- admin/item_circulation_alerts.pl
- rotating_collections/transferCollection.pl
- suggestion/suggestion.pl
- tools/export.pl

Notes for QA:
- There are 2 FIXMEs in the patch set, I have kept the existing behavior,
but I am not sure it's the good one. Feel free to open a bug report and
I will fill a patch if you think it's not correct. Otherwise, remove the
FIXME lines in a follow-up patch.
- The whole patch set is huge and makes a lot of changes.
But it finally will tremendously reduce the number of lines:
716 insertions for 1910 deletions

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
2016-09-08 14:36:01 +00:00

230 lines
8 KiB
Perl

#!/usr/bin/perl
use Modern::Perl;
use Koha::DateUtils;
use Koha::Libraries;
use Test::More tests => 14;
BEGIN {
use_ok('C4::NewsChannels');
}
my $dbh = C4::Context->dbh;
# Start transaction
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
# Add LIB1, if it doesn't exist.
my $addbra = 'LIB1';
unless ( Koha::Libraries->find($addbra) ) {
$dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) },
undef, ( $addbra, "$addbra branch" ) );
}
# Add CAT1, if it doesn't exist.
my $addcat = 'CAT1';
{
my $sth = $dbh->prepare( q{ SELECT categorycode FROM categories WHERE categorycode = ? } );
$sth->execute ( $addcat );
if ( not defined $sth->fetchrow () ) {
$dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) },
undef, ( $addcat, "$addcat description") );
}
}
# Add a test user if not already present.
my $addbrwr = 'BRWR1';
my $brwrnmbr;
{
my $query =
q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
my $sth = $dbh->prepare( $query );
$sth->execute( ($addbrwr, $addbra, $addcat) );
$brwrnmbr = $sth->fetchrow;
# Not found, let us insert it.
if ( not defined $brwrnmbr ) {
$dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) },
undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) );
# Retrieve the njew borrower number.
$query =
q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? };
my $sth = $dbh->prepare( $query );
$sth->execute( ($addbrwr, $addbra, $addcat) );
$brwrnmbr = $sth->fetchrow;
}
}
# Must have valid borrower number, or tests are meaningless.
ok ( defined $brwrnmbr );
# Test add_opac_new
my $rv = add_opac_new(); # intentionally bad
is( $rv, 0, 'Correctly failed on no parameter!' );
my $timestamp = '2000-01-01';
my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
my $timestamp3 = '2000-01-02';
my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
my $href_entry1 = {
title => $title1,
new => $new1,
lang => $lang1,
expirationdate => $expirationdate1,
timestamp => $timestamp1,
number => $number1,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry1);
is( $rv, 1, 'Successfully added the first dummy news item!' );
my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
my $href_entry2 = {
title => $title2,
new => $new2,
lang => $lang2,
expirationdate => $expirationdate2,
timestamp => $timestamp2,
number => $number2,
borrowernumber => $brwrnmbr,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry2);
is( $rv, 1, 'Successfully added the second dummy news item!' );
my ( $title3, $new3, $lang3, $number3 ) =
( 'News Title3', '<p>News without expiration date</p>', q{}, 1 );
my $href_entry3 = {
title => $title3,
new => $new3,
lang => $lang3,
timestamp => $timestamp3,
number => $number3,
borrowernumber => $brwrnmbr,
branchcode => 'LIB1',
};
$rv = add_opac_new($href_entry3);
is( $rv, 1, 'Successfully added the third dummy news item without expiration date!' );
# We need to determine the idnew in a non-MySQLism way.
# This should be good enough.
my $query =
q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
my ( $idnew1 ) = $dbh->selectrow_array( $query );
$query =
q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
my ( $idnew2 ) = $dbh->selectrow_array( $query );
$query =
q{ SELECT idnew from opac_news WHERE timestamp='2000-01-02'; };
my ( $idnew3 ) = $dbh->selectrow_array( $query );
# Test upd_opac_new
$rv = upd_opac_new(); # intentionally bad parmeters
is( $rv, 0, 'Correctly failed on no parameter!' );
$new2 = '<p>Update! There is no news!</p>';
$href_entry2->{new} = $new2;
$href_entry2->{idnew} = $idnew2;
$rv = upd_opac_new($href_entry2);
is( $rv, 1, 'Successfully updated second dummy news item!' );
# Test get_opac_new (single news item)
$timestamp1 = output_pref( { dt => dt_from_string( $timestamp1 ), dateonly => 1 } );
$expirationdate1 = output_pref( { dt => dt_from_string( $expirationdate1 ), dateonly => 1 } );
$timestamp2 = output_pref( { dt => dt_from_string( $timestamp2 ), dateonly => 1 } );
$expirationdate2 = output_pref( { dt => dt_from_string( $expirationdate2) , dateonly => 1 } );
is_deeply(
get_opac_new($idnew1),
{
title => $title1,
new => $new1,
lang => $lang1,
expirationdate => $expirationdate1,
timestamp => $timestamp1,
number => $number1,
borrowernumber => undef,
idnew => $idnew1,
branchname => "$addbra branch",
branchcode => $addbra,
# this represents $lang => 1 in the hash
# that's returned... which seems a little
# redundant given that there's a perfectly
# good 'lang' key in the hash
'' => 1,
},
'got back expected news item via get_opac_new - ID 1'
);
# Test get_opac_new (single news item)
is_deeply(
get_opac_new($idnew2),
{
title => $title2,
new => $new2,
lang => $lang2,
expirationdate => $expirationdate2,
timestamp => $timestamp2,
number => $number2,
borrowernumber => $brwrnmbr,
idnew => $idnew2,
branchname => "$addbra branch",
branchcode => $addbra,
'' => 1,
},
'got back expected news item via get_opac_new - ID 2'
);
# Test get_opac_new (single news item without expiration date)
my $news3 = get_opac_new($idnew3);
is($news3->{ expirationdate }, undef, "Expiration date should be empty");
# Test get_opac_news (multiple news items)
my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
# using >= 2, because someone may have LIB1 news already.
ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
# Test GetNewsToDisplay
( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
# Regression test 14248 -- make sure author_title, author_firstname, and
# author_surname exist.
subtest 'Regression tests on author title, firstname, and surname.', sub {
my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
ok($opac_news_count>0,'Data exists for regression testing');
foreach my $news_item (@$opac_news) {
ok(exists $news_item->{author_title}, 'Author title exists');
ok(exists $news_item->{author_firstname},'Author first name exists');
ok(exists $news_item->{author_surname}, 'Author surname exists');
if ($news_item->{borrowernumber}) {
ok(defined $news_item->{author_title} ||
defined $news_item->{author_firstname} ||
defined $news_item->{author_surname}, 'Author data defined');
$check = $check | 2; # bitwise flag;
}
else {
ok(!defined $news_item->{author_title},
'Author title undefined as expected');
ok(!defined $news_item->{author_firstname},
'Author first name undefined as expected');
ok(!defined $news_item->{author_surname},
'Author surname undefined as expected');
$check = $check | 1; # bitwise flag;
}
}
ok($check==3,'Both with and without author data tested');
done_testing();
};
$dbh->rollback;