From da1de2ff7f2cd1fc701164181d5270b53d6a779e Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 8 Mar 2023 15:43:30 +0000 Subject: [PATCH] Issue #3266341 by nlisgo, cilefen, carstenG, david.qdoscc, Lendude, quietone, smustgrave: Views pagers do math on disparate data types, resulting in type errors in PHP 8 (cherry picked from commit cbd7cf7be9c2687bbc41948d7eae60ac5262ff90) --- .../views/src/Plugin/views/pager/SqlBase.php | 2 +- .../src/Unit/Plugin/pager/SqlBaseTest.php | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 core/modules/views/tests/src/Unit/Plugin/pager/SqlBaseTest.php diff --git a/core/modules/views/src/Plugin/views/pager/SqlBase.php b/core/modules/views/src/Plugin/views/pager/SqlBase.php index a3f8a0a4c68..c3b1d99920d 100644 --- a/core/modules/views/src/Plugin/views/pager/SqlBase.php +++ b/core/modules/views/src/Plugin/views/pager/SqlBase.php @@ -260,7 +260,7 @@ abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInt if ($this->itemsPerPageExposed()) { $query = $this->view->getRequest()->query; $items_per_page = $query->get('items_per_page'); - if ($items_per_page > 0) { + if ((int) $items_per_page > 0) { $this->options['items_per_page'] = $items_per_page; } elseif ($items_per_page == 'All' && $this->options['expose']['items_per_page_options_all']) { diff --git a/core/modules/views/tests/src/Unit/Plugin/pager/SqlBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/pager/SqlBaseTest.php new file mode 100644 index 00000000000..99b65df3c33 --- /dev/null +++ b/core/modules/views/tests/src/Unit/Plugin/pager/SqlBaseTest.php @@ -0,0 +1,91 @@ +pager = $this->getMockBuilder('Drupal\views\Plugin\views\pager\SqlBase') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + /** @var \Drupal\views\ViewExecutable|\PHPUnit\Framework\MockObject\MockObject $view */ + $this->view = $this->getMockBuilder('Drupal\views\ViewExecutable') + ->disableOriginalConstructor() + ->getMock(); + + $query = $this->getMockBuilder(QueryPluginBase::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->view->query = $query; + + $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase') + ->disableOriginalConstructor() + ->getMock(); + + $container = new ContainerBuilder(); + $container->set('string_translation', $this->getStringTranslationStub()); + \Drupal::setContainer($container); + } + + /** + * Tests the query() method. + * + * @see \Drupal\views\Plugin\views\pager\SqlBase::query() + */ + public function testQuery() { + $request = new Request([ + 'items_per_page' => 'All', + ]); + $this->view->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($request)); + + $options = []; + $this->pager->init($this->view, $this->display, $options); + $this->pager->query(); + $this->assertSame(10, $this->pager->options['items_per_page']); + + $options = [ + 'expose' => [ + 'items_per_page' => TRUE, + 'items_per_page_options_all' => TRUE, + ], + ]; + $this->pager->init($this->view, $this->display, $options); + $this->pager->query(); + $this->assertSame(0, $this->pager->options['items_per_page']); + } + +}