config_preview_deploy-1.0.0-alpha3/tests/src/Functional/DrushCommandsTest.php

tests/src/Functional/DrushCommandsTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\config_preview_deploy\Functional;

use Drupal\Tests\BrowserTestBase;
use Drush\TestTraits\DrushTestTrait;

/**
 * Tests Drush commands for Config Preview Deploy.
 *
 * @group config_preview_deploy
 */
class DrushCommandsTest extends BrowserTestBase {

  use DrushTestTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

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

  /**
   * {@inheritdoc}
   *
   * We disable strict config schema checking because config_ignore module's
   * schema is complex and dynamically loaded, which can cause issues in
   * functional tests where the full module discovery might not be complete.
   */
  protected $strictConfigSchema = FALSE;

  /**
   * Tests cpd:init command.
   */
  public function testInitCommand(): void {
    $this->drush('config:preview-deploy:init');
    $this->assertStringContainsString('Base checkpoint created', $this->getOutput());
  }

  /**
   * Tests cpd:status command.
   */
  public function testStatusCommand(): void {
    // Initialize first.
    $this->drush('config:preview-deploy:init');

    $this->drush('config:preview-deploy:status');
    $output = $this->getOutput();
    $this->assertStringContainsString('Config Preview Deploy Status', $output);
    $this->assertStringContainsString('Environment:', $output);
    $this->assertStringContainsString('Base version:', $output);
  }

  /**
   * Tests cpd:diff command.
   */
  public function testDiffCommand(): void {
    // Initialize first.
    $this->drush('config:preview-deploy:init');

    // Should work even with no changes.
    $this->drush('config:preview-deploy:diff');
    $this->assertStringContainsString('No configuration changes detected', $this->getOutput());
  }

  /**
   * Tests cpd:apply command is executable.
   */
  public function testApplyCommandExecutable(): void {
    // Test the command without valid file to verify it's not fatally broken.
    // We expect this to fail but not with undefined method errors.
    $this->drush('config:preview-deploy:apply', [], [], NULL, NULL, 1);
    // Just verify the command executed without fatal translation errors.
    $this->assertTrue(TRUE, 'Apply command executed without fatal errors');
  }

  /**
   * Tests command aliases work.
   */
  public function testCommandAliases(): void {
    // Test short aliases.
    $this->drush('cpd:init');
    $this->assertStringContainsString('Base checkpoint created', $this->getOutput());

    $this->drush('cpd:status');
    $this->assertStringContainsString('Config Preview Deploy Status', $this->getOutput());

    $this->drush('cpd:diff');
    $this->assertStringContainsString('No configuration changes detected', $this->getOutput());
  }

  /**
   * Tests that commands work when patch tool is available.
   */
  public function testCommandsWithPatchTool(): void {
    // Skip if patch tool is not available.
    $patchTool = $this->container->get('config_preview_deploy.patch_tool');
    if (!$patchTool->isAvailable()) {
      $this->markTestSkipped('GNU patch tool is required for this test');
    }

    $this->drush('cpd:init');
    $this->assertStringContainsString('Base checkpoint created', $this->getOutput());

    $this->drush('cpd:status');
    $this->assertStringContainsString('Changes: None', $this->getOutput());
  }

  /**
   * Tests register environment command is executable.
   */
  public function testRegisterEnvironmentCommandExecutable(): void {
    // Test that the command exists and runs without fatal errors.
    $this->drush('config:preview-deploy:register-environment', ['https://test.example.com']);
    $output = $this->getOutput();

    // Command should execute successfully and register the environment.
    $this->assertStringContainsString('Registered preview environment', $output);
    $this->assertStringContainsString('https://test.example.com', $output);
  }

  /**
   * Tests register environment command alias works.
   */
  public function testRegisterEnvironmentCommandAlias(): void {
    // Test that the alias works.
    $this->drush('cpd:register', ['invalid-url'], [], NULL, NULL, 1);
    $output = $this->getOutput();

    // Should show URL validation error.
    $this->assertStringContainsString('Invalid base URI format', $output);
  }

  /**
   * Tests cpd:diff command with --no-filter option.
   */
  public function testDiffCommandWithNoFilter(): void {
    // Check if config_ignore module exists.
    $module_list = \Drupal::service('extension.list.module');
    if (!$module_list->exists('config_ignore')) {
      $this->markTestSkipped('Config ignore module not available in codebase');
    }

    // Initialize first.
    $this->drush('config:preview-deploy:init');

    // Enable config_ignore module.
    $this->container->get('module_installer')->install(['config_ignore']);

    // Configure config_ignore to ignore system.site.
    $config = \Drupal::configFactory()->getEditable('config_ignore.settings');
    $config->set('ignored_config_entities', ['system.site'])
      ->save();

    // Clear caches to ensure services are updated.
    drupal_flush_all_caches();

    // Make configuration changes.
    \Drupal::configFactory()->getEditable('system.site')
      ->set('name', 'Ignored Site Name')
      ->save();

    \Drupal::configFactory()->getEditable('system.performance')
      ->set('cache.page.max_age', 3600)
      ->save();

    // Test diff WITHOUT --no-filter (default behavior, should filter).
    $this->drush('config:preview-deploy:diff');
    $output = $this->getOutput();

    // Should show 3 changed configs (system.performance,
    // config_ignore.settings, and core.extension). system.site should be
    // filtered out by config_ignore.
    $this->assertStringContainsString('Found 3 changed configuration(s)', $output);
    $this->assertStringContainsString('system.performance', $output);
    $this->assertStringContainsString('config_ignore.settings', $output);
    $this->assertStringContainsString('core.extension', $output);
    // The diff content should not show system.site changes since it's ignored.
    // Test diff WITH --no-filter (should show all changes).
    $this->drush('config:preview-deploy:diff', [], ['no-filter' => TRUE]);
    $output = $this->getOutput();

    // Should show 4 changed configs (including system.site and core.extension).
    $this->assertStringContainsString('Found 4 changed configuration(s)', $output);
    $this->assertStringContainsString('- system.site', $output);
    $this->assertStringContainsString('- system.performance', $output);
    $this->assertStringContainsString('- config_ignore.settings', $output);
    $this->assertStringContainsString('- core.extension', $output);
  }

}

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

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