config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/ConfigVerifierTest.php
tests/src/Kernel/ConfigVerifierTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\config_preview_deploy\Kernel;
use Drupal\config_preview_deploy\ConfigVerifier;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the ConfigVerifier service.
*
* @group config_preview_deploy
*/
class ConfigVerifierTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['config_preview_deploy', 'system'];
/**
* The config verifier service.
*/
protected ConfigVerifier $configVerifier;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['system', 'config_preview_deploy']);
// Get config verifier service.
$this->configVerifier = $this->container->get('config_preview_deploy.config_verifier');
}
/**
* Tests verification request validation with various scenarios.
*/
public function testVerificationRequestValidation(): void {
// Mock HTTP_HOST for testing.
$_SERVER['HTTP_HOST'] = 'localhost';
$hashVerification = $this->container->get('config_preview_deploy.hash_verification');
// Use current time and adjust for testing.
$currentTime = time();
// Test 1: Invalid verification (old timestamp).
$this->container->get('state')->set('config_preview_deploy.last_change', $currentTime);
// Older than last_change, so invalid.
$baseTimestamp = $currentTime - 100;
// Get the host from Drupal's base URL to match verification logic.
$currentUrl = Url::fromRoute('system.admin', [], ['absolute' => TRUE])->toString();
$productionHost = parse_url($currentUrl, PHP_URL_HOST);
$authHash = $hashVerification->generateVerificationHash($productionHost, $baseTimestamp);
$request = [
'environment' => 'pr-feature-123',
'base_timestamp' => $baseTimestamp,
'auth_hash' => $authHash,
];
$result = $this->configVerifier->validateVerificationRequest($request);
$this->assertIsArray($result);
$this->assertArrayHasKey('valid', $result);
$this->assertArrayHasKey('current_timestamp', $result);
$this->assertArrayHasKey('last_deployed_from', $result);
$this->assertFalse($result['valid'], 'Should be invalid since base is older');
$this->assertEquals($currentTime, $result['current_timestamp']);
// Test 2: Valid verification (new timestamp).
$this->container->get('state')->set('config_preview_deploy.last_change', $currentTime - 200);
// Newer than last_change, so valid.
$baseTimestamp = $currentTime - 50;
$authHash = $hashVerification->generateVerificationHash($productionHost, $baseTimestamp);
$request = [
'environment' => 'pr-feature-123',
'base_timestamp' => $baseTimestamp,
'auth_hash' => $authHash,
];
$result = $this->configVerifier->validateVerificationRequest($request);
$this->assertTrue($result['valid'], 'Should be valid since base is newer');
$this->assertEquals($currentTime - 200, $result['current_timestamp']);
// Test 3: Authentication failure.
$request = [
'environment' => 'pr-feature-123',
'base_timestamp' => $currentTime - 50,
'auth_hash' => 'invalid-hash',
];
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid authentication');
$this->configVerifier->validateVerificationRequest($request);
}
/**
* Tests updating last change timestamp.
*/
public function testLastChangeTracking(): void {
$environment = 'pr-feature-123';
// Get the current timestamp (may already be set by event subscriber).
$initialTimestamp = $this->container->get('state')->get('config_preview_deploy.last_change', 0);
$this->configVerifier->updateLastChange($environment);
// Verify timestamp was updated.
$lastChange = $this->container->get('state')->get('config_preview_deploy.last_change');
$this->assertGreaterThanOrEqual($initialTimestamp, $lastChange);
$this->assertLessThanOrEqual(time(), $lastChange);
// Verify environment was recorded.
$this->assertEquals($environment, $this->container->get('state')->get('config_preview_deploy.last_deployed_from'));
}
}
