Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / ArgumentValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\views\Views;
7 use Drupal\views_test_data\Plugin\views\argument_validator\ArgumentValidatorTest as ArgumentValidatorTestPlugin;
8
9 /**
10  * Tests Views argument validators.
11  *
12  * @group views
13  */
14 class ArgumentValidatorTest extends ViewsKernelTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_view_argument_validate_numeric', 'test_view'];
22
23   public function testArgumentValidateNumeric() {
24     $view = Views::getView('test_view_argument_validate_numeric');
25     $view->initHandlers();
26     $this->assertFalse($view->argument['null']->validateArgument($this->randomString()));
27     // Reset safed argument validation.
28     $view->argument['null']->argument_validated = NULL;
29     $this->assertTrue($view->argument['null']->validateArgument(12));
30   }
31
32   /**
33    * Tests the argument validator test plugin.
34    *
35    * @see Drupal\views_test_data\Plugin\views\argument_validator\ArgumentValidatorTest
36    */
37   public function testArgumentValidatorPlugin() {
38     $view = Views::getView('test_view');
39
40     // Add a new argument and set the test plugin for the argument_validator.
41     $options = [
42       'specify_validation' => TRUE,
43       'validate' => [
44         'type' => 'argument_validator_test',
45       ],
46     ];
47     $id = $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
48     $view->initHandlers();
49
50     $test_value = $this->randomMachineName();
51
52     $argument = $view->argument[$id];
53     $argument->options['validate_options']['test_value'] = $test_value;
54     $this->assertFalse($argument->validateArgument($this->randomMachineName()), 'A random value does not validate.');
55     // Reset internal flag.
56     $argument->argument_validated = NULL;
57     $this->assertTrue($argument->validateArgument($test_value), 'The right argument validates.');
58
59     $plugin = $argument->getPlugin('argument_validator');
60     $this->assertTrue($plugin instanceof ArgumentValidatorTestPlugin, 'The correct argument validator plugin is used.');
61     $this->assertFalse($plugin->validateArgument($this->randomMachineName()), 'A random value does not validate.');
62     $this->assertTrue($plugin->validateArgument($test_value), 'The right argument validates.');
63   }
64
65 }