ArgumentValidatorTermTest.php 3.84 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Functional\Views;
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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'];

50 51
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    // 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;
  }

}