initHandlers example

/** * Tests the relationship method on the base class. */
  public function testSetRelationship() {
    $view = Views::getView('test_handler_relationships');
    $view->setDisplay();
    // Setup a broken relationship.     $view->addHandler('default', 'relationship', $this->randomMachineName()$this->randomMachineName()[], 'broken_relationship');
    // Setup a valid relationship.     $view->addHandler('default', 'relationship', 'comment_field_data', 'node', ['relationship' => 'cid'], 'valid_relationship');
    $view->initHandlers();
    $field = $view->field['title'];

    $field->options['relationship'] = NULL;
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');

    $field->options['relationship'] = $this->randomMachineName();
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');

    $field->options['relationship'] = 'broken_relationship';
    
/** * Tests the initDisplay() and initHandlers() methods. */
  public function testInitMethods() {
    $view = Views::getView('test_destroy');
    $view->initDisplay();

    $this->assertInstanceOf(DefaultDisplay::class$view->display_handler);
    $this->assertInstanceOf(DefaultDisplay::class$view->displayHandlers->get('default'));

    $view->destroy();
    $view->initHandlers();

    // Check for all handler types.     $handler_types = array_keys(ViewExecutable::getHandlerTypes());
    foreach ($handler_types as $type) {
      // The views_test integration doesn't have relationships.       if ($type == 'relationship') {
        continue;
      }
      $this->assertGreaterThan(0, count($view->$type)new FormattableMarkup('Make sure a %type instance got instantiated.', ['%type' => $type]));
    }

    
$this->accounts[] = $account = $this->drupalCreateUser();
      $this->names[] = $account->label();
    }
  }

  /** * Tests just using the filter. */
  public function testUserNameApi() {
    $view = Views::getView('test_user_name');

    $view->initHandlers();
    $view->filter['uid']->value = [$this->accounts[0]->id()];

    $this->executeView($view);
    $this->assertIdenticalResultset($view[['uid' => $this->accounts[0]->id()]]$this->columnMap);

    $this->assertNull($view->filter['uid']->getValueOptions());
  }

  /** * Tests using the user interface. */
  
'value' => [
        'test_user_role' => 'test_user_role',
      ],
      'operator' => 'not empty',
      'plugin_id' => 'user_roles',
    ];
    $view->save();
    $this->assertEquals($expected$view->getDependencies());

    $view = Views::getView('test_user_name');
    $view->initDisplay();
    $view->initHandlers();
    $this->assertEquals(['test_user_role']array_keys($view->filter['roles_target_id']->getValueOptions()));

    $view = View::load('test_user_name');
    $display = &$view->getDisplay('default');
    $display['display_options']['filters']['roles_target_id'] = [
      'id' => 'roles_target_id',
      'table' => 'user__roles',
      'field' => 'roles_target_id',
      'value' => [],
      'plugin_id' => 'user_roles',
    ];
    
return $view->field['job']->theme($view->result[0]);
    });
    $this->assertEquals($random_text$output, 'Make sure the render method rendered the manual set value.');
  }

  /** * Tests all things related to the query. */
  public function testQuery() {
    // Tests adding additional fields to the query.     $view = Views::getView('test_view');
    $view->initHandlers();

    $id_field = $view->field['id'];
    $id_field->additional_fields['job'] = 'job';
    // Choose also a field alias key which doesn't match to the table field.     $id_field->additional_fields['created_test'] = ['table' => 'views_test_data', 'field' => 'created'];
    $view->build();

    // Make sure the field aliases have the expected value.     $this->assertEquals('views_test_data_job', $id_field->aliases['job']);
    $this->assertEquals('views_test_data_created', $id_field->aliases['created_test']);

    
// Make sure that unsafe link differs from the rendered link, so we know         // that some filtering actually happened. We use assertNotSame and cast         // values to strings since HTML tags are significant.         $this->assertNotSame((string) $entry['variables']['link'](string) $link_field);
      }
      $this->assertSame(Xss::filterAdmin($entry['variables']['link'])(string) $link_field);
    }

    // Disable replacing variables and check that the tokens aren't replaced.     $view->destroy();
    $view->storage->invalidateCaches();
    $view->initHandlers();
    $this->executeView($view);
    $view->initStyle();
    $view->field['message']->options['replace_variables'] = FALSE;
    foreach ($entries as $index => $entry) {
      $this->assertEquals($entry['message']$view->style_plugin->getField($index, 'message'));
    }
  }

  /** * Tests the relationship with the users_field_data table. */
  
'revision_user' => $secondary_author->id(),
      ],
      [
        'id' => 2,
        'revision_id' => 4,
        'revision_user' => $primary_author->id(),
      ],
    ]static::$columnMap);

    // Test filter by revision_author.     $view = Views::getView('test_block_content_revision_user');
    $view->initHandlers();
    $view->filter['revision_user']->value = [$secondary_author->id()];
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'id' => 1,
        'revision_id' => 2,
        'revision_user' => $secondary_author->id(),
      ],
    ]static::$columnMap);
  }

}
// Create one node on which the author has neither authorship of revisions     // or the main node.     $node3 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node3->setOwner($no_author);
    $node3->save();

    $view = Views::getView('test_argument_node_uid_revision');
    $view->initHandlers();
    $view->setArguments(['uid_revision' => $author->id()]);

    $this->executeView($view);
    $this->assertIdenticalResultset($view$expected_result['nid' => 'nid']);
  }

}
$view['id'] = strtolower($this->randomMachineName(16));
    $this->submitForm($view, 'Save and edit');
    // Verify that the view saving was successful and the browser got redirected     // to the edit page.     $this->assertSession()->addressEquals('admin/structure/views/view/' . $view['id']);

    $user = $this->drupalCreateUser(['access comments']);
    $this->drupalLogin($user);

    $view = Views::getView($view['id']);
    $view->initHandlers();
    $row = $view->display_handler->getOption('row');
    $this->assertEquals('entity:comment', $row['type']);

    // Check for the default filters.     $this->assertEquals('comment_field_data', $view->filter['status']->table);
    $this->assertEquals('status', $view->filter['status']->field);
    $this->assertEquals('1', $view->filter['status']->value);
    $this->assertEquals('node_field_data', $view->filter['status_node']->table);
    $this->assertEquals('status', $view->filter['status_node']->field);
    $this->assertEquals('1', $view->filter['status_node']->value);

    
class ArgumentValidatorTest extends ViewsKernelTestBase {

  /** * 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 testResponsiveGrid(array $options, array $expected): void {
    // Create and preview a View with the provided options.     $view = Views::getView('test_grid_responsive');
    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $view->style_plugin->options = $options + $view->style_plugin->options;
    $this->executeView($view);
    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);

    // Confirm that the alignment class is added.     $result = $this->xpath('//div[contains(@class, "views-view-responsive-grid") and contains(@class, :alignment)]', [':alignment' => 'views-view-responsive-grid--' . $expected['alignment']]);
    $this->assertGreaterThan(0, count($result), "Alignment CSS variable value is detected and correct.");

    
// Test without the tar option.     $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      $this->assertEquals('png', $view->field['extension']->advancedRender($view->result[0]));
      $this->assertEquals('tar', $view->field['extension']->advancedRender($view->result[1]));
      $this->assertEquals('gz', $view->field['extension']->advancedRender($view->result[2]));
      $this->assertEquals('', $view->field['extension']->advancedRender($view->result[3]));
    });

    // Test with the tar option.     $view = Views::getView('file_extension_view');
    $view->setDisplay();
    $view->initHandlers();

    $view->field['extension']->options['settings']['extension_detect_tar'] = TRUE;
    $this->executeView($view);

    $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      $this->assertEquals('png', $view->field['extension']->advancedRender($view->result[0]));
      $this->assertEquals('tar', $view->field['extension']->advancedRender($view->result[1]));
      $this->assertEquals('tar.gz', $view->field['extension']->advancedRender($view->result[2]));
      $this->assertEquals('', $view->field['extension']->advancedRender($view->result[3]));
    });
  }

}

    $this->terms[] = $unique;
    $this->names[] = $unique->label();
    $this->ids[] = $unique->id();
  }

  /** * Tests the term name argument validator plugin. */
  public function testArgumentValidatorTermName() {
    $view = Views::getView('test_taxonomy_name_argument');
    $view->initHandlers();

    // Test with name that does not correspond to any term.     $this->assertFalse($view->argument['name']->setArgument('not a term name'));
    $view->argument['name']->validated_title = NULL;
    $view->argument['name']->argument_validated = NULL;

    // Test with term in the wrong vocabulary.     $this->assertFalse($view->argument['name']->setArgument($this->names[4]));
    $view->argument['name']->validated_title = NULL;
    $view->argument['name']->argument_validated = NULL;

    
$this->_testExact();
  }

  /** * Tests offset operations. */
  protected function _testOffset() {
    $view = Views::getView('test_filter_datetime');
    $field = static::$fieldName . '_value';

    // Test simple operations.     $view->initHandlers();

    $view->filter[$field]->operator = '>';
    $view->filter[$field]->value['type'] = 'offset';
    $view->filter[$field]->value['value'] = '+1 hour';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
      ['nid' => $this->nodes[3]->id()],
    ];
    $this->assertIdenticalResultset($view$expected_result$this->map);
    $view->destroy();

    
$this->_testFilterDateUI();
    $this->_testFilterDatetimeUI();
  }

  /** * Tests the general offset functionality. */
  protected function _testOffset() {
    $view = Views::getView('test_filter_date_between');

    // Test offset for simple operator.     $view->initHandlers();
    $view->filter['created']->operator = '>';
    $view->filter['created']->value['type'] = 'offset';
    $view->filter['created']->value['value'] = '+1 hour';
    $view->executeDisplay('default');
    $expected_result = [
      ['nid' => $this->nodes[3]->id()],
    ];
    $this->assertIdenticalResultset($view$expected_result$this->map);
    $view->destroy();

    // Test offset for between operator.
Home | Imprint | This part of the site doesn't use cookies.