Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Views / TaxonomyTermViewTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Views;
4
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;
11
12 /**
13  * Tests the taxonomy term view page and its translation.
14  *
15  * @group taxonomy
16  */
17 class TaxonomyTermViewTest extends TaxonomyTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['taxonomy', 'views'];
25
26   /**
27    * An user with permissions to administer taxonomy.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $adminUser;
32
33   /**
34    * Name of the taxonomy term reference field.
35    *
36    * @var string
37    */
38   protected $fieldName1;
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp($import_test_views = TRUE) {
44     parent::setUp($import_test_views);
45
46     // Create an administrative user.
47     $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'bypass node access']);
48     $this->drupalLogin($this->adminUser);
49
50     // Create a vocabulary and add two term reference fields to article nodes.
51
52     $this->fieldName1 = mb_strtolower($this->randomMachineName());
53
54     $handler_settings = [
55       'target_bundles' => [
56         $this->vocabulary->id() => $this->vocabulary->id(),
57       ],
58       'auto_create' => TRUE,
59     ];
60     $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
61
62     entity_get_form_display('node', 'article', 'default')
63       ->setComponent($this->fieldName1, [
64         'type' => 'options_select',
65       ])
66       ->save();
67     entity_get_display('node', 'article', 'default')
68       ->setComponent($this->fieldName1, [
69         'type' => 'entity_reference_label',
70       ])
71       ->save();
72   }
73
74   /**
75    * Tests that the taxonomy term view is working properly.
76    */
77   public function testTaxonomyTermView() {
78     // Create terms in the vocabulary.
79     $term = $this->createTerm();
80
81     // Post an article.
82     $edit = [];
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]']);
88
89     $this->drupalGet('taxonomy/term/' . $term->id());
90     $this->assertText($term->label());
91     $this->assertText($node->label());
92
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
96     // picked up.
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')
102       ->save();
103     drupal_static_reset();
104     \Drupal::entityManager()->clearCachedDefinitions();
105     \Drupal::service('router.builder')->rebuild();
106     \Drupal::service('entity.definition_update_manager')->applyUpdates();
107
108     $edit['title[0][value]'] = $translated_title = $this->randomMachineName();
109
110     $this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
111
112     $this->drupalGet('taxonomy/term/' . $term->id());
113     $this->assertText($term->label());
114     $this->assertText($original_title);
115     $this->assertNoText($translated_title);
116
117     $this->drupalGet('ur/taxonomy/term/' . $term->id());
118     $this->assertText($term->label());
119     $this->assertNoText($original_title);
120     $this->assertText($translated_title);
121
122     // Uninstall language module and ensure that the language is not part of the
123     // query anymore.
124     // @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
125     $node->delete();
126
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) {
130       $node->delete();
131     }
132     \Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
133
134     $view = Views::getView('taxonomy_term');
135     $view->initDisplay();
136     $view->setArguments([$term->id()]);
137     $view->build();
138     /** @var \Drupal\Core\Database\Query\Select $query */
139     $query = $view->build_info['query'];
140     $tables = $query->getTables();
141
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));
149
150     // Clear permissions for anonymous users to check access for default views.
151     Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
152
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);
159   }
160
161 }