You are here

public function VocabularyAccessTest::testTaxonomyVocabularyCollection in Taxonomy access fix 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/VocabularyAccessTest.php \Drupal\Tests\taxonomy_access_fix\Functional\VocabularyAccessTest::testTaxonomyVocabularyCollection()

Tests access to administrative Taxonomy Vocabulary collection.

File

tests/src/Functional/VocabularyAccessTest.php, line 105

Class

VocabularyAccessTest
Tests administrative Taxonomy UI access.

Namespace

Drupal\Tests\taxonomy_access_fix\Functional

Code

public function testTaxonomyVocabularyCollection() {
  $assert_session = $this
    ->assertSession();

  // Test the 'administer taxonomy' permission.
  $this
    ->drupalLogin($this->users['administer']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains(t('Add vocabulary'));
  $this
    ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertSortableTable(FALSE);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $assert_session
      ->pageTextContains($vocabulary
      ->label());
    $assert_session
      ->pageTextContains($vocabulary
      ->getDescription());
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $add_terms_url = Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    $this
      ->assertLinkByEndOfHref($add_terms_url);
  }

  // Test the 'access taxonomy overview' permission.
  $this
    ->drupalLogin($this->users['overview']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  $assert_session
    ->pageTextContains(t('No vocabularies available.'));
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $assert_session
      ->pageTextNotContains($vocabulary
      ->label());
    $assert_session
      ->pageTextNotContains($vocabulary
      ->getDescription());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
  }

  // Test the per vocabulary 'view terms in' permission.
  $this
    ->drupalLogin($this->users['view_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->users['overview_and_view_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  $assert_session
    ->pageTextContains(t('No vocabularies available.'));
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $assert_session
      ->pageTextNotContains($vocabulary
      ->label());
    $assert_session
      ->pageTextNotContains($vocabulary
      ->getDescription());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
  }

  // Test the per vocabulary 'create terms in' permission.
  $this
    ->drupalLogin($this->users['create_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->users['overview_and_create_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $overview_url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    $add_terms_url = Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    if ($delta === 0) {
      $assert_session
        ->pageTextContains($vocabulary
        ->label());
      $assert_session
        ->pageTextContains($vocabulary
        ->getDescription());
      $this
        ->assertLinkByEndOfHref($overview_url);
      $this
        ->assertLinkByEndOfHref($add_terms_url);
    }
    else {
      $assert_session
        ->pageTextNotContains($vocabulary
        ->label());
      $assert_session
        ->pageTextNotContains($vocabulary
        ->getDescription());
      $this
        ->assertNoLinkByEndOfHref($overview_url);
      $this
        ->assertNoLinkByEndOfHref($add_terms_url);
    }
  }

  // Test the per vocabulary 'edit terms in' permission.
  $this
    ->drupalLogin($this->users['update_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->users['overview_and_update_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $overview_url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    if ($delta === 0) {
      $assert_session
        ->pageTextContains($vocabulary
        ->label());
      $assert_session
        ->pageTextContains($vocabulary
        ->getDescription());
      $this
        ->assertLinkByEndOfHref($overview_url);
    }
    else {
      $assert_session
        ->pageTextNotContains($vocabulary
        ->label());
      $assert_session
        ->pageTextNotContains($vocabulary
        ->getDescription());
      $this
        ->assertNoLinkByEndOfHref($overview_url);
    }
  }

  // Test the per vocabulary 'delete terms in' permission.
  $this
    ->drupalLogin($this->users['delete_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->users['overview_and_delete_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $overview_url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    if ($delta === 0) {
      $assert_session
        ->pageTextContains($vocabulary
        ->label());
      $assert_session
        ->pageTextContains($vocabulary
        ->getDescription());
      $this
        ->assertLinkByEndOfHref($overview_url);
    }
    else {
      $assert_session
        ->pageTextNotContains($vocabulary
        ->label());
      $assert_session
        ->pageTextNotContains($vocabulary
        ->getDescription());
      $this
        ->assertNoLinkByEndOfHref($overview_url);
    }
  }

  // Test the per vocabulary 'reorder terms in' permission.
  $this
    ->drupalLogin($this->users['reorder_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->users['overview_and_reorder_first_vocabulary']);
  $this
    ->drupalGet('admin/structure/taxonomy');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains(t('Add vocabulary'));
  $this
    ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.add_form')
    ->toString());
  $this
    ->assertNoSortableTable(FALSE);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
        ->toString(),
    ])
      ->toString());
    $this
      ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $overview_url = Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString();
    if ($delta === 0) {
      $assert_session
        ->pageTextContains($vocabulary
        ->label());
      $assert_session
        ->pageTextContains($vocabulary
        ->getDescription());
      $this
        ->assertLinkByEndOfHref($overview_url);
    }
    else {
      $assert_session
        ->pageTextNotContains($vocabulary
        ->label());
      $assert_session
        ->pageTextNotContains($vocabulary
        ->getDescription());
      $this
        ->assertNoLinkByEndOfHref($overview_url);
    }
  }
}