entity_type_access_conditions-1.0.1/tests/src/Kernel/EntityTypeAccessTestBase.php

tests/src/Kernel/EntityTypeAccessTestBase.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\entity_type_access_conditions\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\entity_type_access_conditions\Traits\EntityTypeAccessTestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\Role;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\user\Entity\User;

/**
 * Base class for entity type access condition kernel tests.
 */
abstract class EntityTypeAccessTestBase extends KernelTestBase {

  use EntityTypeAccessTestTrait;
  use UserCreationTrait;

  /**
   * The access handler.
   *
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
   */
  protected $accessHandler;

  /**
   * The test user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $testUser;

  /**
   * The test role.
   *
   * @var \Drupal\user\RoleInterface
   */
  protected $testRole;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'system',
    'user',
    'field',
    'text',
    'file',
    'image',
    'node',
    'media',
    'media_test_source',
    'taxonomy',
    'entity_type_access_conditions',
    'entity_type_access_conditions_test',
    'conditions_helper',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installEntitySchema('file');
    $this->installEntitySchema('media');
    $this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('taxonomy_vocabulary');
    $this->installSchema('file', 'file_usage');
    $this->installConfig([
      'system', 'user', 'field', 'file', 'node',
      'media', 'taxonomy', 'entity_type_access_conditions',
    ]);

    // Create a test role.
    $this->createRole([], 'entity_tester_role');
    $this->testRole = Role::load('entity_tester_role');
    // Ensure the bypass permission is NOT granted.
    $this->assertFalse($this->testRole->hasPermission('bypass entity type access conditions'), 'Test role should not have bypass permission initially.');

    // Create a user 1.
    $this->createUser([], NULL, TRUE);
    // Create a test user and assign to the test role.
    $this->testUser = $this->createUser([], NULL, FALSE, ['roles' => [$this->testRole->id()]]);
    $this->setCurrentUser($this->testUser);
  }

  /**
   * Grants permissions to the test role.
   *
   * @param string[] $permissions
   *   An array of permission strings.
   */
  protected function grantPermissionsToTestRole(array $permissions): void {
    $role = Role::load($this->testRole->id());
    foreach ($permissions as $permission) {
      $role = $role->grantPermission($permission);
    }
    $role->save();
    // Re-load the user entity to ensure its permissions are updated.
    $this->testUser = User::load($this->testUser->id());
    // Ensures account in \Drupal::currentUser() is fresh.
    $this->setCurrentUser($this->testUser);
  }

  /**
   * Revokes permissions from the test role.
   *
   * @param string[] $permissions
   *   An array of permission strings.
   */
  protected function revokePermissionsFromTestRole(array $permissions): void {
    $role = Role::load($this->testRole->id());
    foreach ($permissions as $permission) {
      $role->revokePermission($permission);
    }
    $role->save();
    // Re-load the user entity to ensure its permissions are updated.
    $this->testUser = User::load($this->testUser->id());
    $this->setCurrentUser($this->testUser);
  }

  /**
   * Applies the test condition to an entity type's configuration.
   *
   * @param string $entity_type_id
   *   The entity type ID of the config entity (e.g., 'node_type', 'media_type',
   *   'taxonomy_vocabulary').
   * @param string $bundle_id
   *   The bundle ID (config entity ID).
   * @param string $condition_plugin_id
   *   The ID of the condition plugin to apply.
   * @param array $condition_config
   *   The configuration for the condition plugin (excluding the 'id').
   */
  protected function applyConditionToEntityTypeConfig(string $entity_type_id, string $bundle_id, string $condition_plugin_id = 'test_condition', array $condition_config = []): void {
    $config_entity_storage = $this->container->get('entity_type.manager')->getStorage($entity_type_id);
    $config_entity = $config_entity_storage->load($bundle_id);

    if (!$config_entity instanceof ConfigEntityInterface) {
      $this->fail("Config entity {$entity_type_id}.{$bundle_id} not found or not a ConfigEntityInterface.");
      return;
    }

    $condition_to_apply = ['id' => $condition_plugin_id] + $condition_config;

    $config_entity->setThirdPartySetting('entity_type_access_conditions', $condition_plugin_id, $condition_to_apply);
    $config_entity->save();
  }

  /**
   * Clears all conditions from an entity type's configuration.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   * @param string $bundle_id
   *   The bundle ID.
   */
  protected function clearConditionsFromEntityTypeConfig(string $entity_type_id, string $bundle_id): void {
    $config_entity_storage = $this->container->get('entity_type.manager')->getStorage($entity_type_id);
    $config_entity = $config_entity_storage->load($bundle_id);

    if (!$config_entity instanceof ConfigEntityInterface) {
      $this->fail("Config entity {$entity_type_id}.{$bundle_id} not found or not a ConfigEntityInterface.");
      return;
    }

    $config_entity->unsetThirdPartySetting('entity_type_access_conditions', 'test_condition');
    $config_entity->save();
  }

  /**
   * Checks access for a named route.
   *
   * @param string $route_name
   *   The name of the route to check.
   * @param array $route_parameters
   *   (optional) An associative array of route parameters.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  protected function checkRouteAccess(string $route_name, array $route_parameters = []): AccessResultInterface {
    return $this->container->get('access_manager')->checkNamedRoute($route_name, $route_parameters, $this->testUser, TRUE);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc