You are here

public function TaxonomyTermViewTest::testTaxonomyTermView in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php \Drupal\Tests\taxonomy\Functional\Views\TaxonomyTermViewTest::testTaxonomyTermView()
  2. 9 core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php \Drupal\Tests\taxonomy\Functional\Views\TaxonomyTermViewTest::testTaxonomyTermView()

Tests that the taxonomy term view is working properly.

File

core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php, line 87

Class

TaxonomyTermViewTest
Tests the taxonomy term view page and its translation.

Namespace

Drupal\Tests\taxonomy\Functional\Views

Code

public function testTaxonomyTermView() {

  // Create terms in the vocabulary.
  $term = $this
    ->createTerm();

  // Post an article.
  $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();

  // Enable translation for the article content type and ensure the change is
  // picked up.
  \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);

  // Uninstall language module and ensure that the language is not part of the
  // query anymore.
  // @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
  $node
    ->delete();

  // We also have to remove the nodes created by the parent ::setUp() method
  // if we want to be able to uninstall the Content Translation module.
  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();

  /** @var \Drupal\Core\Database\Query\Select $query */
  $query = $view->build_info['query'];
  $tables = $query
    ->getTables();

  // Ensure that the join to node_field_data is not added by default.
  $this
    ->assertEquals([
    'node_field_data',
    'taxonomy_index',
  ], array_keys($tables));

  // Ensure that the filter to the language column is not there by default.
  $condition = $query
    ->conditions();

  // We only want to check the no. of conditions in the query.
  unset($condition['#conjunction']);
  $this
    ->assertCount(1, $condition);

  // Clear permissions for anonymous users to check access for default views.
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->revokePermission('access content')
    ->save();

  // Test the default views disclose no data by default.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/feed');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}