Commit 4c1b7b8e authored by catch's avatar catch
Browse files

Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four, daffie,...

Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four, daffie, Lendude: 500 error on passing invalid month to MonthDate view argument handler

(cherry picked from commit f4e6cd9bc0075cc9381dd771e594852cbf1dd849)
parent 752615a3
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -24,16 +24,26 @@ class MonthDate extends Date {
   */
  public function summaryName($data) {
    $month = str_pad($data->{$this->name_alias}, 2, '0', STR_PAD_LEFT);
    try {
      return $this->dateFormatter->format(strtotime("2005" . $month . "15" . " 00:00:00 UTC"), 'custom', $this->format, 'UTC');
    }
    catch (\InvalidArgumentException $e) {
      return parent::summaryName($data);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function title() {
    $month = str_pad($this->argument, 2, '0', STR_PAD_LEFT);
    try {
      return $this->dateFormatter->format(strtotime("2005" . $month . "15" . " 00:00:00 UTC"), 'custom', $this->format, 'UTC');
    }
    catch (\InvalidArgumentException $e) {
      return parent::title();
    }
  }

  public function summaryArgument($data) {
    // Make sure the argument contains leading zeroes.
+173 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - node
id: test_month_date_plugin
label: 'Test Month Date Plugin'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: none
        options: {  }
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: none
        options:
          offset: 0
      style:
        type: default
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
          uses_fields: false
      row:
        type: fields
        options:
          inline: {  }
          separator: ''
          hide_empty: false
          default_field_elements: true
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          entity_type: node
          entity_field: title
          label: ''
          alter:
            alter_text: false
            make_link: false
            absolute: false
            trim: false
            word_boundary: false
            ellipsis: false
            strip_tags: false
            html: false
          hide_empty: false
          empty_zero: false
          settings:
            link_to_entity: true
          plugin_id: field
          relationship: none
          group_type: group
          admin_label: ''
          exclude: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_alter_empty: true
          click_sort_column: value
          type: string
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
      filters: {  }
      sorts: {  }
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments:
        created_month:
          id: created_month
          table: node_field_data
          field: created_month
          relationship: none
          group_type: group
          admin_label: ''
          default_action: default
          exception:
            value: all
            title_enable: false
            title: All
          title_enable: false
          title: ''
          default_argument_type: query_parameter
          default_argument_options:
            query_param: month
            fallback: all
            multiple: or
          default_argument_skip_url: false
          summary_options:
            base_path: ''
            count: true
            items_per_page: 25
            override: false
          summary:
            sort_order: asc
            number_of_records: 0
            format: default_summary
          specify_validation: false
          validate:
            type: none
            fail: 'not found'
          validate_options: {  }
          entity_type: node
          plugin_id: date_month
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - url
        - 'user.node_grants:view'
      tags: {  }
  page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: test-month-date-plugin
    cache_metadata:
      max-age: -1
      contexts:
        - url
        - 'user.node_grants:view'
      tags: {  }
+101 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\views\Functional\Plugin;

use Drupal\Core\Url;
use Drupal\Tests\views\Functional\ViewTestBase;

/**
 * Tests the Month Date Plugin.
 *
 * @group views
 */
class MonthDatePluginTest extends ViewTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = ['test_month_date_plugin'];

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = ['node'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Test node 1.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $node1;

  /**
   * Test node 2.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $node2;

  /**
   * {@inheritdoc}
   */
  public function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);
    $date1 = '2020-10-01 00:00:00';
    $this->node1 = $this->drupalCreateNode(['created' => strtotime($date1)]);
    $date2 = '2020-11-01 00:00:00';
    $this->node2 = $this->drupalCreateNode(['created' => strtotime($date2)]);
  }

  /**
   * Tests the Month Date Plugin.
   */
  public function testMonthDatePlugin() {
    $assert_session = $this->assertSession();

    // Test fallback value.
    $this->drupalGet('test-month-date-plugin');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains($this->node1->getTitle());
    $assert_session->pageTextContains($this->node2->getTitle());

    // Test 'all' values.
    $this->drupalGet('test-month-date-plugin/all');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains($this->node1->getTitle());
    $assert_session->pageTextContains($this->node2->getTitle());

    // Test valid month value.
    $this->drupalGet('test-month-date-plugin/10');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains($this->node1->getTitle());
    $assert_session->pageTextNotContains($this->node2->getTitle());

    // Test query parameter.
    $url = Url::fromUserInput('/test-month-date-plugin', [
      'query' => [
        'month' => 10,
      ],
    ]);
    $this->drupalGet($url);
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains($this->node1->getTitle());
    $assert_session->pageTextNotContains($this->node2->getTitle());

    // Test invalid month name.
    $this->drupalGet('test-month-date-plugin/invalid-month');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextNotContains($this->node1->getTitle());
    $assert_session->pageTextNotContains($this->node2->getTitle());
  }

}