From 3f00a89f5e01336c188567160fea682d892ca6bf Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 7 Sep 2015 15:56:49 +0100
Subject: [PATCH] =?UTF-8?q?Issue=20#1739846=20by=20dawehner,=20B=C3=A8s,?=
 =?UTF-8?q?=20DuaelFr,=20madhavvyas,=20damiankloip,=20aspilicious,=20fasta?=
 =?UTF-8?q?ngel,=20tim.plunkett,=20xjm:=20Tests=20taxonomy=20argument=20va?=
 =?UTF-8?q?lidator=20plugin?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Tests/Views/ArgumentValidatorTermTest.php | 127 +++++++++++
 .../Tests/Plugin/ArgumentValidatorTest.php    |  36 ++-
 ...iews.view.test_argument_validator_term.yml | 213 ++++++++++++++++++
 .../ArgumentValidatorTest.php                 |  17 ++
 4 files changed, 392 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/taxonomy/src/Tests/Views/ArgumentValidatorTermTest.php
 create mode 100644 core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument_validator_term.yml

diff --git a/core/modules/taxonomy/src/Tests/Views/ArgumentValidatorTermTest.php b/core/modules/taxonomy/src/Tests/Views/ArgumentValidatorTermTest.php
new file mode 100644
index 000000000000..0e2853b5b23f
--- /dev/null
+++ b/core/modules/taxonomy/src/Tests/Views/ArgumentValidatorTermTest.php
@@ -0,0 +1,127 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\taxonomy\Tests\Views\ArgumentValidatorTermTest.
+ */
+
+namespace Drupal\taxonomy\Tests\Views;
+
+use Drupal\views\Views;
+
+/**
+ * Tests the plugin of the taxonomy: term argument validator.
+ *
+ * @group taxonomy
+ * @see Views\taxonomy\Plugin\views\argument_validator\Term
+ */
+class ArgumentValidatorTermTest extends TaxonomyTestBase {
+
+  /**
+   * Stores the taxonomy term used by this test.
+   *
+   * @var array
+   */
+  protected $terms = [];
+
+  /**
+   * Stores the taxonomy names used by this test.
+   *
+   * @var array
+   */
+  protected $names = [];
+
+  /**
+   * Stores the taxonomy IDs used by this test.
+   *
+   * @var array
+   */
+  protected $ids = [];
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = ['taxonomy', 'taxonomy_test_views', 'views_test_config'];
+
+  /**
+   * Views used by this test.
+   *
+   * @var array
+   */
+  public static $testViews = ['test_argument_validator_term'];
+
+  protected function setUp() {
+    parent::setUp();
+
+    // Add three terms to the 'tags' vocabulary.
+    for ($i = 0; $i < 3; $i++) {
+      $this->terms[] = $term = $this->createTerm();
+      $this->names[] = $term->label();
+      $this->ids[] = $term->id();
+    }
+  }
+
+  /**
+   * Tests the term argument validator plugin.
+   */
+  public function testArgumentValidatorTerm() {
+    $view = Views::getView('test_argument_validator_term');
+    $view->initHandlers();
+
+
+    // Test the single validator for term IDs.
+    $view->argument['tid']->validator->options['type'] = 'tid';
+
+    // Pass in a single valid term.
+    foreach ($this->terms as $term) {
+      $this->assertTrue($view->argument['tid']->setArgument($term->id()));
+      $this->assertEqual($view->argument['tid']->getTitle(), $term->label());
+      $view->argument['tid']->validated_title = NULL;
+      $view->argument['tid']->argument_validated = NULL;
+    }
+
+    // Pass in a invalid term.
+    $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
+    $this->assertEqual('', $view->argument['tid']->getTitle());
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+
+
+    // Test the multiple validator for term IDs.
+    $view->argument['tid']->validator->options['type'] = 'tids';
+    $view->argument['tid']->options['break_phrase'] = TRUE;
+
+    // Pass in a single term.
+    $this->assertTrue($view->argument['tid']->setArgument($this->terms[0]->id()));
+    $this->assertEqual($view->argument['tid']->getTitle(), $this->terms[0]->label());
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+
+    // Check for multiple valid terms separated by commas.
+    $this->assertTrue($view->argument['tid']->setArgument(implode(',', $this->ids)));
+    $this->assertEqual($view->argument['tid']->getTitle(), implode(', ', $this->names));
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+
+    // Check for multiple valid terms separated by plus signs.
+    $this->assertTrue($view->argument['tid']->setArgument(implode('+', $this->ids)));
+    $this->assertEqual($view->argument['tid']->getTitle(), implode(' + ', $this->names));
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+
+    // Check for a single invalid term.
+    $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
+    $this->assertEqual('', $view->argument['tid']->getTitle());
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+
+    // Check for multiple invalid terms.
+    $this->assertFalse($view->argument['tid']->setArgument(implode(',', [rand(1000, 10000), rand(1000, 10000)])));
+    $this->assertEqual('', $view->argument['tid']->getTitle());
+    $view->argument['tid']->validated_title = NULL;
+    $view->argument['tid']->argument_validated = NULL;
+  }
+
+}
diff --git a/core/modules/views/src/Tests/Plugin/ArgumentValidatorTest.php b/core/modules/views/src/Tests/Plugin/ArgumentValidatorTest.php
index ecbcf51fd5f4..08b8984c625d 100644
--- a/core/modules/views/src/Tests/Plugin/ArgumentValidatorTest.php
+++ b/core/modules/views/src/Tests/Plugin/ArgumentValidatorTest.php
@@ -9,6 +9,7 @@
 
 use Drupal\views\Tests\ViewKernelTestBase;
 use Drupal\views\Views;
+use Drupal\views_test_data\Plugin\views\argument_validator\ArgumentValidatorTest as ArgumentValidatorTestPlugin;
 
 /**
  * Tests Views argument validators.
@@ -22,7 +23,7 @@ class ArgumentValidatorTest extends ViewKernelTestBase {
    *
    * @var array
    */
-  public static $testViews = array('test_view_argument_validate_numeric');
+  public static $testViews = ['test_view_argument_validate_numeric', 'test_view'];
 
   function testArgumentValidateNumeric() {
     $view = Views::getView('test_view_argument_validate_numeric');
@@ -33,4 +34,37 @@ function testArgumentValidateNumeric() {
     $this->assertTrue($view->argument['null']->validateArgument(12));
   }
 
+  /**
+   * Tests the argument validator test plugin.
+   *
+   * @see Drupal\views_test_data\Plugin\views\argument_validator\ArgumentValidatorTest
+   */
+  public function testArgumentValidatorPlugin() {
+    $view = Views::getView('test_view');
+
+    // Add a new argument and set the test plugin for the argument_validator.
+    $options = [
+      'specify_validation' => TRUE,
+      'validate' => [
+        'type' => 'argument_validator_test'
+      ]
+    ];
+    $id = $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
+    $view->initHandlers();
+
+    $test_value = $this->randomMachineName();
+
+    $argument = $view->argument[$id];
+    $argument->options['validate_options']['test_value'] = $test_value;
+    $this->assertFalse($argument->validateArgument($this->randomMachineName()), 'A random value does not validate.');
+    // Reset internal flag.
+    $argument->argument_validated = NULL;
+    $this->assertTrue($argument->validateArgument($test_value), 'The right argument validates.');
+
+    $plugin = $argument->getPlugin('argument_validator');
+    $this->assertTrue($plugin instanceof ArgumentValidatorTestPlugin, 'The correct argument validator plugin is used.');
+    $this->assertFalse($plugin->validateArgument($this->randomMachineName()), 'A random value does not validate.');
+    $this->assertTrue($plugin->validateArgument($test_value), 'The right argument validates.');
+  }
+
 }
diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument_validator_term.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument_validator_term.yml
new file mode 100644
index 000000000000..18e28d1b0fed
--- /dev/null
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument_validator_term.yml
@@ -0,0 +1,213 @@
+langcode: en
+status: true
+dependencies:
+  module:
+    - node
+    - taxonomy
+    - user
+id: test_argument_validator_term
+label: test_argument_validator_term
+module: views
+description: ''
+tag: ''
+base_table: node_field_data
+base_field: nid
+core: 8.x
+display:
+  default:
+    display_plugin: default
+    id: default
+    display_title: Master
+    position: 0
+    display_options:
+      access:
+        type: perm
+        options:
+          perm: 'access content'
+      cache:
+        type: none
+        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: full
+        options:
+          items_per_page: 10
+          offset: 0
+          id: 0
+          total_pages: null
+          expose:
+            items_per_page: false
+            items_per_page_label: 'Items per page'
+            items_per_page_options: '5, 10, 25, 50'
+            items_per_page_options_all: false
+            items_per_page_options_all_label: '- All -'
+            offset: false
+            offset_label: Offset
+          tags:
+            previous: '‹ previous'
+            next: 'next ›'
+            first: '« first'
+            last: 'last »'
+          quantity: 9
+      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:
+        status:
+          value: true
+          table: node_field_data
+          field: status
+          plugin_id: boolean
+          entity_type: node
+          entity_field: status
+          id: status
+          expose:
+            operator: ''
+          group: 1
+      sorts:
+        created:
+          id: created
+          table: node_field_data
+          field: created
+          order: DESC
+          entity_type: node
+          entity_field: created
+          plugin_id: date
+          relationship: none
+          group_type: group
+          admin_label: ''
+          exposed: false
+          expose:
+            label: ''
+          granularity: second
+      header: {  }
+      footer: {  }
+      empty: {  }
+      relationships: {  }
+      arguments:
+        tid:
+          id: tid
+          table: taxonomy_index
+          field: tid
+          relationship: none
+          group_type: group
+          admin_label: ''
+          default_action: ignore
+          exception:
+            value: all
+            title_enable: false
+            title: All
+          title_enable: true
+          title: '%1'
+          default_argument_type: fixed
+          default_argument_options:
+            argument: ''
+          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: true
+          validate:
+            type: 'entity:taxonomy_term'
+            fail: 'access denied'
+          validate_options:
+            operation: view
+            multiple: 1
+            bundles: {  }
+            access: false
+          break_phrase: false
+          add_table: false
+          require_value: false
+          reduce_duplicates: false
+          plugin_id: taxonomy_index_tid
+      display_extenders: {  }
+    cache_metadata:
+      contexts:
+        - 'languages:language_content'
+        - 'languages:language_interface'
+        - url
+        - 'url.query_args.pagers:0'
+        - 'user.node_grants:view'
+      cacheable: false
diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_validator/ArgumentValidatorTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_validator/ArgumentValidatorTest.php
index 83e6f3c2ea86..86ecacf1d441 100644
--- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_validator/ArgumentValidatorTest.php
+++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_validator/ArgumentValidatorTest.php
@@ -27,4 +27,21 @@ public function calculateDependencies() {
     ];
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function defineOptions() {
+    $options = parent::defineOptions();
+    $options['test_value'] = ['default' => ''];
+
+    return $options;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateArgument($arg) {
+    return $arg == $this->options['test_value'];
+  }
+
 }
-- 
GitLab