Respect cutoff for caching

Change-Id: I59a2dba2c35cff170cbe56e596aa6adee5bec933
diff --git a/lib/Kalamar/Controller/Search.pm b/lib/Kalamar/Controller/Search.pm
index e8f3dff..9afa171 100644
--- a/lib/Kalamar/Controller/Search.pm
+++ b/lib/Kalamar/Controller/Search.pm
@@ -34,14 +34,19 @@
   $v->optional('ql')->in(qw/poliqarp cosmas2 annis cql fcsql/);
   $v->optional('collection', 'trim'); # Legacy
   $v->optional('cq', 'trim');         # New
-  $v->optional('cutoff')->in(qw/true false/);
-  $v->optional('count')->num(1, undef);
+  $v->optional('cutoff', 'trim')->in(qw/true false/);
+  $v->optional('count', 'trim')->num(1, undef);
   $v->optional('p', 'trim')->num(1, undef); # Start page
   $v->optional('o', 'trim')->num(1, undef); # Offset
   $v->optional('context');
   # $v->optional('action'); # action 'inspect' is no longer valid
   # $v->optional('snippet');
 
+  my $cutoff = 0;
+  if ($v->param('cutoff') && $v->param('cutoff') =~ /true/i) {
+    $cutoff = 1;
+  };
+
   # Get query
   my $query = $v->param('q');
 
@@ -106,7 +111,8 @@
 
   # Check if total results information is cached
   my $total_results = -1;
-  unless ($c->no_cache) {
+
+  if (!$cutoff && !$c->no_cache) {
 
     # Create cache string
     my $user = $c->user->handle;
@@ -150,7 +156,7 @@
       unless (defined $total_results) {
 
         # There are results to remember
-        if ($json->{meta}->{totalResults} >= 0) {
+        if (!$cutoff && $json->{meta}->{totalResults} >= 0) {
 
           # Remove cutoff requirement again
           # $url->query([cutoff => 'true']);
@@ -167,6 +173,7 @@
         # Undefined total results
         else {
           $c->stash(total_results => -1);
+          $total_results = -1;
         };
       };
 
diff --git a/lib/Kalamar/Controller/User.pm b/lib/Kalamar/Controller/User.pm
index 6eb0372..af199a2 100644
--- a/lib/Kalamar/Controller/User.pm
+++ b/lib/Kalamar/Controller/User.pm
@@ -62,7 +62,7 @@
 };
 
 
-
+# Currently not in used
 sub register {
   my $c = shift;
   $c->render(json => {
@@ -71,7 +71,7 @@
 };
 
 
-
+# Currently not in use
 sub pwdforgotten {
   my $c = shift;
   $c->render(json => {
diff --git a/t/query.t b/t/query.t
index 59d8fc9..215061a 100644
--- a/t/query.t
+++ b/t/query.t
@@ -100,6 +100,30 @@
   ->text_is('#total-results', 51)
   ;
 
+# Query without partial cache (unfortunately) (but no total results)
+$t->get_ok('/?q=baum&cutoff=true')
+  ->status_is(200)
+  ->text_is('#error','')
+  ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
+  ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
+  ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
+  ->header_isnt('X-Kalamar-Cache', 'true')
+  ->content_like(qr!\"cutOff":true!)
+  ->element_exists_not('#total-results')
+  ;
+
+# Query with partial cache (but no total results)
+$t->get_ok('/?q=baum&cutoff=true')
+  ->status_is(200)
+  ->text_is('#error','')
+  ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
+  ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
+  ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
+  ->header_is('X-Kalamar-Cache', 'true')
+  ->content_like(qr!\"cutOff":true!)
+  ->element_exists_not('#total-results')
+  ;
+
 # Query with full cache
 $t->get_ok('/?q=baum')
   ->status_is(200)