Verified Commit 99dea2fa 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

(cherry picked from commit 56c078e5)
parent def593ba
Loading
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -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;
    }

+14 −9
Original line number Diff line number Diff line
@@ -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 {
    // Error is logged while calling the wrong display.
    $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());
    }
    $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));
+13 −7
Original line number Diff line number Diff line
@@ -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());
    }
    $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);