You are here

public function HandlerTest::testRelationshipUI in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/HandlerTest.php \Drupal\views\Tests\Handler\HandlerTest::testRelationshipUI()

Tests the relationship ui for field/filter/argument/relationship.

File

core/modules/views/src/Tests/Handler/HandlerTest.php, line 219
Contains \Drupal\views\Tests\Handler\HandlerTest.

Class

HandlerTest
Tests abstract handler definitions.

Namespace

Drupal\views\Tests\Handler

Code

public function testRelationshipUI() {
  $views_admin = $this
    ->drupalCreateUser(array(
    'administer views',
  ));
  $this
    ->drupalLogin($views_admin);

  // Make sure the link to the field options exists.
  $handler_options_path = 'admin/structure/views/nojs/handler/test_handler_relationships/default/field/title';
  $view_edit_path = 'admin/structure/views/view/test_handler_relationships/edit';
  $this
    ->drupalGet($view_edit_path);
  $this
    ->assertLinkByHref($handler_options_path);

  // The test view has a relationship to node_revision so the field should
  // show a relationship selection.
  $this
    ->drupalGet($handler_options_path);
  $relationship_name = 'options[relationship]';
  $this
    ->assertFieldByName($relationship_name);

  // Check for available options.
  $xpath = $this
    ->constructFieldXpath('name', $relationship_name);
  $fields = $this
    ->xpath($xpath);
  $options = array();
  foreach ($fields as $field) {
    $items = $this
      ->getAllOptions($field);
    foreach ($items as $item) {
      $options[] = $item
        ->attributes()->value;
    }
  }
  $expected_options = array(
    'none',
    'nid',
  );
  $this
    ->assertEqual($options, $expected_options);

  // Remove the relationship and make sure no relationship option appears.
  $this
    ->drupalPostForm('admin/structure/views/nojs/handler/test_handler_relationships/default/relationship/nid', array(), t('Remove'));
  $this
    ->drupalGet($handler_options_path);
  $this
    ->assertNoFieldByName($relationship_name, NULL, 'Make sure that no relationship option is available');

  // Create a view of comments with node relationship.
  View::create([
    'base_table' => 'comment_field_data',
    'id' => 'test_get_entity_type',
  ])
    ->save();
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/relationship', [
    'name[comment_field_data.node]' => 'comment_field_data.node',
  ], t('Add and configure relationships'));
  $this
    ->drupalPostForm(NULL, [], t('Apply'));

  // Add a content type filter.
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/filter', [
    'name[node_field_data.type]' => 'node_field_data.type',
  ], t('Add and configure filter criteria'));
  $this
    ->assertOptionSelected('edit-options-relationship', 'node');
  $this
    ->drupalPostForm(NULL, [
    'options[value][page]' => 'page',
  ], t('Apply'));

  // Check content type filter options.
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_get_entity_type/default/filter/type');
  $this
    ->assertOptionSelected('edit-options-relationship', 'node');
  $this
    ->assertFieldChecked('edit-options-value-page');
}