Skip to content

Commit f3741c6

Browse files
authored
fix: subfiltering handlers persist (#4521)
* fix: subfiltering handlers persist * Fixed PHPStan error
1 parent a39d8a1 commit f3741c6

6 files changed

Lines changed: 76 additions & 17 deletions

File tree

lib/Model/ProjectCreation/JobCreationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private function saveJobsMetadata(JobStruct $job, ProjectStructure $projectStruc
193193
}
194194
}
195195

196-
if (!empty($projectStructure->subfiltering_handlers)) {
196+
if (!empty($projectStructure->subfiltering_handlers) && $projectStructure->subfiltering_handlers !== '[]') {
197197
$metadata[JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value] = $projectStructure->subfiltering_handlers;
198198
}
199199

lib/Model/ProjectCreation/ProjectMetadataService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function save(ProjectStructure $projectStructure, FeatureSet $features):
9999
* But the analysis must everytime be performed with the current configuration.
100100
* @see JobCreationService::saveJobsMetadata()
101101
*/
102-
if (!empty($projectStructure->subfiltering_handlers)) {
102+
if (!empty($projectStructure->subfiltering_handlers) && $projectStructure->subfiltering_handlers !== '[]') {
103103
$options[JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value] = $projectStructure->subfiltering_handlers;
104104
}
105105

lib/Utils/Subfiltering/SubfilteringOptionsValidator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public static function validate(string $subfiltering_handlers): ?array
3434
array_keys(HandlersSorter::getDefaultInjectedHandlers())
3535
);
3636

37-
$subfiltering_handlers_array = json_decode($subfiltering_handlers);
37+
$subfiltering_handlers_array = json_decode($subfiltering_handlers, true);
3838

3939
if(empty($subfiltering_handlers_array)){
4040
return [];
4141
}
4242

43-
if(empty(array_diff($defaultHandlers, $subfiltering_handlers_array))){
43+
if(
44+
count($defaultHandlers ?? []) === count($subfiltering_handlers_array) &&
45+
empty(array_diff($defaultHandlers, $subfiltering_handlers_array))
46+
){
4447
// subfiltering is default
4548
return [];
4649
}

tests/unit/Model/ProjectCreation/SaveJobsMetadataTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,25 @@ private function setConfigAndSave(array $extras = []): void
9393
}
9494

9595
// =========================================================================
96-
// SUBFILTERING_HANDLERS — always persisted unconditionally
96+
// SUBFILTERING_HANDLERS
9797
// =========================================================================
9898

9999
#[Test]
100-
public function testSubfilteringHandlersIsAlwaysPersisted(): void
100+
public function testSubfilteringHandlersIsNotPersistedWhenEmptyJsonArray(): void
101101
{
102102
$this->setConfigAndSave();
103103

104-
$this->assertArrayHasKey(JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value, $this->capturedMetadata);
105-
$this->assertSame('[]', $this->capturedMetadata[JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value]);
104+
$this->assertArrayNotHasKey(JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value, $this->capturedMetadata);
105+
}
106+
107+
#[Test]
108+
public function testSubfilteringHandlersIsNotPersistedWhenNull(): void
109+
{
110+
$this->setConfigAndSave([
111+
JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value => null,
112+
]);
113+
114+
$this->assertArrayNotHasKey(JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value, $this->capturedMetadata);
106115
}
107116

108117
#[Test]
@@ -136,16 +145,15 @@ public function testBulkSetUsesCorrectJobIdAndPassword(): void
136145
}
137146

138147
// =========================================================================
139-
// Empty project structure — only SUBFILTERING_HANDLERS persisted
148+
// Empty project structure — persists nothing
140149
// =========================================================================
141150

142151
#[Test]
143-
public function testEmptyProjectStructureOnlyPersistsSubfilteringHandlers(): void
152+
public function testEmptyProjectStructurePersistsNothing(): void
144153
{
145154
$this->setConfigAndSave();
146155

147-
$this->assertCount(1, $this->capturedMetadata);
148-
$this->assertArrayHasKey(JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value, $this->capturedMetadata);
156+
$this->assertCount(0, $this->capturedMetadata);
149157
}
150158

151159
// =========================================================================
@@ -377,6 +385,7 @@ public function testMetadataKeyOrderMatchesCodeOrder(): void
377385
'character_counter_mode' => 'source',
378386
'tm_prioritization' => true,
379387
'dialect_strict' => ['it-IT' => true],
388+
JobsMetadataMarshaller::SUBFILTERING_HANDLERS->value => json_encode([['handler' => 'xliff']]),
380389
]);
381390

382391
$this->assertSame([

tests/unit/Model/ProjectCreation/SaveMetadataTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public function testSubfilteringHandlersIsAlwaysPersisted(): void
113113
self::assertSame('["handler_a"]', $calls[0]['metadata'][ProjectsMetadataMarshaller::SUBFILTERING_HANDLERS->value]);
114114
}
115115

116+
#[Test]
117+
public function testSubfilteringHandlersIsNotPersistedWhenEmptyJsonArray(): void
118+
{
119+
$this->projectStructure->subfiltering_handlers = '[]';
120+
121+
$this->service->save($this->projectStructure, $this->features);
122+
123+
$calls = $this->findDaoCallsByKey(ProjectsMetadataMarshaller::SUBFILTERING_HANDLERS->value);
124+
self::assertEmpty($calls, 'subfiltering_handlers should NOT be persisted when it is "[]"');
125+
}
126+
116127
#[Test]
117128
public function testAllDaoSetCallsUseCorrectProjectId(): void
118129
{
@@ -140,13 +151,12 @@ public function testEmptyMetadataOnlyPersistsSubfilteringHandlersAndDefaults():
140151
// metadata is already empty by default in ProjectStructure
141152
$this->service->save($this->projectStructure, $this->features);
142153

143-
// pretranslate_101 always exists (DTO default = 1), plus subfiltering_handlers
154+
// pretranslate_101 always exists (DTO default = 1)
144155
$metadata = $this->getSinglePersistedMetadataMap();
145-
self::assertCount(2, $metadata);
156+
self::assertCount(1, $metadata);
146157

147158
$keys = array_keys($metadata);
148159
self::assertContains(ProjectsMetadataMarshaller::PRE_TRANSLATE_101->value, $keys);
149-
self::assertContains(ProjectsMetadataMarshaller::SUBFILTERING_HANDLERS->value, $keys);
150160
}
151161

152162
// =========================================================================
@@ -348,9 +358,9 @@ public function testAllMetadataOptionsArePersistedViaBulkSet(): void
348358

349359
$this->service->save($this->projectStructure, $this->features);
350360

351-
// 3 metadata keys + 1 pretranslate_101 (DTO default) + 1 subfiltering_handlers = 5 total
361+
// 3 metadata keys + 1 pretranslate_101 (DTO default) = 4 total
352362
$metadata = $this->getSinglePersistedMetadataMap();
353-
self::assertCount(5, $metadata);
363+
self::assertCount(4, $metadata);
354364

355365
self::assertSame('1', $this->getPersistedValue(ProjectsMetadataMarshaller::ICU_ENABLED->value));
356366
self::assertSame('0', $this->getPersistedValue(ProjectsMetadataMarshaller::MT_EVALUATION->value));

tests/unit/Utils/SubfilteringOptionsValidatorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,41 @@ public function test_validate_with_single_handler()
136136
$this->assertIsArray($result);
137137
$this->assertEquals(['twig'], $result);
138138
}
139+
140+
#[Test]
141+
public function test_validate_with_extra_valid_handler_returns_array()
142+
{
143+
$defaultHandlers = InjectableFiltersTags::tagNamesForArrayClasses(
144+
array_keys(HandlersSorter::getDefaultInjectedHandlers())
145+
);
146+
147+
$customHandlers = $defaultHandlers;
148+
// find a handler that is in schema but not in default
149+
$customHandlers[] = 'sprintf';
150+
151+
$jsonHandlers = json_encode($customHandlers);
152+
$result = SubfilteringOptionsValidator::validate($jsonHandlers);
153+
154+
$this->assertIsArray($result);
155+
$this->assertCount(count($defaultHandlers) + 1, $result);
156+
$this->assertEquals($customHandlers, $result);
157+
}
158+
159+
#[Test]
160+
public function test_validate_with_duplicate_handlers_returns_array()
161+
{
162+
$defaultHandlers = InjectableFiltersTags::tagNamesForArrayClasses(
163+
array_keys(HandlersSorter::getDefaultInjectedHandlers())
164+
);
165+
166+
$customHandlers = $defaultHandlers;
167+
$customHandlers[] = $defaultHandlers[0]; // Duplicate first handler
168+
169+
$jsonHandlers = json_encode($customHandlers);
170+
$result = SubfilteringOptionsValidator::validate($jsonHandlers);
171+
172+
$this->assertIsArray($result);
173+
$this->assertCount(count($defaultHandlers) + 1, $result);
174+
$this->assertEquals($customHandlers, $result);
175+
}
139176
}

0 commit comments

Comments
 (0)