config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/PreviewEnvironmentAccessProductionTest.php
tests/src/Kernel/PreviewEnvironmentAccessProductionTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\config_preview_deploy\Kernel;
use Drupal\Core\Url;
use Drupal\config_preview_deploy\Access\PreviewEnvironmentAccess;
use Drupal\KernelTests\KernelTestBase;
/**
* Test production environment detection in PreviewEnvironmentAccess.
*
* @group config_preview_deploy
*/
class PreviewEnvironmentAccessProductionTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['config_preview_deploy', 'system'];
/**
* The preview environment access service.
*/
protected PreviewEnvironmentAccess $previewAccess;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['config_preview_deploy']);
$this->previewAccess = $this->container->get('config_preview_deploy.preview_environment_access');
}
/**
* Tests environment variable detection with various values.
*/
public function testEnvironmentVariableDetection(): void {
// Test with environment variable set to true.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=1');
$this->assertTrue($this->previewAccess->isProduction());
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
// Test with environment variable set to false.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=0');
// Should fall back to domain checking.
$this->assertFalse($this->previewAccess->isProduction());
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
// Test various environment variable values.
$testCases = [
'true' => TRUE,
'false' => FALSE,
'1' => TRUE,
'0' => FALSE,
];
foreach ($testCases as $value => $expected) {
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=' . $value);
if ($expected) {
$this->assertTrue($this->previewAccess->isProduction(), "Environment variable '$value' should return TRUE");
}
else {
// When env var is false, it falls back to domain checking which
// should be false in test.
$this->assertFalse($this->previewAccess->isProduction(), "Environment variable '$value' should return FALSE");
}
// Clean up.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
}
}
/**
* Tests production detection with basic domain matching scenarios.
*/
public function testBasicDomainMatching(): void {
// Test with no production URL configured.
$this->config('config_preview_deploy.settings')
->set('production_url', '')
->save();
$this->assertFalse($this->previewAccess->isProduction());
// Test with matching domain.
$currentUrl = Url::fromRoute('system.admin', [], ['absolute' => TRUE])->toString();
$parsed = parse_url($currentUrl);
$baseUrl = $parsed['scheme'] . '://' . $parsed['host'];
if (isset($parsed['port'])) {
$baseUrl .= ':' . $parsed['port'];
}
$this->config('config_preview_deploy.settings')
->set('production_url', $baseUrl)
->save();
$this->assertTrue($this->previewAccess->isProduction());
// Test with non-matching domain.
$this->config('config_preview_deploy.settings')
->set('production_url', 'https://example.com')
->save();
$this->assertFalse($this->previewAccess->isProduction());
}
/**
* Tests domain matching with protocol and port differences.
*/
public function testDomainMatchingProtocolAndPorts(): void {
// Test HTTPS vs HTTP domain matching.
$currentUrl = Url::fromRoute('system.admin', [], ['absolute' => TRUE])->toString();
$parsed = parse_url($currentUrl);
$otherProtocol = $parsed['scheme'] === 'https' ? 'http' : 'https';
$urlWithDifferentProtocol = $otherProtocol . '://' . $parsed['host'];
if (isset($parsed['port'])) {
$urlWithDifferentProtocol .= ':' . $parsed['port'];
}
$this->config('config_preview_deploy.settings')
->set('production_url', $urlWithDifferentProtocol)
->save();
// Should NOT match since we compare full base URLs including protocol.
$this->assertFalse($this->previewAccess->isProduction());
// Test with port numbers in URLs.
$urlWithPort = $parsed['scheme'] . '://' . $parsed['host'] . ':8080';
$this->config('config_preview_deploy.settings')
->set('production_url', $urlWithPort)
->save();
// Should NOT match since we compare full base URLs including port.
$this->assertFalse($this->previewAccess->isProduction());
}
/**
* Tests domain matching edge cases.
*/
public function testDomainMatchingEdgeCases(): void {
// Test with subdomain differences.
$this->config('config_preview_deploy.settings')
->set('production_url', 'https://www.localhost')
->save();
// Should not match since hostnames are different.
$this->assertFalse($this->previewAccess->isProduction());
// Test with invalid production URL.
$this->config('config_preview_deploy.settings')
->set('production_url', 'not-a-valid-url')
->save();
// Should return false when URL parsing fails.
$this->assertFalse($this->previewAccess->isProduction());
}
/**
* Tests that environment variable takes precedence over domain matching.
*/
public function testEnvironmentVariablePrecedence(): void {
// Configure non-matching production URL.
$this->config('config_preview_deploy.settings')
->set('production_url', 'https://example.com')
->save();
// Set environment variable to true.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=1');
// Should return true due to environment variable, despite non-matching
// domain.
$this->assertTrue($this->previewAccess->isProduction());
// Clean up.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
}
/**
* Tests comprehensive production detection scenarios.
*/
public function testComprehensiveProductionDetection(): void {
// Start with clean state.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
$this->config('config_preview_deploy.settings')
->set('production_url', '')
->save();
// Verify default state is non-production.
$this->assertFalse($this->previewAccess->isProduction());
// Test environment variable override.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=1');
$this->assertTrue($this->previewAccess->isProduction());
// Test environment variable false allows domain checking.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION=0');
// Configure matching domain.
$currentUrl = Url::fromRoute('system.admin', [], ['absolute' => TRUE])->toString();
$parsed = parse_url($currentUrl);
$baseUrl = $parsed['scheme'] . '://' . $parsed['host'];
if (isset($parsed['port'])) {
$baseUrl .= ':' . $parsed['port'];
}
$this->config('config_preview_deploy.settings')
->set('production_url', $baseUrl)
->save();
$this->assertTrue($this->previewAccess->isProduction());
// Clean up.
putenv('DRUPAL_CONFIG_PREVIEW_DEPLOY_IS_PRODUCTION');
}
}
