You are here

public function VocabularyAccessTest::testTaxonomyVocabularyOverview in Taxonomy access fix 8.2

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

Tests access to Taxonomy Vocabulary overview page.

File

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

Class

VocabularyAccessTest
Tests administrative Taxonomy UI access.

Namespace

Drupal\Tests\taxonomy_access_fix\Functional

Code

public function testTaxonomyVocabularyOverview() {
  $assert_session = $this
    ->assertSession();
  $published_terms = [];
  $unpublished_terms = [];
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $published_terms[$delta] = $this
      ->createTerm($vocabulary, [
      'name' => 'Published term',
      'status' => 1,
    ]);
    $published_terms[$delta]
      ->save();
    $unpublished_terms[$delta] = $this
      ->createTerm($vocabulary, [
      'name' => 'Unpublished term',
      'status' => 0,
    ]);
    $unpublished_terms[$delta]
      ->save();
  }

  // Test the 'administer taxonomy' permission.
  $this
    ->drupalLogin($this->users['administer']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(200);
    $this
      ->assertElementByCssSelector('#edit-reset-alphabetical');
    $this
      ->assertSortableTable();
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
      'taxonomy_term' => $published_terms[$delta]
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString(),
    ])
      ->toString());
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
      'taxonomy_term' => $published_terms[$delta]
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString(),
    ])
      ->toString());
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
      'taxonomy_term' => $unpublished_terms[$delta]
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString(),
    ])
      ->toString());
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
      'taxonomy_term' => $unpublished_terms[$delta]
        ->id(),
    ])
      ->setOption('query', [
      'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString(),
    ])
      ->toString());
    $assert_session
      ->pageTextContains(t('Add term'));
    $this
      ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $vocabulary
        ->id(),
    ])
      ->toString());
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(200);
  }

  // Test the 'access taxonomy overview' permission.
  $this
    ->drupalLogin($this->users['overview']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }

  // Test the per vocabulary 'create terms in' permission.
  $this
    ->drupalLogin($this->users['create_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
  $this
    ->drupalLogin($this->users['overview_and_create_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    if ($delta === 0) {
      $assert_session
        ->statusCodeEquals(200);
      $this
        ->assertNoElementByCssSelector('#edit-reset-alphabetical');
      $this
        ->assertNoSortableTable();
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $assert_session
        ->pageTextContains(t('Add term'));
      $this
        ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString());
    }
    else {
      $assert_session
        ->statusCodeEquals(403);
    }
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }

  // Test the per vocabulary 'edit terms in' permission.
  $this
    ->drupalLogin($this->users['update_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
  $this
    ->drupalLogin($this->users['overview_and_update_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    if ($delta === 0) {
      $assert_session
        ->statusCodeEquals(200);
      $this
        ->assertNoElementByCssSelector('#edit-reset-alphabetical');
      $this
        ->assertNoSortableTable();
      $this
        ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $assert_session
        ->pageTextNotContains(t('Add term'));
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString());
    }
    else {
      $assert_session
        ->statusCodeEquals(403);
    }
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }

  // Test the per vocabulary 'delete terms in' permission.
  $this
    ->drupalLogin($this->users['delete_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
  $this
    ->drupalLogin($this->users['overview_and_delete_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    if ($delta === 0) {
      $assert_session
        ->statusCodeEquals(200);
      $this
        ->assertNoElementByCssSelector('#edit-reset-alphabetical');
      $this
        ->assertNoSortableTable();
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $assert_session
        ->pageTextNotContains(t('Add term'));
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString());
    }
    else {
      $assert_session
        ->statusCodeEquals(403);
    }
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }

  // Test the per vocabulary 'reorder terms in' permission.
  $this
    ->drupalLogin($this->users['reorder_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
  $this
    ->drupalLogin($this->users['overview_and_reorder_first_vocabulary']);
  foreach ($this->vocabularies as $delta => $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    if ($delta === 0) {
      $assert_session
        ->statusCodeEquals(200);

      // @todo: This fails, but should be available, since it related to
      // reordering terms and the route is also available. Fix and enable.
      // $this->assertElementByCssSelector('#edit-reset-alphabetical');
      // @todo: Enable once Issue 2958658 has been fixed.
      // $this->assertSortableTable();
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $published_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.edit_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.delete_form', [
        'taxonomy_term' => $unpublished_terms[$delta]
          ->id(),
      ])
        ->setOption('query', [
        'destination' => Url::fromRoute('entity.taxonomy_vocabulary.overview_form', [
          'taxonomy_vocabulary' => $vocabulary
            ->id(),
        ])
          ->toString(),
      ])
        ->toString());
      $assert_session
        ->pageTextNotContains(t('Add term'));
      $this
        ->assertNoLinkByEndOfHref(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $vocabulary
          ->id(),
      ])
        ->toString());
    }
    else {
      $assert_session
        ->statusCodeEquals(403);
    }
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
  $this
    ->drupalLogin($this->users['view_first_vocabulary']);
  foreach ($this->vocabularies as $vocabulary) {
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/overview');
    $assert_session
      ->statusCodeEquals(403);
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/reset');
    $assert_session
      ->statusCodeEquals(403);
  }
}