config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/ConfigTimestampTest.php
tests/src/Kernel/ConfigTimestampTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\config_preview_deploy\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests automatic configuration change timestamp tracking.
*
* @group config_preview_deploy
*/
class ConfigTimestampTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system', 'config_preview_deploy'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['system', 'config_preview_deploy']);
}
/**
* Tests that configuration saves update the last change timestamp.
*/
public function testConfigSaveUpdatesTimestamp(): void {
$state = $this->container->get('state');
// Clear any existing timestamp.
$state->delete('config_preview_deploy.last_change');
// Verify no timestamp is set initially.
$initialTimestamp = $state->get('config_preview_deploy.last_change', 0);
$this->assertEquals(0, $initialTimestamp, 'No initial timestamp should be set.');
// Record the time before making changes.
$beforeChange = time();
// Make a configuration change to trigger the event subscriber.
$this->config('system.site')
->set('name', 'Test Site Name for Timestamp Test')
->set('slogan', 'Testing automatic timestamp tracking')
->save();
// Get the updated timestamp.
$afterTimestamp = $state->get('config_preview_deploy.last_change', 0);
// Verify timestamp was updated.
$this->assertGreaterThan($initialTimestamp, $afterTimestamp, 'Timestamp should be updated after config change.');
$this->assertGreaterThanOrEqual($beforeChange, $afterTimestamp, 'Timestamp should be at or after the time of change.');
$this->assertLessThanOrEqual(time(), $afterTimestamp, 'Timestamp should not be in the future.');
}
/**
* Tests that multiple configuration changes update the timestamp.
*/
public function testMultipleConfigChangesUpdateTimestamp(): void {
$state = $this->container->get('state');
// Clear any existing timestamp.
$state->delete('config_preview_deploy.last_change');
// Make first configuration change.
$this->config('system.site')
->set('name', 'First Change')
->save();
$firstTimestamp = $state->get('config_preview_deploy.last_change', 0);
$this->assertGreaterThan(0, $firstTimestamp, 'First change should set timestamp.');
// Wait a moment to ensure different timestamp.
sleep(1);
// Make second configuration change.
$this->config('system.theme')
->set('default', 'test_theme')
->save();
$secondTimestamp = $state->get('config_preview_deploy.last_change', 0);
$this->assertGreaterThan($firstTimestamp, $secondTimestamp, 'Second change should update timestamp to a later value.');
}
/**
* Tests that configuration deletions update the timestamp.
*/
public function testConfigDeleteUpdatesTimestamp(): void {
$state = $this->container->get('state');
// Create a test configuration using existing system config.
$testConfig = $this->config('system.performance');
$testConfig->set('cache.page.max_age', 3600)->save();
// Clear timestamp after creation.
$state->delete('config_preview_deploy.last_change');
$initialTimestamp = $state->get('config_preview_deploy.last_change', 0);
$beforeDelete = time();
// Delete the configuration.
$testConfig->delete();
$afterTimestamp = $state->get('config_preview_deploy.last_change', 0);
// Verify timestamp was updated by deletion.
$this->assertGreaterThan($initialTimestamp, $afterTimestamp, 'Timestamp should be updated after config deletion.');
$this->assertGreaterThanOrEqual($beforeDelete, $afterTimestamp, 'Timestamp should be at or after the time of deletion.');
}
/**
* Tests that all config changes including our own module update timestamp.
*/
public function testAllConfigChangesTracked(): void {
$state = $this->container->get('state');
// Clear any existing timestamp.
$state->delete('config_preview_deploy.last_change');
$beforeChange = time();
// Change our own module's configuration.
$this->config('config_preview_deploy.settings')
->set('production_url', 'https://example.com')
->save();
// Verify timestamp WAS updated.
$afterTimestamp = $state->get('config_preview_deploy.last_change', 0);
$this->assertGreaterThanOrEqual($beforeChange, $afterTimestamp, 'All config changes including our own should update timestamp.');
}
/**
* Tests that the event subscriber handles rapid configuration changes.
*/
public function testRapidConfigChanges(): void {
$state = $this->container->get('state');
$state->delete('config_preview_deploy.last_change');
$timestamps = [];
// Make several rapid configuration changes.
for ($i = 0; $i < 5; $i++) {
$this->config('system.site')
->set('name', "Rapid Change $i")
->save();
$timestamp = $state->get('config_preview_deploy.last_change', 0);
$timestamps[] = $timestamp;
// Small delay to ensure different timestamps.
// 0.1 seconds.
usleep(100000);
}
// Verify all timestamps are valid and generally increasing.
foreach ($timestamps as $i => $timestamp) {
$this->assertGreaterThan(0, $timestamp, "Timestamp $i should be set.");
if ($i > 0) {
$this->assertGreaterThanOrEqual($timestamps[$i - 1], $timestamp, "Timestamp $i should be greater than or equal to previous timestamp.");
}
}
// Verify the final timestamp is the most recent.
$finalTimestamp = $state->get('config_preview_deploy.last_change', 0);
$this->assertEquals(end($timestamps), $finalTimestamp, 'Final timestamp should match the last recorded timestamp.');
}
}
