3365bd89197ac4a882b6fbc50266238abf4ba46b
[yaffs-website] / web / core / modules / user / src / Tests / Views / ArgumentValidateTest.php
1 <?php
2
3 namespace Drupal\user\Tests\Views;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
7 use Drupal\views\Views;
8
9 /**
10  * Tests user argument validators for ID and name.
11  *
12  * @group user
13  */
14 class ArgumentValidateTest extends UserTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_view_argument_validate_user', 'test_view_argument_validate_username'];
22
23   /**
24    * A user for this test.
25    *
26    * @var \Drupal\user\UserInterface
27    */
28   protected $account;
29
30   protected function setUp() {
31     parent::setUp();
32
33     $this->account = $this->drupalCreateUser();
34   }
35
36   /**
37    * Tests the User (ID) argument validator.
38    */
39   public function testArgumentValidateUserUid() {
40     $account = $this->account;
41
42     $view = Views::getView('test_view_argument_validate_user');
43     $this->executeView($view);
44
45     $this->assertTrue($view->argument['null']->validateArgument($account->id()));
46     // Reset argument validation.
47     $view->argument['null']->argument_validated = NULL;
48     // Fail for a valid numeric, but for a user that doesn't exist
49     $this->assertFalse($view->argument['null']->validateArgument(32));
50
51     $form = [];
52     $form_state = new FormState();
53     $view->argument['null']->buildOptionsForm($form, $form_state);
54     $sanitized_id = ArgumentPluginBase::encodeValidatorId('entity:user');
55     $this->assertTrue($form['validate']['options'][$sanitized_id]['roles']['#states']['visible'][':input[name="options[validate][options][' . $sanitized_id . '][restrict_roles]"]']['checked']);
56   }
57
58   /**
59    * Tests the UserName argument validator.
60    */
61   public function testArgumentValidateUserName() {
62     $account = $this->account;
63
64     $view = Views::getView('test_view_argument_validate_username');
65     $this->executeView($view);
66
67     $this->assertTrue($view->argument['null']->validateArgument($account->getUsername()));
68     // Reset argument validation.
69     $view->argument['null']->argument_validated = NULL;
70     // Fail for a valid string, but for a user that doesn't exist
71     $this->assertFalse($view->argument['null']->validateArgument($this->randomMachineName()));
72   }
73
74 }