diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 24955c271fcbf0bf8c663511dfc3b28a97f38507..db777c9ee79b22f0c3a7360d2e1fe8fe9dbdc888 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -2,9 +2,9 @@ namespace Drupal\views; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Tags; +use Drupal\Core\Logger\LoggerChannelTrait; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Session\AccountInterface; use Drupal\views\Plugin\views\display\DisplayRouterInterface; @@ -28,6 +28,8 @@ #[\AllowDynamicProperties] class ViewExecutable { + use LoggerChannelTrait; + /** * The config entity in which the view is stored. * @@ -821,7 +823,12 @@ public function setDisplay($display_id = NULL) { // Ensure the requested display exists. if (!$this->displayHandlers->has($display_id)) { - trigger_error(new FormattableMarkup('setDisplay() called with invalid display ID "@display".', ['@display' => $display_id]), E_USER_WARNING); + $this->getLogger('views')->warning( + 'setDisplay() called with invalid display ID "@display_id".', + [ + '@display_id' => $display_id, + ], + ); return FALSE; } diff --git a/core/modules/views/tests/src/Kernel/ViewExecutableTest.php b/core/modules/views/tests/src/Kernel/ViewExecutableTest.php index 907dff79596764fb2a53c4ad618fd13d62bb31f0..5afd8a1c7bb7c8b370ed268db37c3eba80324a72 100644 --- a/core/modules/views/tests/src/Kernel/ViewExecutableTest.php +++ b/core/modules/views/tests/src/Kernel/ViewExecutableTest.php @@ -6,6 +6,7 @@ use Drupal\comment\Tests\CommentTestTrait; use Drupal\Component\Utility\Xss; +use Drupal\Core\Database\Database; use Drupal\node\Entity\NodeType; use Drupal\views\Entity\View; use Drupal\views\Views; @@ -21,7 +22,6 @@ use Drupal\views\Plugin\views\pager\PagerPluginBase; use Drupal\views\Plugin\views\query\QueryPluginBase; use Drupal\views_test_data\Plugin\views\display\DisplayTest; -use PHPUnit\Framework\Error\Warning; use Symfony\Component\HttpFoundation\Response; /** @@ -204,17 +204,22 @@ public function testProperties() { } public function testSetDisplayWithInvalidDisplay() { + \Drupal::service('module_installer')->install(['dblog']); $view = Views::getView('test_executable_displays'); $view->initDisplay(); - // Error is triggered while calling the wrong display. - try { - $view->setDisplay('invalid'); - $this->fail('Expected error, when setDisplay() called with invalid display ID'); - } - catch (Warning $e) { - $this->assertEquals('setDisplay() called with invalid display ID "invalid".', $e->getMessage()); - } + // Error is logged while calling the wrong display. + $view->setDisplay('invalid'); + $arguments = [ + '@display_id' => 'invalid', + ]; + $logged = Database::getConnection()->select('watchdog') + ->fields('watchdog', ['variables']) + ->condition('type', 'views') + ->condition('message', 'setDisplay() called with invalid display ID "@display_id".') + ->execute() + ->fetchField(); + $this->assertEquals(serialize($arguments), $logged); $this->assertEquals('default', $view->current_display, 'If setDisplay is called with an invalid display id the default display should be used.'); $this->assertEquals(spl_object_hash($view->displayHandlers->get('default')), spl_object_hash($view->display_handler)); diff --git a/core/modules/views_ui/tests/src/Functional/ViewEditTest.php b/core/modules/views_ui/tests/src/Functional/ViewEditTest.php index 12b9c02360252ac4c39c793c2fd7950b23041266..eb074c45ffbe58bd8217729d656eb62b20ae6ad8 100644 --- a/core/modules/views_ui/tests/src/Functional/ViewEditTest.php +++ b/core/modules/views_ui/tests/src/Functional/ViewEditTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\views_ui\Functional; +use Drupal\Core\Database\Database; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\views\Entity\View; @@ -50,6 +51,7 @@ public function testDeleteLink() { * Tests the machine name and administrative comment forms. */ public function testOtherOptions() { + \Drupal::service('module_installer')->install(['dblog']); $this->drupalGet('admin/structure/views/view/test_view'); // Add a new attachment display. $this->submitForm([], 'Add Attachment'); @@ -85,13 +87,17 @@ public function testOtherOptions() { $error_text = 'Display machine name must contain only lowercase letters, numbers, or underscores.'; // Test that potential invalid display ID requests are detected - try { - $this->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title'); - $this->fail('Expected error, when setDisplay() called with invalid display ID'); - } - catch (\Exception $e) { - $this->assertStringContainsString('setDisplay() called with invalid display ID "fake_display_name".', $e->getMessage()); - } + $this->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title'); + $arguments = [ + '@display_id' => 'fake_display_name', + ]; + $logged = Database::getConnection()->select('watchdog') + ->fields('watchdog', ['variables']) + ->condition('type', 'views') + ->condition('message', 'setDisplay() called with invalid display ID "@display_id".') + ->execute() + ->fetchField(); + $this->assertEquals(serialize($arguments), $logged); $edit = ['display_id' => 'test 1']; $this->drupalGet($machine_name_edit_url);