You are here

public function ArgumentValidatorTest::testArgumentValidatorPlugin in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Plugin/ArgumentValidatorTest.php \Drupal\Tests\views\Kernel\Plugin\ArgumentValidatorTest::testArgumentValidatorPlugin()

Tests the argument validator test plugin.

See also

Drupal\views_test_data\Plugin\views\argument_validator\ArgumentValidatorTest

File

core/modules/views/tests/src/Kernel/Plugin/ArgumentValidatorTest.php, line 37

Class

ArgumentValidatorTest
Tests Views argument validators.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

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
    ->assertInstanceOf(ArgumentValidatorTestPlugin::class, $plugin);
  $this
    ->assertFalse($plugin
    ->validateArgument($this
    ->randomMachineName()), 'A random value does not validate.');
  $this
    ->assertTrue($plugin
    ->validateArgument($test_value), 'The right argument validates.');
}