validateArgument example

/** * Views used by this test. * * @var array */
  public static $testViews = ['test_view_argument_validate_numeric', 'test_view'];

  public function testArgumentValidateNumeric() {
    $view = Views::getView('test_view_argument_validate_numeric');
    $view->initHandlers();
    $this->assertFalse($view->argument['null']->validateArgument($this->randomString()));
    // Reset saved argument validation.     $view->argument['null']->argument_validated = NULL;
    $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() {
    
'null' => [
        'id' => 'null',
        'table' => 'views',
        'field' => 'null',
      ],
    ]);

    $this->executeView($view);

    // Make sure that the argument is not validated yet.     unset($view->argument['null']->argument_validated);
    $this->assertTrue($view->argument['null']->validateArgument(26));
    // test must_not_be option.     unset($view->argument['null']->argument_validated);
    $view->argument['null']->options['must_not_be'] = TRUE;
    $this->assertFalse($view->argument['null']->validateArgument(26), 'must_not_be returns FALSE, if there is an argument');
    unset($view->argument['null']->argument_validated);
    $this->assertTrue($view->argument['null']->validateArgument(NULL), 'must_not_be returns TRUE, if there is no argument');

    // Test execution.     $view->destroy();
    $view->setDisplay();

    
// By using % in URLs, arguments could be validated twice; this eases     // that pain.     if (isset($this->argument_validated)) {
      return $this->argument_validated;
    }

    if ($this->isException($arg)) {
      return $this->argument_validated = TRUE;
    }

    $plugin = $this->getPlugin('argument_validator');
    return $this->argument_validated = $plugin->validateArgument($arg);
  }

  /** * Called by the menu system to validate an argument. * * This checks to see if this is a 'soft fail', which means that if the * argument fails to validate, but there is an action to take anyway, * then validation cannot actually fail. */
  public function validateMenuArgument($arg) {
    $validate_info = $this->defaultActions($this->options['validate']['fail']);
    
$this->account = $this->createUser();
    ViewTestData::createTestViews(static::class['user_test_views']);
  }

  /** * Tests the User (ID) argument validator. */
  public function testArgumentValidateUserUid() {
    $view = Views::getView('test_view_argument_validate_user');
    $this->executeView($view);

    $this->assertTrue($view->argument['null']->validateArgument($this->account->id()));
    // Reset argument validation.     $view->argument['null']->argument_validated = NULL;
    // Fail for a valid numeric, but for a user that doesn't exist     $this->assertFalse($view->argument['null']->validateArgument(32));

    $form = [];
    $form_state = new FormState();
    $view->argument['null']->buildOptionsForm($form$form_state);
    $sanitized_id = ArgumentPluginBase::encodeValidatorId('entity:user');
    $this->assertTrue($form['validate']['options'][$sanitized_id]['roles']['#states']['visible'][':input[name="options[validate][options][' . $sanitized_id . '][restrict_roles]"]']['checked']);
  }

  
/** * Tests the validate argument method with no access and bundles. * * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument() */
  public function testValidateArgumentNoAccess() {
    $options = [];
    $options['access'] = FALSE;
    $options['bundles'] = [];
    $this->argumentValidator->init($this->executable, $this->display, $options);

    $this->assertFalse($this->argumentValidator->validateArgument(3));
    $this->assertFalse($this->argumentValidator->validateArgument(''));
    $this->assertFalse($this->argumentValidator->validateArgument(NULL));

    $this->assertTrue($this->argumentValidator->validateArgument(1));
    $this->assertTrue($this->argumentValidator->validateArgument(2));
    $this->assertFalse($this->argumentValidator->validateArgument('1,2'));
  }

  /** * Tests the validate argument method with access and no bundles. * * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument() */
Home | Imprint | This part of the site doesn't use cookies.