-
Notifications
You must be signed in to change notification settings - Fork 1
[BUGFIX] detect and surface site settings write failures #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace T3G\Analytics\Service; | ||
|
|
||
| use T3G\Analytics\Exception\AnalyticsApiException; | ||
| use TYPO3\CMS\Core\Core\Environment; | ||
| use TYPO3\CMS\Core\Site\Entity\Site; | ||
| use TYPO3\CMS\Core\Site\SiteSettingsFactory; | ||
|
|
||
| final readonly class SiteSettingsWriteVerifier implements SiteSettingsWriteVerifierInterface | ||
| { | ||
| public function __construct( | ||
| private SiteSettingsFactory $siteSettingsFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function assertDirectoryWritable(Site $site): void | ||
| { | ||
| $identifier = $site->getIdentifier(); | ||
| $configDir = Environment::getConfigPath() . '/sites/' . $identifier; | ||
|
|
||
| if (!is_dir($configDir) || !is_writable($configDir)) { | ||
| throw new AnalyticsApiException( | ||
| 'Cannot write to site configuration directory for "' . $identifier . '". Check file system permissions.', | ||
| 0 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function assertSettingsPersisted(Site $site, array $expected): void | ||
| { | ||
| $identifier = $site->getIdentifier(); | ||
| $persisted = $this->siteSettingsFactory->loadLocalSettings($identifier) ?? []; | ||
|
|
||
| foreach ($expected as $key => $value) { | ||
| if (($persisted[$key] ?? '') !== $value) { | ||
| throw new AnalyticsApiException( | ||
| 'Settings for site "' . $identifier . '" could not be persisted. Check file system permissions.', | ||
| 0 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace T3G\Analytics\Service; | ||
|
|
||
| use T3G\Analytics\Exception\AnalyticsApiException; | ||
| use TYPO3\CMS\Core\Site\Entity\Site; | ||
|
|
||
| interface SiteSettingsWriteVerifierInterface | ||
| { | ||
| /** | ||
| * Asserts that the site configuration directory is writable. | ||
| * | ||
| * @throws AnalyticsApiException | ||
| */ | ||
| public function assertDirectoryWritable(Site $site): void; | ||
|
|
||
| /** | ||
| * Asserts that the given settings were actually persisted to disk. | ||
| * Call this after writeSettings() to detect silent write failures. | ||
| * | ||
| * @param array<string, mixed> $expected key-value pairs that must appear in the persisted settings | ||
| * @throws AnalyticsApiException | ||
| */ | ||
| public function assertSettingsPersisted(Site $site, array $expected): void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Tests/Functional/Service/SiteSettingsWriteVerifierTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace T3G\Analytics\Tests\Functional\Service; | ||
|
|
||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use T3G\Analytics\Exception\AnalyticsApiException; | ||
| use T3G\Analytics\Service\SiteSettingsWriteVerifier; | ||
| use T3G\Analytics\Tests\Functional\Bootstrap\FunctionalTestCase; | ||
| use TYPO3\CMS\Core\Http\Uri; | ||
| use TYPO3\CMS\Core\Settings\Settings; | ||
| use TYPO3\CMS\Core\Site\Entity\Site; | ||
| use TYPO3\CMS\Core\Site\Entity\SiteSettings; | ||
| use TYPO3\CMS\Core\Site\SiteSettingsFactory; | ||
|
|
||
| final class SiteSettingsWriteVerifierTest extends FunctionalTestCase | ||
| { | ||
| private SiteSettingsFactory&MockObject $siteSettingsFactory; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->siteSettingsFactory = $this->createMock(SiteSettingsFactory::class); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function assertSettingsPersistedDoesNotThrowWhenAllValuesMatch(): void | ||
| { | ||
| $this->expectNotToPerformAssertions(); | ||
|
|
||
| $this->siteSettingsFactory->method('loadLocalSettings')->willReturn([ | ||
| 'websiteId' => 'w-123', | ||
| 'instanceId' => 'i-456', | ||
| ]); | ||
| $verifier = new SiteSettingsWriteVerifier($this->siteSettingsFactory); | ||
|
|
||
| $verifier->assertSettingsPersisted($this->buildSite(), ['websiteId' => 'w-123', 'instanceId' => 'i-456']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function assertSettingsPersistedThrowsWhenKeyIsAbsent(): void | ||
| { | ||
| $this->siteSettingsFactory->method('loadLocalSettings')->willReturn([]); | ||
| $verifier = new SiteSettingsWriteVerifier($this->siteSettingsFactory); | ||
|
|
||
| $this->expectException(AnalyticsApiException::class); | ||
| $this->expectExceptionMessageMatches('/"main"/'); | ||
|
|
||
| $verifier->assertSettingsPersisted($this->buildSite(), ['websiteId' => 'w-123']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function assertSettingsPersistedThrowsWhenValueDoesNotMatch(): void | ||
| { | ||
| $this->siteSettingsFactory->method('loadLocalSettings')->willReturn(['websiteId' => 'other-id']); | ||
| $verifier = new SiteSettingsWriteVerifier($this->siteSettingsFactory); | ||
|
|
||
| $this->expectException(AnalyticsApiException::class); | ||
|
|
||
| $verifier->assertSettingsPersisted($this->buildSite(), ['websiteId' => 'w-123']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function assertSettingsPersistedThrowsOnFirstFailingKey(): void | ||
| { | ||
| $this->siteSettingsFactory->method('loadLocalSettings')->willReturn(['websiteId' => 'w-123']); | ||
| $verifier = new SiteSettingsWriteVerifier($this->siteSettingsFactory); | ||
|
|
||
| $this->expectException(AnalyticsApiException::class); | ||
|
|
||
| $verifier->assertSettingsPersisted($this->buildSite(), ['websiteId' => 'w-123', 'instanceId' => 'i-456']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function assertSettingsPersistedReadsSettingsOnlyOnce(): void | ||
| { | ||
| $this->siteSettingsFactory | ||
| ->expects(self::once()) | ||
| ->method('loadLocalSettings') | ||
| ->willReturn(['websiteId' => 'w-123', 'instanceId' => 'i-456']); | ||
|
|
||
| $verifier = new SiteSettingsWriteVerifier($this->siteSettingsFactory); | ||
| $verifier->assertSettingsPersisted($this->buildSite(), ['websiteId' => 'w-123', 'instanceId' => 'i-456']); | ||
| } | ||
|
|
||
| private function buildSite(): Site | ||
| { | ||
| $site = $this->createMock(Site::class); | ||
| $site->method('getIdentifier')->willReturn('main'); | ||
| $site->method('getBase')->willReturn(new Uri('https://example.com')); | ||
| $site->method('getSettings')->willReturn(new SiteSettings(new Settings([]), [], [])); | ||
| return $site; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.