Bug 8435: (follow-up) handle lack of userenv gracefully
authorJonathan Druart <jonathan.druart@biblibre.com>
Thu, 31 Oct 2013 08:49:12 +0000 (09:49 +0100)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 31 Oct 2013 15:29:42 +0000 (15:29 +0000)
C4::Serials::can_edit_subscription now deals with C4::Context->userenv.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
C4/Serials.pm
t/db_dependent/Serials_2.t

index 7c4bf82..da876e2 100644 (file)
@@ -2805,6 +2805,7 @@ Return 1 if the subscription is editable by the current logged user (or a given
 
 sub can_edit_subscription {
     my ( $subscription, $userid ) = @_;
+    return 0 unless C4::Context->userenv;
     my $flags = C4::Context->userenv->{flags};
     $userid ||= C4::Context->userenv->{'id'};
     my $independent_branches = C4::Context->preference('IndependentBranches');
index 72d71d9..3d921b8 100644 (file)
@@ -1,10 +1,12 @@
 #!/usr/bin/perl
 use Modern::Perl;
 
-use Test::More tests => 4;
+use Test::More tests => 5;
 
 use MARC::Record;
+
 use C4::Biblio qw( AddBiblio );
+use C4::Context;
 use_ok('C4::Serials');
 use_ok('C4::Budgets');
 
@@ -12,6 +14,7 @@ my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
+
 my $supplierlist=eval{GetSuppliersWithLateIssues()};
 ok(length($@)==0,"No SQL problem in GetSuppliersWithLateIssues");
 
@@ -39,16 +42,19 @@ my $budget_id = AddBudget({
 });
 
 my $subscriptionid = NewSubscription(
-    undef,      "",     undef, undef, $budget_id, $biblionumber, '01-01-2013',undef,
-    undef,      undef,  undef, undef, undef,      undef,         undef,  undef,
-    undef,      undef,  undef, undef, undef,      undef,         undef,  undef,
-    undef,      undef,  undef, undef, undef,      undef,         undef,  1,
-    "notes",    undef,  undef, undef, undef,      undef,         undef,  0,
-    "intnotes", 0,      undef, undef, 0,          undef,         '12-31-2013',
+    undef,      "",     undef, undef, $budget_id, $biblionumber,
+    '2013-01-01', undef, undef, undef,  undef,
+    undef,      undef,  undef, undef, undef, undef,
+    1,          "notes",undef, '2013-01-01', undef, undef,
+    undef,       undef,  0,    "intnotes",  0,
+    undef, undef, 0,          undef,         '2013-12-31', 0
 );
 die unless $subscriptionid;
 
-# Can edit a subscription
+
+my $subscription = GetSubscription( $subscriptionid );
+is( C4::Serials::can_edit_subscription($subscription), 0, "cannot edit a subscription without userenv set");
+
 my @USERENV = (
     1,
     'test',
@@ -62,9 +68,9 @@ my @USERENV = (
 
 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
 C4::Context->set_userenv ( @USERENV );
-my $userenv = C4::Context->userenv;
 
-my $subscription = GetSubscription( $subscriptionid );
+# Can edit a subscription
+my $userenv = C4::Context->userenv;
 
 is( C4::Serials::can_edit_subscription($subscription), 1, "User can edit a subscription with an empty branchcode");
 #TODO add UT when C4::Auth->set_permissions (or setuserflags) will exist.