3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\RoleInterface;
10 use Drupal\views\Views;
13 * Tests the taxonomy term view page and its translation.
17 class TaxonomyTermViewTest extends TaxonomyTestBase {
24 public static $modules = ['taxonomy', 'views'];
27 * An user with permissions to administer taxonomy.
29 * @var \Drupal\user\UserInterface
34 * Name of the taxonomy term reference field.
38 protected $fieldName1;
43 protected function setUp($import_test_views = TRUE) {
44 parent::setUp($import_test_views);
46 // Create an administrative user.
47 $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'bypass node access']);
48 $this->drupalLogin($this->adminUser);
50 // Create a vocabulary and add two term reference fields to article nodes.
52 $this->fieldName1 = mb_strtolower($this->randomMachineName());
56 $this->vocabulary->id() => $this->vocabulary->id(),
58 'auto_create' => TRUE,
60 $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
62 entity_get_form_display('node', 'article', 'default')
63 ->setComponent($this->fieldName1, [
64 'type' => 'options_select',
67 entity_get_display('node', 'article', 'default')
68 ->setComponent($this->fieldName1, [
69 'type' => 'entity_reference_label',
75 * Tests that the taxonomy term view is working properly.
77 public function testTaxonomyTermView() {
78 // Create terms in the vocabulary.
79 $term = $this->createTerm();
83 $edit['title[0][value]'] = $original_title = $this->randomMachineName();
84 $edit['body[0][value]'] = $this->randomMachineName();
85 $edit["{$this->fieldName1}[]"] = $term->id();
86 $this->drupalPostForm('node/add/article', $edit, t('Save'));
87 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
89 $this->drupalGet('taxonomy/term/' . $term->id());
90 $this->assertText($term->label());
91 $this->assertText($node->label());
93 \Drupal::service('module_installer')->install(['language', 'content_translation']);
94 ConfigurableLanguage::createFromLangcode('ur')->save();
95 // Enable translation for the article content type and ensure the change is
97 \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
98 $roles = $this->adminUser->getRoles(TRUE);
99 Role::load(reset($roles))
100 ->grantPermission('create content translations')
101 ->grantPermission('translate any entity')
103 drupal_static_reset();
104 \Drupal::entityManager()->clearCachedDefinitions();
105 \Drupal::service('router.builder')->rebuild();
106 \Drupal::service('entity.definition_update_manager')->applyUpdates();
108 $edit['title[0][value]'] = $translated_title = $this->randomMachineName();
110 $this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
112 $this->drupalGet('taxonomy/term/' . $term->id());
113 $this->assertText($term->label());
114 $this->assertText($original_title);
115 $this->assertNoText($translated_title);
117 $this->drupalGet('ur/taxonomy/term/' . $term->id());
118 $this->assertText($term->label());
119 $this->assertNoText($original_title);
120 $this->assertText($translated_title);
122 // Uninstall language module and ensure that the language is not part of the
124 // @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
127 // We also have to remove the nodes created by the parent ::setUp() method
128 // if we want to be able to uninstall the Content Translation module.
129 foreach (Node::loadMultiple() as $node) {
132 \Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
134 $view = Views::getView('taxonomy_term');
135 $view->initDisplay();
136 $view->setArguments([$term->id()]);
138 /** @var \Drupal\Core\Database\Query\Select $query */
139 $query = $view->build_info['query'];
140 $tables = $query->getTables();
142 // Ensure that the join to node_field_data is not added by default.
143 $this->assertEqual(['node_field_data', 'taxonomy_index'], array_keys($tables));
144 // Ensure that the filter to the language column is not there by default.
145 $condition = $query->conditions();
146 // We only want to check the no. of conditions in the query.
147 unset($condition['#conjunction']);
148 $this->assertEqual(1, count($condition));
150 // Clear permissions for anonymous users to check access for default views.
151 Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
153 // Test the default views disclose no data by default.
154 $this->drupalLogout();
155 $this->drupalGet('taxonomy/term/' . $term->id());
156 $this->assertResponse(403);
157 $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
158 $this->assertResponse(403);