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