diff --git a/core/modules/views/src/Tests/DefaultViewsTest.php b/core/modules/views/src/Tests/DefaultViewsTest.php index 23e7c97e54db8b88e7f368d2296d51dbfa3a4762..197d25e5adf993141bc6953074db78ec4933047b 100644 --- a/core/modules/views/src/Tests/DefaultViewsTest.php +++ b/core/modules/views/src/Tests/DefaultViewsTest.php @@ -210,6 +210,13 @@ public function testArchiveView() { ), ); $this->assertIdenticalResultset($view, $expected_result, $column_map); + + $view->storage->setStatus(TRUE); + $view->save(); + \Drupal::service('router.builder')->rebuild(); + + $this->drupalGet('archive'); + $this->assertResponse(200); } } diff --git a/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php b/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..cea8fba1bc050d3c378218d9db00602f47e5bbfd --- /dev/null +++ b/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php @@ -0,0 +1,77 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Tests\Plugin\StyleSummaryTest. + */ + +namespace Drupal\views\Tests\Plugin; + +use Drupal\entity_test\Entity\EntityTest; +use Drupal\views\Tests\ViewTestBase; + +/** + * Tests the summary style plugin. + * + * @group views + */ +class StyleSummaryTest extends ViewTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['entity_test']; + + /** + * {@inheritdoc} + */ + public static $testViews = ['test_summary']; + + /** + * @var \Drupal\entity_test\Entity\EntityTest[] + */ + protected $entities = []; + + /** + * {@inheritdoc} + */ + protected function setUp($import_test_views = TRUE) { + parent::setUp($import_test_views); + + // Create 5 entities per bundle, to allow a summary overview per bundle. + for ($i = 0; $i < 5; $i++) { + for ($j = 0; $j < 5; $j++) { + $this->entities[] = $entity = EntityTest::create([ + 'name' => 'Entity ' . ($i * 5 + $j), + 'type' => 'type' . $i, + ]); + $entity->save(); + } + } + } + + /** + * Tests a summary view. + */ + public function testSummaryView() { + $this->drupalGet('test-summary'); + + $summary_list = $this->cssSelect('ul.views-summary li'); + $this->assertEqual(4, count($summary_list)); + + foreach ($summary_list as $summary_list_item) { + $this->assertEqual('(5)', trim((string) $summary_list_item)); + } + + $summary_links = $this->cssSelect('ul.views-summary a'); + $this->assertEqual(4, count($summary_links)); + foreach ($summary_links as $index => $summary_link) { + $this->assertEqual('type' . $index, trim((string) $summary_link)); + } + + $this->clickLink('type1'); + $entries = $this->cssSelect('div.view-content div.views-row'); + $this->assertEqual(2, count($entries)); + } + +} diff --git a/core/modules/views/src/Tests/Plugin/StyleTestBase.php b/core/modules/views/src/Tests/Plugin/StyleTestBase.php index 38db4d9030bec585650fb23cb99090c163ce6670..508fc1214b5f216ae1c72301a5734e930a78c224 100644 --- a/core/modules/views/src/Tests/Plugin/StyleTestBase.php +++ b/core/modules/views/src/Tests/Plugin/StyleTestBase.php @@ -18,7 +18,7 @@ abstract class StyleTestBase extends ViewKernelTestBase { /** * Stores the SimpleXML representation of the output. * - * @var SimpleXMLElement + * @var \SimpleXMLElement */ protected $elements; diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 9f5d32a88cca37952ad4af67dc3c8a6557383440..3ac4e17923d774f99dd56df4f7c8e3456da5cb50 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -553,7 +553,11 @@ public function getItemsPerPage() { * The items per page. */ public function setItemsPerPage($items_per_page) { - $this->element['#cache']['keys'][] = 'items_per_page:' . $items_per_page; + // Check whether the element is pre rendered. At that point, the cache keys + // cannot longer be manipulated. + if (empty($this->element['#pre_rendered'])) { + $this->element['#cache']['keys'][] = 'items_per_page:' . $items_per_page; + } $this->items_per_page = $items_per_page; // If the pager is already initialized, pass it through to the pager. @@ -583,9 +587,15 @@ public function getOffset() { * The pager offset. */ public function setOffset($offset) { - $this->element['#cache']['keys'][] = 'offset:' . $offset; + // Check whether the element is pre rendered. At that point, the cache keys + // cannot longer be manipulated. + if (empty($this->element['#pre_rendered'])) { + $this->element['#cache']['keys'][] = 'offset:' . $offset; + } + $this->offset = $offset; + // If the pager is already initialized, pass it through to the pager. if (!empty($this->pager)) { $this->pager->setOffset($offset); diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml new file mode 100644 index 0000000000000000000000000000000000000000..daf92fa2a27bd01e71355f20878e62145b2f9c1c --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml @@ -0,0 +1,127 @@ +langcode: en +status: true +id: test_summary +label: Test Summary +module: views +description: '' +tag: default +base_table: entity_test +base_field: id +core: '8' +display: + default: + id: default + display_title: Master + display_plugin: default + position: 0 + display_options: + query: + type: views_query + options: + query_comment: '' + disable_sql_rewrite: false + distinct: false + replica: false + query_tags: { } + title: 'Summary test' + access: + type: none + cache: + type: tag + options: { } + pager: + type: mini + options: + items_per_page: 2 + offset: 0 + id: 0 + total_pages: 0 + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '1, 2, 3, 4' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: ‹‹ + next: ›› + sorts: + id: + id: id + table: entity_test + field: id + order: ASC + plugin_id: standard + relationship: none + arguments: + type: + id: type + field: type + table: entity_test + default_action: summary + exception: + title_enable: true + title_enable: true + title: '{{ arguments.type }}' + default_argument_type: fixed + summary: + sort_order: asc + format: default_summary + summary_options: + override: true + items_per_page: 4 + specify_validation: true + plugin_id: string + entity_type: entity_test + fields: + id: + id: id + field: id + table: entity_test + plugin_id: field + entity_type: entity_test + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + header: { } + footer: { } + empty: { } + relationships: { } + fields: { } + display_extenders: { } + cache_metadata: + contexts: + - 'languages:language_interface' + - url + - url.query_args + - user.permissions + max-age: -1 + tags: { } + page_1: + id: page_1 + display_title: Page + display_plugin: page + position: 2 + display_options: + query: + type: views_query + options: { } + path: test-summary + display_extenders: { } + cache_metadata: + contexts: + - 'languages:language_interface' + - url + - url.query_args + - 'user.node_grants:view' + - user.permissions + max-age: -1 + tags: { } diff --git a/core/modules/views/tests/src/Unit/ViewExecutableTest.php b/core/modules/views/tests/src/Unit/ViewExecutableTest.php index fea412b63b36c9901e689c2afde50485d8e9e604..049df8d9e8ede7fd398a3b70d1e90983b270f26b 100644 --- a/core/modules/views/tests/src/Unit/ViewExecutableTest.php +++ b/core/modules/views/tests/src/Unit/ViewExecutableTest.php @@ -469,4 +469,100 @@ protected function setupBaseViewAndDisplay() { return array($view, $display); } + /** + * @covers ::setItemsPerPage + * @covers ::getItemsPerPage + */ + public function testSetItemsPerPageBeforePreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $view->setItemsPerPage(12); + $this->assertEquals(12, $view->getItemsPerPage()); + $this->assertContains('items_per_page:12', $view->element['#cache']['keys']); + } + + /** + * @covers ::setItemsPerPage + * @covers ::getItemsPerPage + */ + public function testSetItemsPerPageDuringPreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $elements = &$view->element; + $elements['#cache'] += ['keys' => []]; + $elements['#pre_rendered'] = TRUE; + + $view->setItemsPerPage(12); + $this->assertEquals(12, $view->getItemsPerPage()); + $this->assertNotContains('items_per_page:12', $view->element['#cache']['keys']); + } + + /** + * @covers ::setOffset + * @covers ::getOffset + */ + public function testSetOffsetBeforePreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $view->setOffset(12); + $this->assertEquals(12, $view->getOffset()); + $this->assertContains('offset:12', $view->element['#cache']['keys']); + } + + /** + * @covers ::setOffset + * @covers ::getOffset + */ + public function testSetOffsetDuringPreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $elements = &$view->element; + $elements['#cache'] += ['keys' => []]; + $elements['#pre_rendered'] = TRUE; + + $view->setOffset(12); + $this->assertEquals(12, $view->getOffset()); + $this->assertNotContains('offset:12', $view->element['#cache']['keys']); + } + + /** + * @covers ::setCurrentPage + * @covers ::getCurrentPage + */ + public function testSetCurrentPageBeforePreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $view->setCurrentPage(12); + $this->assertEquals(12, $view->getCurrentPage()); + $this->assertContains('page:12', $view->element['#cache']['keys']); + } + + /** + * @covers ::setCurrentPage + * @covers ::getCurrentPage + */ + public function testSetCurrentPageDuringPreRender() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $elements = &$view->element; + $elements['#cache'] += ['keys' => []]; + $elements['#pre_rendered'] = TRUE; + + $view->setCurrentPage(12); + $this->assertEquals(12, $view->getCurrentPage()); + $this->assertNotContains('page:12', $view->element['#cache']['keys']); + } + }