View source
<?php
namespace Drupal\Tests\taxonomy\Functional\Views;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
class TaxonomyTermViewTest extends TaxonomyTestBase {
protected static $modules = [
'taxonomy',
'views',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $fieldName1;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->adminUser = $this
->drupalCreateUser([
'administer taxonomy',
'bypass node access',
]);
$this
->drupalLogin($this->adminUser);
$this->fieldName1 = mb_strtolower($this
->randomMachineName());
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->fieldName1, [
'type' => 'options_select',
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent($this->fieldName1, [
'type' => 'entity_reference_label',
])
->save();
}
public function testTaxonomyTermView() {
$term = $this
->createTerm();
$edit = [];
$edit['title[0][value]'] = $original_title = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$edit["{$this->fieldName1}[]"] = $term
->id();
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->drupalGet('taxonomy/term/' . $term
->id());
$this
->assertSession()
->pageTextContains($term
->label());
$this
->assertSession()
->pageTextContains($node
->label());
\Drupal::service('module_installer')
->install([
'language',
'content_translation',
]);
ConfigurableLanguage::createFromLangcode('ur')
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$roles = $this->adminUser
->getRoles(TRUE);
Role::load(reset($roles))
->grantPermission('create content translations')
->grantPermission('translate any entity')
->save();
$edit['title[0][value]'] = $translated_title = $this
->randomMachineName();
$this
->drupalGet('node/' . $node
->id() . '/translations/add/en/ur');
$this
->submitForm($edit, 'Save (this translation)');
$this
->drupalGet('taxonomy/term/' . $term
->id());
$this
->assertSession()
->pageTextContains($term
->label());
$this
->assertSession()
->pageTextContains($original_title);
$this
->assertSession()
->pageTextNotContains($translated_title);
$this
->drupalGet('ur/taxonomy/term/' . $term
->id());
$this
->assertSession()
->pageTextContains($term
->label());
$this
->assertSession()
->pageTextNotContains($original_title);
$this
->assertSession()
->pageTextContains($translated_title);
$node
->delete();
foreach (Node::loadMultiple() as $node) {
$node
->delete();
}
\Drupal::service('module_installer')
->uninstall([
'content_translation',
'language',
]);
$view = Views::getView('taxonomy_term');
$view
->initDisplay();
$view
->setArguments([
$term
->id(),
]);
$view
->build();
$query = $view->build_info['query'];
$tables = $query
->getTables();
$this
->assertEquals([
'node_field_data',
'taxonomy_index',
], array_keys($tables));
$condition = $query
->conditions();
unset($condition['#conjunction']);
$this
->assertCount(1, $condition);
Role::load(RoleInterface::ANONYMOUS_ID)
->revokePermission('access content')
->save();
$this
->drupalLogout();
$this
->drupalGet('taxonomy/term/' . $term
->id());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('taxonomy/term/' . $term
->id() . '/feed');
$this
->assertSession()
->statusCodeEquals(403);
}
}