From 2be29106853fd0c99313eafd6fc67de9e894e4b1 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 3 Apr 2017 12:59:34 +0100
Subject: [PATCH] Issue #2782221 by Grimreaper, dawehner, Lendude, alexpott:
 Result summary Area plugin not displayed when there is no result

---
 .../views/src/Plugin/views/area/Result.php    | 12 ++-
 .../views.view.test_area_result.yml           | 79 +++++++++++++++++++
 .../src/Kernel/Handler/AreaResultTest.php     | 72 +++++++++++++++++
 3 files changed, 159 insertions(+), 4 deletions(-)
 create mode 100644 core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml
 create mode 100644 core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php

diff --git a/core/modules/views/src/Plugin/views/area/Result.php b/core/modules/views/src/Plugin/views/area/Result.php
index f0082dbb7b36..d2f1b01ab613 100644
--- a/core/modules/views/src/Plugin/views/area/Result.php
+++ b/core/modules/views/src/Plugin/views/area/Result.php
@@ -83,9 +83,13 @@ public function render($empty = FALSE) {
     // Not every view has total_rows set, use view->result instead.
     $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result);
     $label = Html::escape($this->view->storage->label());
+    // If there is no result the "start" and "current_record_count" should be
+    // equal to 0. To have the same calculation logic, we use a "start offset"
+    // to handle all the cases.
+    $start_offset = empty($total) ? 0 : 1;
     if ($per_page === 0) {
       $page_count = 1;
-      $start = 1;
+      $start = $start_offset;
       $end = $total;
     }
     else {
@@ -94,10 +98,10 @@ public function render($empty = FALSE) {
       if ($total_count > $total) {
         $total_count = $total;
       }
-      $start = ($current_page - 1) * $per_page + 1;
+      $start = ($current_page - 1) * $per_page + $start_offset;
       $end = $total_count;
     }
-    $current_record_count = ($end - $start) + 1;
+    $current_record_count = ($end - $start) + $start_offset;
     // Get the search information.
     $replacements = [];
     $replacements['@start'] = $start;
@@ -109,7 +113,7 @@ public function render($empty = FALSE) {
     $replacements['@current_record_count'] = $current_record_count;
     $replacements['@page_count'] = $page_count;
     // Send the output.
-    if (!empty($total)) {
+    if (!empty($total) || !empty($this->options['empty'])) {
       $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format));
     }
     // Return as render array.
diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml
new file mode 100644
index 000000000000..8ca5628583ec
--- /dev/null
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_area_result.yml
@@ -0,0 +1,79 @@
+langcode: en
+status: true
+dependencies: {  }
+id: test_area_result
+label: ''
+module: views
+description: ''
+tag: ''
+base_table: views_test_data
+base_field: nid
+core: '8'
+display:
+  default:
+    display_options:
+      defaults:
+        fields: false
+        pager: false
+        sorts: false
+      fields:
+        id:
+          field: id
+          id: id
+          relationship: none
+          table: views_test_data
+          plugin_id: numeric
+      pager:
+        options:
+          offset: 0
+        type: none
+      sorts:
+        id:
+          field: id
+          id: id
+          order: ASC
+          relationship: none
+          table: views_test_data
+          plugin_id: numeric
+      empty:
+        title:
+          field: title
+          id: title
+          table: views
+          plugin_id: title
+          title: test_title_empty
+      header:
+        result:
+          id: result
+          table: views
+          field: result
+          relationship: none
+          group_type: group
+          admin_label: ''
+          empty: true
+          content: "start: @start | end: @end | total: @total | label: @label | per page: @per_page | current page: @current_page | current record count: @current_record_count | page count: @page_count"
+          plugin_id: result
+    display_plugin: default
+    display_title: Master
+    id: default
+    position: 0
+  page_1:
+    display_options:
+      path: test-area-result
+      defaults:
+        header: false
+      header:
+        result:
+          id: result
+          table: views
+          field: result
+          relationship: none
+          group_type: group
+          admin_label: ''
+          empty: false
+          content: "start: @start | end: @end | total: @total | label: @label | per page: @per_page | current page: @current_page | current record count: @current_record_count | page count: @page_count"
+          plugin_id: result
+    display_plugin: page
+    display_title: 'Page 1'
+    id: page_1
+    position: 1
diff --git a/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
new file mode 100644
index 000000000000..8b67fec67bb9
--- /dev/null
+++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Drupal\Tests\views\Kernel\Handler;
+
+use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
+use Drupal\views\Views;
+
+/**
+ * Tests the result area handler.
+ *
+ * @group views
+ * @see \Drupal\views\Plugin\views\area\Result
+ */
+class AreaResultTest extends ViewsKernelTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $testViews = ['test_area_result'];
+
+  /**
+   * Tests the results area handler.
+   */
+  public function testResult() {
+    $view = Views::getView('test_area_result');
+    $view->setDisplay('default');
+    $this->executeView($view);
+    $output = $view->render();
+    $output = \Drupal::service('renderer')->renderRoot($output);
+    $this->setRawContent($output);
+    $this->assertText('start: 1 | end: 5 | total: 5 | label: test_area_result | per page: 0 | current page: 1 | current record count: 5 | page count: 1');
+  }
+
+  /**
+   * Tests the results area handler.
+   */
+  public function testResultEmpty() {
+    $view = Views::getView('test_area_result');
+
+    // Test that the area is displayed if we have checked the empty checkbox.
+    $view->setDisplay('default');
+
+    // Add a filter that will make the result set empty.
+    $view->displayHandlers->get('default')->overrideOption('filters', [
+      'name' => [
+        'id' => 'name',
+        'table' => 'views_test_data',
+        'field' => 'name',
+        'relationship' => 'none',
+        'operator' => '=',
+        'value' => 'non-existing-name',
+      ],
+    ]);
+
+    $this->executeView($view);
+    $output = $view->render();
+    $output = \Drupal::service('renderer')->renderRoot($output);
+    $this->setRawContent($output);
+    $this->assertText('start: 0 | end: 0 | total: 0 | label: test_area_result | per page: 0 | current page: 1 | current record count: 0 | page count: 1');
+
+    // Test that the area is not displayed if we have not checked the empty
+    // checkbox.
+    $view->setDisplay('page_1');
+
+    $this->executeView($view);
+    $output = $view->render();
+    $output = \Drupal::service('renderer')->renderRoot($output);
+    $this->setRawContent($output);
+    $this->assertNoText('start: 0 | end: 0 | total: 0 | label: test_area_result | per page: 0 | current page: 1 | current record count: 0 | page count: 1');
+  }
+
+}
-- 
GitLab