Skip to content
Snippets Groups Projects
Verified Commit 56c078e5 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3440041 by quietone, mondrake: Log warning for an invalid view display...

Issue #3440041 by quietone, mondrake: Log warning for an invalid view display in  \Drupal\views\ViewExecutable::setDisplay
parent e7ec8284
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
namespace Drupal\views; namespace Drupal\views;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\Tags;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\display\DisplayRouterInterface; use Drupal\views\Plugin\views\display\DisplayRouterInterface;
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#[\AllowDynamicProperties] #[\AllowDynamicProperties]
class ViewExecutable { class ViewExecutable {
use LoggerChannelTrait;
/** /**
* The config entity in which the view is stored. * The config entity in which the view is stored.
* *
...@@ -821,7 +823,12 @@ public function setDisplay($display_id = NULL) { ...@@ -821,7 +823,12 @@ public function setDisplay($display_id = NULL) {
// Ensure the requested display exists. // Ensure the requested display exists.
if (!$this->displayHandlers->has($display_id)) { 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; return FALSE;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\NodeType; use Drupal\node\Entity\NodeType;
use Drupal\views\Entity\View; use Drupal\views\Entity\View;
use Drupal\views\Views; use Drupal\views\Views;
...@@ -21,7 +22,6 @@ ...@@ -21,7 +22,6 @@
use Drupal\views\Plugin\views\pager\PagerPluginBase; use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\Plugin\views\query\QueryPluginBase; use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views_test_data\Plugin\views\display\DisplayTest; use Drupal\views_test_data\Plugin\views\display\DisplayTest;
use PHPUnit\Framework\Error\Warning;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
/** /**
...@@ -204,17 +204,22 @@ public function testProperties() { ...@@ -204,17 +204,22 @@ public function testProperties() {
} }
public function testSetDisplayWithInvalidDisplay() { public function testSetDisplayWithInvalidDisplay() {
\Drupal::service('module_installer')->install(['dblog']);
$view = Views::getView('test_executable_displays'); $view = Views::getView('test_executable_displays');
$view->initDisplay(); $view->initDisplay();
// Error is triggered while calling the wrong display. // Error is logged while calling the wrong display.
try { $view->setDisplay('invalid');
$view->setDisplay('invalid'); $arguments = [
$this->fail('Expected error, when setDisplay() called with invalid display ID'); '@display_id' => 'invalid',
} ];
catch (Warning $e) { $logged = Database::getConnection()->select('watchdog')
$this->assertEquals('setDisplay() called with invalid display ID "invalid".', $e->getMessage()); ->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('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)); $this->assertEquals(spl_object_hash($view->displayHandlers->get('default')), spl_object_hash($view->display_handler));
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
namespace Drupal\Tests\views_ui\Functional; namespace Drupal\Tests\views_ui\Functional;
use Drupal\Core\Database\Database;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Entity\View; use Drupal\views\Entity\View;
...@@ -50,6 +51,7 @@ public function testDeleteLink() { ...@@ -50,6 +51,7 @@ public function testDeleteLink() {
* Tests the machine name and administrative comment forms. * Tests the machine name and administrative comment forms.
*/ */
public function testOtherOptions() { public function testOtherOptions() {
\Drupal::service('module_installer')->install(['dblog']);
$this->drupalGet('admin/structure/views/view/test_view'); $this->drupalGet('admin/structure/views/view/test_view');
// Add a new attachment display. // Add a new attachment display.
$this->submitForm([], 'Add Attachment'); $this->submitForm([], 'Add Attachment');
...@@ -85,13 +87,17 @@ public function testOtherOptions() { ...@@ -85,13 +87,17 @@ public function testOtherOptions() {
$error_text = 'Display machine name must contain only lowercase letters, numbers, or underscores.'; $error_text = 'Display machine name must contain only lowercase letters, numbers, or underscores.';
// Test that potential invalid display ID requests are detected // 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->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title'); $arguments = [
$this->fail('Expected error, when setDisplay() called with invalid display ID'); '@display_id' => 'fake_display_name',
} ];
catch (\Exception $e) { $logged = Database::getConnection()->select('watchdog')
$this->assertStringContainsString('setDisplay() called with invalid display ID "fake_display_name".', $e->getMessage()); ->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']; $edit = ['display_id' => 'test 1'];
$this->drupalGet($machine_name_edit_url); $this->drupalGet($machine_name_edit_url);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment