3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\node\Entity\Node;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\RoleInterface;
11 use Drupal\views\Views;
14 * Tests the taxonomy term view page and its translation.
18 class TaxonomyTermViewTest extends TaxonomyTestBase {
25 public static $modules = ['taxonomy', 'views'];
28 * An user with permissions to administer taxonomy.
30 * @var \Drupal\user\UserInterface
35 * Name of the taxonomy term reference field.
39 protected $fieldName1;
44 protected function setUp($import_test_views = TRUE) {
45 parent::setUp($import_test_views);
47 // Create an administrative user.
48 $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'bypass node access']);
49 $this->drupalLogin($this->adminUser);
51 // Create a vocabulary and add two term reference fields to article nodes.
53 $this->fieldName1 = Unicode::strtolower($this->randomMachineName());
57 $this->vocabulary->id() => $this->vocabulary->id(),
59 'auto_create' => TRUE,
61 $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
63 entity_get_form_display('node', 'article', 'default')
64 ->setComponent($this->fieldName1, [
65 'type' => 'options_select',
68 entity_get_display('node', 'article', 'default')
69 ->setComponent($this->fieldName1, [
70 'type' => 'entity_reference_label',
76 * Tests that the taxonomy term view is working properly.
78 public function testTaxonomyTermView() {
79 // Create terms in the vocabulary.
80 $term = $this->createTerm();
84 $edit['title[0][value]'] = $original_title = $this->randomMachineName();
85 $edit['body[0][value]'] = $this->randomMachineName();
86 $edit["{$this->fieldName1}[]"] = $term->id();
87 $this->drupalPostForm('node/add/article', $edit, t('Save'));
88 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
90 $this->drupalGet('taxonomy/term/' . $term->id());
91 $this->assertText($term->label());
92 $this->assertText($node->label());
94 \Drupal::service('module_installer')->install(['language', 'content_translation']);
95 ConfigurableLanguage::createFromLangcode('ur')->save();
96 // Enable translation for the article content type and ensure the change is
98 \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
99 $roles = $this->adminUser->getRoles(TRUE);
100 Role::load(reset($roles))
101 ->grantPermission('create content translations')
102 ->grantPermission('translate any entity')
104 drupal_static_reset();
105 \Drupal::entityManager()->clearCachedDefinitions();
106 \Drupal::service('router.builder')->rebuild();
107 \Drupal::service('entity.definition_update_manager')->applyUpdates();
109 $edit['title[0][value]'] = $translated_title = $this->randomMachineName();
111 $this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
113 $this->drupalGet('taxonomy/term/' . $term->id());
114 $this->assertText($term->label());
115 $this->assertText($original_title);
116 $this->assertNoText($translated_title);
118 $this->drupalGet('ur/taxonomy/term/' . $term->id());
119 $this->assertText($term->label());
120 $this->assertNoText($original_title);
121 $this->assertText($translated_title);
123 // Uninstall language module and ensure that the language is not part of the
125 // @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
128 // We also have to remove the nodes created by the parent ::setUp() method
129 // if we want to be able to uninstall the Content Translation module.
130 foreach (Node::loadMultiple() as $node) {
133 \Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
135 $view = Views::getView('taxonomy_term');
136 $view->initDisplay();
137 $view->setArguments([$term->id()]);
139 /** @var \Drupal\Core\Database\Query\Select $query */
140 $query = $view->build_info['query'];
141 $tables = $query->getTables();
143 // Ensure that the join to node_field_data is not added by default.
144 $this->assertEqual(['node_field_data', 'taxonomy_index'], array_keys($tables));
145 // Ensure that the filter to the language column is not there by default.
146 $condition = $query->conditions();
147 // We only want to check the no. of conditions in the query.
148 unset($condition['#conjunction']);
149 $this->assertEqual(1, count($condition));
151 // Clear permissions for anonymous users to check access for default views.
152 Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
154 // Test the default views disclose no data by default.
155 $this->drupalLogout();
156 $this->drupalGet('taxonomy/term/' . $term->id());
157 $this->assertResponse(403);
158 $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
159 $this->assertResponse(403);