Added a new syspref to Load history to the next user
authorBaptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Mon, 13 Feb 2017 15:21:57 +0000 (15:21 +0000)
committerBrendan A Gallagher <brendan@bywatersolutions.com>
Wed, 22 Mar 2017 19:01:28 +0000 (19:01 +0000)
Added a syspref LoadHistory addSearchHistoryToTheFirstLoggedUser to select if you want the system to add the history of searches performed without session when you log in as registered user.
TEST PLAN
1 - Search in the catalogue, check you are not logged
2 - Log in : your last history should appear
4 - Log out
5 - Apply the patch
6 - Repeat 1 and 2
7 - Desactivate the syspref addSearchHistoryToTheFirstLoggedUser
8 - Repeat 1 and 2 : your last history shouldn't appear

The Unit test doesn't rollback but delete the added lines : the function get_template_and_user allway sets the autocommit to 1.

https://bugs.koha-community.org/show_bug.cgi?id=8010

Tested 3 patches together, works as expected.
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
C4/Auth.pm
installer/data/mysql/atomicupdate/bug_8010-add_LoadSearchHistoryToTheFirstLoggedUser_syspref.sql [new file with mode: 0644]
installer/data/mysql/sysprefs.sql
koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
t/db_dependent/Auth2.t [new file with mode: 0644]

index 5dcf1cd..54d9194 100644 (file)
@@ -330,31 +330,33 @@ sub get_template_and_user {
                 # We show the link in opac
                 $template->param( EnableOpacSearchHistory => 1 );
             }
+            if (C4::Context->preference('addSearchHistoryToTheFirstLoggedUser'))
+            {
+                # And if there are searches performed when the user was not logged in,
+                # we add them to the logged-in search history
+                my @recentSearches = C4::Search::History::get_from_session( { cgi => $in->{'query'} } );
+                if (@recentSearches) {
+                    my $dbh   = C4::Context->dbh;
+                    my $query = q{
+                        INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, type,  total, time )
+                        VALUES (?, ?, ?, ?, ?, ?, ?)
+                    };
+                    my $sth = $dbh->prepare($query);
+                    $sth->execute( $borrowernumber,
+                        $in->{query}->cookie("CGISESSID"),
+                        $_->{query_desc},
+                        $_->{query_cgi},
+                        $_->{type} || 'biblio',
+                        $_->{total},
+                        $_->{time},
+                    ) foreach @recentSearches;
+
+                    # clear out the search history from the session now that
+                    # we've saved it to the database
+                 }
+              }
+              C4::Search::History::set_to_session( { cgi => $in->{'query'}, search_history => [] } );
 
-            # And if there are searches performed when the user was not logged in,
-            # we add them to the logged-in search history
-            my @recentSearches = C4::Search::History::get_from_session( { cgi => $in->{'query'} } );
-            if (@recentSearches) {
-                my $dbh   = C4::Context->dbh;
-                my $query = q{
-                    INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, type,  total, time )
-                    VALUES (?, ?, ?, ?, ?, ?, ?)
-                };
-
-                my $sth = $dbh->prepare($query);
-                $sth->execute( $borrowernumber,
-                    $in->{query}->cookie("CGISESSID"),
-                    $_->{query_desc},
-                    $_->{query_cgi},
-                    $_->{type} || 'biblio',
-                    $_->{total},
-                    $_->{time},
-                ) foreach @recentSearches;
-
-                # clear out the search history from the session now that
-                # we've saved it to the database
-                C4::Search::History::set_to_session( { cgi => $in->{'query'}, search_history => [] } );
-            }
         } elsif ( $in->{type} eq 'intranet' and C4::Context->preference('EnableSearchHistory') ) {
             $template->param( EnableSearchHistory => 1 );
         }
diff --git a/installer/data/mysql/atomicupdate/bug_8010-add_LoadSearchHistoryToTheFirstLoggedUser_syspref.sql b/installer/data/mysql/atomicupdate/bug_8010-add_LoadSearchHistoryToTheFirstLoggedUser_syspref.sql
new file mode 100644 (file)
index 0000000..78c7256
--- /dev/null
@@ -0,0 +1,2 @@
+INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
+('addSearchHistoryToTheFirstLoggedUser', '1', NULL, 'If ON, the next user will automatically get the last seaches in his history', 'YesNo');
index 1aa3f8d..a85615c 100644 (file)
@@ -235,6 +235,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('LinkerModule','Default','Default|FirstMatch|LastMatch','Chooses which linker module to use (see documentation).','Choice'),
 ('LinkerOptions','','','A pipe-separated list of options for the linker.','free'),
 ('LinkerRelink','1',NULL,'If ON the authority linker will relink headings that have previously been linked every time it runs.','YesNo'),
+('LoadSearchHistoryToTheFirstLoggedUser', '1', NULL, 'If ON, the next user will automatically get the last seaches in his history', 'YesNo'),
 ('LocalCoverImages','0','1','Display local cover images on intranet details pages.','YesNo'),
 ('LocalHoldsPriority',  '0', NULL,  'Enables the LocalHoldsPriority feature',  'YesNo'),
 ('LocalHoldsPriorityItemControl',  'holdingbranch',  'holdingbranch|homebranch',  'decides if the feature operates using the item''s home or holding library.',  'Choice'),
index f996ad6..0178468 100644 (file)
@@ -79,6 +79,14 @@ Searching:
                   no: "Don't keep"
             - patron search history in the staff client.
     Search Form:
+        -
+            - Load the unlogged history to the next user.
+            - pref : LoadSearchHistoryToTheFirstLoggedUser
+              default: 0
+              choices:
+                   yes: "Load"
+                   no : "Don't load"
+            - history to the next client.
         -
             - Show tabs in OPAC and staff-side advanced search for limiting searches on the
             - pref: AdvancedSearchTypes
diff --git a/t/db_dependent/Auth2.t b/t/db_dependent/Auth2.t
new file mode 100644 (file)
index 0000000..7a92d03
--- /dev/null
@@ -0,0 +1,147 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use CGI qw ( -utf8 );
+use Test::MockModule;
+use List::MoreUtils qw/all any none/;
+use Test::More tests => 2;
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use C4::Auth;
+use Koha::AuthUtils qw/hash_password/;
+use Koha::Database;
+
+my $query = new CGI;
+
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
+
+# Borrower Creation
+my $hash = hash_password('password');
+my $patron = $builder->build( { source => 'Borrower' } );
+Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash );
+
+my $session = C4::Auth::get_session("");
+$session->flush;
+
+sub myMockedget_from_session {
+    my $expected_recent_searches = [
+        {
+            'time' => 1374978877,
+            'query_cgi' => 'cgi_test',
+            'total' => 2,
+            'query_desc' => 'kw,wrdl: history, '
+        }
+    ];
+    return @{$expected_recent_searches};
+
+}
+
+my $getfrom = new Test::MockModule( 'C4::Search::History' );
+$getfrom->mock( 'get_from_session', \&myMockedget_from_session );
+
+my $cgi = new Test::MockModule( 'CGI');
+$cgi->mock('cookie', sub {
+   my ($self, $key) = @_;
+  if (!ref($key) && $key eq 'CGISESSID'){
+         return 'ID';
+   }
+});
+
+sub MockedCheckauth {
+    my ($query,$authnotrequired,$flagsrequired,$type) = @_;
+    my $userid = $patron->{userid};
+    my $sessionID = 234;
+    my $flags = {
+        superlibrarian    => 1, acquisition       => 0,
+        borrowers         => 0,
+        catalogue         => 1, circulate         => 0,
+        coursereserves    => 0, editauthorities   => 0,
+        editcatalogue     => 0, management        => 0,
+        parameters        => 0, permissions       => 0,
+        plugins           => 0, reports           => 0,
+        reserveforothers  => 0, serials           => 0,
+        staffaccess       => 0, tools             => 0,
+        updatecharges     => 0
+    };
+
+    my $session_cookie = $query->cookie(
+        -name => 'CGISESSID',
+        -value    => '9884013ae2c441d12e0bc9376242d2a8',
+        -HttpOnly => 1
+    );
+    return ( $userid, $session_cookie, $sessionID, $flags );
+}
+
+# Mock checkauth
+my $auth = new Test::MockModule( 'C4::Auth' );
+$auth->mock( 'checkauth', \&MockedCheckauth );
+
+$query->param('koha_login_context', 'opac');
+$query->param('userid', $patron->{userid});
+$query->param('password', 'password');
+
+# Test when the syspref is disabled
+t::lib::Mocks::mock_preference('addSearchHistoryToTheFirstLoggedUser', 0);
+my $result = $schema->resultset('SearchHistory')->search()->count;
+
+my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
+    {
+        template_name   => "opac-user.tt",
+        query           => $query,
+        type            => "opac",
+        authnotrequired => 0,
+        debug           => 1
+    }
+);
+
+my $result2 = $schema->resultset('SearchHistory')->search()->count;
+is($result2, $result, 'no new search added to borrower');
+
+# Test when the syspref is enabled
+t::lib::Mocks::mock_preference('addSearchHistoryToTheFirstLoggedUser', 1);
+$query->param('koha_login_context', 'opac');
+$query->param('userid', $patron->{userid});
+$query->param('password', 'password');
+$query->cookie(
+        -name     => 'CGISESSID',
+        -value    => $session->id,
+        -HttpOnly => 1
+);
+
+$result = $schema->resultset('SearchHistory')->search()->count;
+
+( $template, $loggedinuser, $cookies ) = get_template_and_user(
+    {
+        template_name   => "opac-user.tt",
+        query           => $query,
+        type            => "opac",
+        authnotrequired => 0,
+        debug           => 1
+    }
+);
+
+$result2 = $schema->resultset('SearchHistory')->search()->count;
+is($result2, $result+1, 'new search added to borrower');
+
+# Delete the inserts
+$result = $schema->resultset('SearchHistory')->search(undef, { query_cgi => 'cgi_test'});
+$result->delete_all();