workflow-8.x-1.x-dev/tests/src/Unit/WorkflowManagerTest.php

tests/src/Unit/WorkflowManagerTest.php
<?php

namespace Drupal\Tests\workflow\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\workflow\Entity\WorkflowManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;

/**
 * Unit tests for the WorkflowManager service.
 *
 * Tests workflow management operations including creation,
 * loading, state management, and transition execution.
 */
#[Group('workflow')]
class WorkflowManagerTest extends UnitTestCase {

  /**
   * Mock workflow manager service.
   *
   * @var \Drupal\workflow\Entity\WorkflowManager|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $workflowManager;

  /**
   * Mock entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

  /**
   * Mock entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityStorage;

  /**
   * Mock config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $configFactory;

  /**
   * Mock entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityFieldManager;

  /**
   * Mock string translation.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $stringTranslation;

  /**
   * Mock module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

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

    // Create mock entity storage for workflow entities.
    $this->entityStorage = $this->getMockBuilder(EntityStorageInterface::class)
      ->getMock();

    // Create mock entity type manager.
    $this->entityTypeManager = $this->getMockBuilder(EntityTypeManagerInterface::class)
      ->getMock();

    // Create mock config factory.
    $this->configFactory = $this->getMockBuilder(ConfigFactoryInterface::class)
      ->getMock();
    $userConfig = $this->createMock(Config::class);
    $this->configFactory->expects($this->any())
      ->method('get')
      ->with('user.settings')
      ->willReturn($userConfig);

    // Create mock entity field manager.
    $this->entityFieldManager = $this->getMockBuilder(EntityFieldManagerInterface::class)
      ->getMock();

    // Create mock string translation.
    $this->stringTranslation = $this->getMockBuilder(TranslationInterface::class)
      ->getMock();

    // Create mock module handler.
    $this->moduleHandler = $this->getMockBuilder(ModuleHandlerInterface::class)
      ->getMock();

    // Configure entity type manager to return storage.
    $this->entityTypeManager->expects($this->any())
      ->method('getStorage')
      ->willReturnMap([
        ['workflow_type', $this->entityStorage],
        ['workflow_state', $this->entityStorage],
        ['workflow_transition', $this->entityStorage],
      ]);

    // Create mock workflow manager with all dependencies.
    $this->workflowManager = $this->getMockBuilder(WorkflowManager::class)
      ->setConstructorArgs([
        $this->configFactory,
        $this->entityFieldManager,
        $this->entityTypeManager,
        $this->stringTranslation,
        $this->moduleHandler,
      ])
      ->getMock();
  }

  /**
   * Test field map retrieval functionality.
   *
   * Manager should be able to retrieve workflow field maps.
   * Field mapping is essential for workflow field operations.
   */
  public function testWorkflowFieldMap() {
    $entity_type_id = 'node';
    $expected_map = ['field_workflow' => ['type' => 'workflow']];

    // Test field map retrieval - mock the actual method call.
    $this->workflowManager->expects($this->once())
      ->method('getFieldMap')
      ->with($entity_type_id)
      ->willReturn($expected_map);

    $result = $this->workflowManager->getFieldMap($entity_type_id);
    $this->assertEquals($expected_map, $result);
  }

  /**
   * Test workflow field definitions retrieval.
   *
   * Manager should provide field definition functionality.
   * Field definitions are needed for workflow field configuration.
   */
  public function testWorkflowFieldDefinitions() {
    $entity_type_id = 'node';
    $entity_bundle = 'article';
    $field_name = 'field_workflow';

    // Test that field definition method exists and can be called.
    $result = $this->workflowManager->getWorkflowFieldDefinitions(NULL, $entity_type_id, $entity_bundle, $field_name);

    // Since this is a mock, we expect an empty array or null.
    $this->assertTrue(is_array($result) || $result === NULL, 'Result should be an array or null');
  }

  /**
   * Test workflow manager dependencies.
   *
   * Manager should be properly constructed with all dependencies.
   * Dependency injection ensures proper service integration.
   */
  public function testWorkflowManagerDependencies() {
    // Test that the manager was constructed with proper dependencies.
    $this->assertInstanceOf(WorkflowManager::class, $this->workflowManager);

    // Test entity type manager is properly injected.
    $this->assertInstanceOf(EntityTypeManagerInterface::class, $this->entityTypeManager);

    // Test config factory is properly injected.
    $this->assertInstanceOf(ConfigFactoryInterface::class, $this->configFactory);
  }

  /**
   * Test entity storage retrieval.
   *
   * Manager should provide access to entity storage via entity type manager.
   * Storage access is needed for workflow entity operations.
   */
  public function testEntityStorageRetrieval() {
    // Test that entity type manager returns configured storage.
    $result = $this->entityTypeManager->getStorage('workflow_type');
    $this->assertEquals($this->entityStorage, $result);

    // Test multiple storage types are supported.
    $state_storage = $this->entityTypeManager->getStorage('workflow_state');
    $this->assertEquals($this->entityStorage, $state_storage);
  }

  /**
   * Test workflow manager configuration access.
   *
   * Manager should have access to configuration services.
   * Configuration is needed for workflow settings and user preferences.
   */
  public function testWorkflowManagerConfiguration() {
    // Test that config factory provides user configuration.
    $userConfig = $this->configFactory->get('user.settings');
    $this->assertInstanceOf(Config::class, $userConfig);

    // Test that entity field manager is available.
    $this->assertInstanceOf(EntityFieldManagerInterface::class, $this->entityFieldManager);
  }

  /**
   * Test workflow manager service integration.
   *
   * Manager should integrate properly with other Drupal services.
   * Service integration ensures proper workflow functionality.
   */
  public function testWorkflowManagerServiceIntegration() {
    // Test string translation service integration.
    $this->assertInstanceOf(TranslationInterface::class, $this->stringTranslation);

    // Test module handler service integration.
    $this->assertInstanceOf(ModuleHandlerInterface::class, $this->moduleHandler);

    // Test that manager has all required dependencies.
    $this->assertInstanceOf(MockObject ::class, $this->workflowManager);
  }

  /**
   * Test workflow field map for all entity types.
   *
   * Manager should provide field maps for all entity types when no type given.
   * Complete field mapping is needed for workflow administration.
   */
  public function testWorkflowFieldMapAllEntityTypes() {
    $complete_map = [
      'node' => ['field_workflow' => ['type' => 'workflow']],
      'user' => ['field_user_workflow' => ['type' => 'workflow']],
    ];

    // Test field map retrieval for all entity types - mock actual method call.
    $this->workflowManager->expects($this->once())
      ->method('getFieldMap')
      ->with('')
      ->willReturn($complete_map);

    $result = $this->workflowManager->getFieldMap('');
    $this->assertEquals($complete_map, $result);
    $this->assertArrayHasKey('node', $result);
    $this->assertArrayHasKey('user', $result);
  }

  /**
   * Test workflow manager service availability.
   *
   * Manager should be available as a service.
   * Service availability ensures proper workflow functionality.
   */
  public function testWorkflowManagerServiceAvailability() {
    // Test that the manager was constructed with proper dependencies.
    $this->assertInstanceOf(WorkflowManager::class, $this->workflowManager);

    // Test entity type manager is properly injected.
    $this->assertInstanceOf(EntityTypeManagerInterface::class, $this->entityTypeManager);
  }

}

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

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