You are here

public function Update8202Test::testUpdateHook in Taxonomy access fix 8.2

Tests that update hook 8202 is properly run.

Overrides UpdateTestBase::testUpdateHook

File

tests/src/Functional/Update/Update8202Test.php, line 25

Class

Update8202Test
Tests update hook 8202.

Namespace

Drupal\Tests\taxonomy_access_fix\Functional\Update

Code

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

  // Create vocabularies.
  $vocabularies = [
    $this
      ->createVocabulary(),
    $this
      ->createVocabulary(),
  ];

  // Create roles.
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user_role');
  $roles_create_access = [
    'administrator' => [
      $vocabularies[0]
        ->id() => TRUE,
      $vocabularies[1]
        ->id() => TRUE,
    ],
    'first_vocabulary' => [
      $vocabularies[0]
        ->id() => TRUE,
      $vocabularies[1]
        ->id() => FALSE,
    ],
    'second_vocabulary' => [
      $vocabularies[0]
        ->id() => FALSE,
      $vocabularies[1]
        ->id() => TRUE,
    ],
    'no_permissions' => [
      $vocabularies[0]
        ->id() => FALSE,
      $vocabularies[1]
        ->id() => FALSE,
    ],
  ];
  foreach ($roles_create_access as $role_id => $create_access) {
    $role = $storage
      ->create([
      'id' => $role_id,
      'label' => $role_id,
      'is_admin' => $role_id === 'administrator',
    ]);
    foreach ($create_access as $vocabulary_id => $has_access) {
      if (!$has_access) {
        continue;
      }
      $role
        ->grantPermission('add terms in ' . $vocabulary_id);
    }
    $role
      ->save();
  }
  $storage
    ->resetCache();

  // Assert expected permissions.
  $roles = $storage
    ->loadMultiple();
  foreach ($roles_create_access as $role_id => $create_access) {
    foreach ($create_access as $vocabulary_id => $has_access) {
      if ($has_access) {
        $this
          ->assertTrue($roles[$role_id]
          ->hasPermission('add terms in ' . $vocabulary_id), new FormattableMarkup('@role_id has permission to add terms in @vocabulary_id', [
          '@role_id' => $role_id,
          '@vocabulary_id' => $vocabulary_id,
        ]));
      }
      else {
        $this
          ->assertFalse($roles[$role_id]
          ->hasPermission('add terms in ' . $vocabulary_id), new FormattableMarkup('@role_id has no permission to add terms in @vocabulary_id', [
          '@role_id' => $role_id,
          '@vocabulary_id' => $vocabulary_id,
        ]));
      }
    }
  }

  // Run update.
  $this
    ->runUpdate(8201, 8202, "Migrate from 'Add terms in %vocabulary' permission to Drupal Core permission.");
  $storage
    ->resetCache();

  // Assert expected permissions.
  $roles = $storage
    ->loadMultiple();
  foreach ($roles_create_access as $role_id => $create_access) {
    foreach ($create_access as $vocabulary_id => $has_access) {
      if ($has_access) {
        $this
          ->assertTrue($roles[$role_id]
          ->hasPermission('create terms in ' . $vocabulary_id), new FormattableMarkup('@role_id has permission to create terms in @vocabulary_id', [
          '@role_id' => $role_id,
          '@vocabulary_id' => $vocabulary_id,
        ]));
      }
      else {
        $this
          ->assertFalse($roles[$role_id]
          ->hasPermission('create terms in ' . $vocabulary_id), new FormattableMarkup('@role_id has no permission to create terms in @vocabulary_id', [
          '@role_id' => $role_id,
          '@vocabulary_id' => $vocabulary_id,
        ]));
      }
      if ($roles[$role_id]
        ->isAdmin()) {
        continue;
      }
      $this
        ->assertFalse($roles[$role_id]
        ->hasPermission('add terms in ' . $vocabulary_id), new FormattableMarkup('@role_id has no permission to add terms in @vocabulary_id', [
        '@role_id' => $role_id,
        '@vocabulary_id' => $vocabulary_id,
      ]));
    }
  }
}