Backup of db before drupal security update
[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\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 = Unicode::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     $language = ConfigurableLanguage::createFromLangcode('ur');
95     $language->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     \Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
128
129     $view = Views::getView('taxonomy_term');
130     $view->initDisplay();
131     $view->setArguments([$term->id()]);
132     $view->build();
133     /** @var \Drupal\Core\Database\Query\Select $query */
134     $query = $view->build_info['query'];
135     $tables = $query->getTables();
136
137     // Ensure that the join to node_field_data is not added by default.
138     $this->assertEqual(['node_field_data', 'taxonomy_index'], array_keys($tables));
139     // Ensure that the filter to the language column is not there by default.
140     $condition = $query->conditions();
141     // We only want to check the no. of conditions in the query.
142     unset($condition['#conjunction']);
143     $this->assertEqual(1, count($condition));
144
145     // Clear permissions for anonymous users to check access for default views.
146     Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
147
148     // Test the default views disclose no data by default.
149     $this->drupalLogout();
150     $this->drupalGet('taxonomy/term/' . $term->id());
151     $this->assertResponse(403);
152     $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
153     $this->assertResponse(403);
154   }
155
156 }