RelationshipNodeTermDataTest.php 3.63 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Functional\Views;
4

5
use Drupal\views\Views;
6
use Drupal\views\ViewExecutable;
7

8
/**
9 10 11
 * Tests the taxonomy term on node relationship handler.
 *
 * @group taxonomy
12
 */
13
class RelationshipNodeTermDataTest extends TaxonomyTestBase {
14

15
  /**
16
   * Views used by this test.
17
   *
18
   * @var array
19
   */
20
  public static $testViews = ['test_taxonomy_node_term_data'];
21

22 23 24 25 26
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

27
  public function testViewsHandlerRelationshipNodeTermData() {
28
    $view = Views::getView('test_taxonomy_node_term_data');
29 30 31 32 33 34 35 36 37
    // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().
    $expected = [
      'config' => ['core.entity_view_mode.node.teaser'],
      'module' => [
        'node',
        'taxonomy',
        'user',
      ],
    ];
38
    $this->assertIdentical($expected, $view->getDependencies());
39 40 41
    $this->executeView($view, [$this->term1->id(), $this->term2->id()]);
    $expected_result = [
      [
42
        'nid' => $this->nodes[1]->id(),
43 44
      ],
      [
45
        'nid' => $this->nodes[0]->id(),
46 47 48
      ],
    ];
    $column_map = ['nid' => 'nid'];
49 50 51
    $this->assertIdenticalResultset($view, $expected_result, $column_map);

    // Change the view to test relation limited by vocabulary.
52
    $this->config('views.view.test_taxonomy_node_term_data')
53
      ->set('display.default.display_options.relationships.term_node_tid.vids', ['views_testing_tags'])
54 55 56
      ->save();

    $view = Views::getView('test_taxonomy_node_term_data');
57 58
    // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().
    $expected['config'][] = 'taxonomy.vocabulary.views_testing_tags';
59
    $this->assertIdentical($expected, $view->getDependencies());
60
    $this->executeView($view, [$this->term1->id(), $this->term2->id()]);
61
    $this->assertIdenticalResultset($view, $expected_result, $column_map);
62
  }
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  /**
   * Tests that the 'taxonomy_term_access' tag is added to the Views query.
   */
  public function testTag() {
    // Change the view to test relation limited by vocabulary.
    $this->config('views.view.test_taxonomy_node_term_data')
      ->set('display.default.display_options.relationships.term_node_tid.vids', ['views_testing_tags'])
      ->save();
    $view = Views::getView('test_taxonomy_node_term_data');
    $this->executeView($view, [$this->term1->id()]);

    // By default, view has taxonomy_term_access tag.
    $this->assertQueriesTermAccessTag($view, TRUE);

    // The term_access tag is not set if disable_sql_rewrite is set.
    $view = Views::getView('test_taxonomy_node_term_data');
    $display = $view->getDisplay();
    $display_options = $display->getOption('query');
    $display_options['options']['disable_sql_rewrite'] = TRUE;
    $display->setOption('query', $display_options);
    $view->save();
    $this->executeView($view, [$this->term1->id()]);

    $this->assertQueriesTermAccessTag($view, FALSE);
  }

  /**
   * Assert views queries have taxonomy_term_access tag.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   The View to check for the term access tag.
   * @param bool $hasTag
   *   The expected existence of taxonomy_term_access tag.
   */
  protected function assertQueriesTermAccessTag(ViewExecutable $view, $hasTag) {
    $main_query = $view->build_info['query'];
    $count_query = $view->build_info['count_query'];

    foreach ([$main_query, $count_query] as $query) {
      $tables = $query->getTables();
      foreach ($tables as $join_table) {
        if (is_object($join_table['table'])) {
          $this->assertSame($join_table['table']->hasTag('taxonomy_term_access'), $hasTag);
        }
      }
    }
  }

112
}