config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/RequirementsTest.php

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

declare(strict_types=1);

namespace Drupal\Tests\config_preview_deploy\Kernel;

use Drupal\config_preview_deploy\PatchTool;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests system requirements validation for config preview deploy.
 *
 * @group config_preview_deploy
 */
class RequirementsTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'config_preview_deploy',
    'system',
  ];

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

    // Include the install file for requirements function.
    \Drupal::moduleHandler()->loadInclude('config_preview_deploy', 'install');
  }

  /**
   * Tests requirements when patch tool is available.
   */
  public function testRequirementsWithPatchToolAvailable(): void {
    // Patch tool is required for running tests - fail if not available.
    $patchTool = $this->container->get('config_preview_deploy.patch_tool');
    if (!$patchTool->isAvailable()) {
      $this->fail('GNU patch tool is required for running tests. Install with: apt install patch');
    }

    $requirements = config_preview_deploy_requirements('runtime');

    $this->assertArrayHasKey('config_preview_deploy_patch_tool', $requirements);
    $requirement = $requirements['config_preview_deploy_patch_tool'];

    $this->assertEquals('Config Preview Deploy - Patch Tool', $requirement['title']);
    $this->assertEquals(REQUIREMENT_OK, $requirement['severity']);
    $this->assertStringContainsString('Available', (string) $requirement['value']);
    $this->assertStringContainsString('working correctly', (string) $requirement['description']);
  }

  /**
   * Tests requirements when patch tool is not available using mock.
   */
  public function testRequirementsWithPatchToolUnavailable(): void {
    // Create a mock PatchTool that returns false for isAvailable.
    $mockPatchTool = $this->createMock(PatchTool::class);
    $mockPatchTool->method('isAvailable')->willReturn(FALSE);
    $mockPatchTool->method('getToolInfo')->willReturn([
      'available' => FALSE,
      'version' => 'unknown',
    ]);

    // Replace the service container with our mock.
    $this->container->set('config_preview_deploy.patch_tool', $mockPatchTool);

    $requirements = config_preview_deploy_requirements('runtime');

    $this->assertArrayHasKey('config_preview_deploy_patch_tool', $requirements);
    $requirement = $requirements['config_preview_deploy_patch_tool'];

    $this->assertEquals(REQUIREMENT_ERROR, $requirement['severity']);
    $this->assertStringContainsString('Not Available', (string) $requirement['value']);
    $this->assertStringContainsString('GNU patch tool is required', (string) $requirement['description']);
    $this->assertStringContainsString('apt install patch', (string) $requirement['description']);
  }

  /**
   * Tests PatchTool service integration.
   */
  public function testPatchToolServiceIntegration(): void {
    $patchTool = $this->container->get('config_preview_deploy.patch_tool');
    $this->assertInstanceOf(PatchTool::class, $patchTool);

    // Test getToolInfo method.
    $toolInfo = $patchTool->getToolInfo();
    $this->assertIsArray($toolInfo);
    $this->assertArrayHasKey('available', $toolInfo);

    if ($toolInfo['available']) {
      $this->assertArrayHasKey('version', $toolInfo);
      $this->assertArrayHasKey('output', $toolInfo);
    }
  }

  /**
   * Tests validation with patch tool unavailable.
   */
  public function testValidationWithPatchToolUnavailable(): void {
    // Create a mock PatchTool that returns false for isAvailable.
    $mockPatchTool = $this->createMock(PatchTool::class);
    $mockPatchTool->method('isAvailable')->willReturn(FALSE);

    // Replace the service with our mock.
    $this->container->set('config_preview_deploy.patch_tool', $mockPatchTool);

    $configDiff = $this->container->get('config_preview_deploy.config_diff');

    $validation = $configDiff->validateDiff('some diff content');

    $this->assertFalse($validation['valid']);
    $this->assertNotEmpty($validation['errors']);
    $this->assertStringContainsString('Patch tool not available', implode(' ', $validation['errors']));
  }

}

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

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