Skip to content

Commit 63082b5

Browse files
committed
Layouts: Review and testing/CI update
1 parent db50b14 commit 63082b5

7 files changed

Lines changed: 42 additions & 12 deletions

File tree

app/Http/Middleware/ClearPerRequestCaches.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ public function __construct(
2121

2222
public function handle(Request $request, Closure $next)
2323
{
24-
$response = $next($request);
24+
$this->clearCaches();
2525

26-
$this->viewBlockManager->clearLocalCache();
26+
return $next($request);
27+
}
2728

28-
return $response;
29+
protected function clearCaches(): void
30+
{
31+
$this->viewBlockManager->clearLocalCache();
2932
}
3033
}

app/View/LayoutController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public function update(string $location, Request $request)
4747
]);
4848

4949
$layoutData = json_decode($data['layout'], true, 5);
50-
$this->viewBlocks->updatePreferencesFromIdPositionMap($location, $layoutData);
50+
if (is_array($layoutData)) {
51+
$this->viewBlocks->updatePreferencesFromIdPositionMap($location, $layoutData);
52+
}
5153

5254
$this->showSuccessNotification(trans('preferences.layout_update_success'));
5355

app/View/ViewBlockManager.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function register(string $location, string $defaultPosition, string $bloc
3535
$this->blocksByLocationAndPosition[$location][$defaultPosition] = [];
3636
}
3737

38+
// @phpstan-ignore-next-line
3839
if (!is_a($blockClass, ViewBlockInterface::class, true)) {
3940
throw new \InvalidArgumentException('When registering a view block, the block class must implement ViewBlockInterface');
4041
}
@@ -81,7 +82,7 @@ protected function blocksToInstances(array $blocks): array
8182
*/
8283
protected function getForLocation(string $location): array
8384
{
84-
$defaults = ViewBlockDefaults::getForLocation($location) ?? [];
85+
$defaults = ViewBlockDefaults::getForLocation($location);
8586
$registered = $this->blocksByLocationAndPosition[$location] ?? [];
8687
return array_merge_recursive($defaults, $registered);
8788
}
@@ -90,7 +91,7 @@ protected function getForLocation(string $location): array
9091
* Get all blocks registered for a given location, as sets of arrays
9192
* keyed by position, for the current user.
9293
* Same as above but with user-specific preferences applied.
93-
* @return array<string, class-string<ViewBlockInterface>[]>
94+
* @return array<string, list<class-string<ViewBlockInterface>>>
9495
* @throws BindingResolutionException
9596
*/
9697
public function getForLocationForCurrentUser(string $location): array
@@ -113,7 +114,7 @@ public function getForLocationForCurrentUser(string $location): array
113114
$results[$position] = [];
114115
foreach ($userBlockIds as $blockId) {
115116
$block = $blocksById[$blockId] ?? null;
116-
if ($block && isset($blocksById[$blockId])) {
117+
if ($block) {
117118
$results[$position][] = $block;
118119
unset($blocksById[$blockId]);
119120
}
@@ -182,7 +183,7 @@ public function clearLocalCache(): void
182183
/**
183184
* Convert a blocksByPosition array into a map of block IDs to blocks.
184185
* @param array<string, class-string<ViewBlockInterface>[]> $blocksByPosition
185-
* @return array<string, ViewBlockInterface>
186+
* @return array<string, class-string<ViewBlockInterface>>
186187
*/
187188
protected function blocksByPositionToIdMap(array $blocksByPosition): array
188189
{

app/View/ViewBlockPreferences.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ public function storeByIdPositionMap(
3636
$validatedLayoutData = [];
3737

3838
foreach ($layoutData as $position => $blockIds) {
39-
if (!in_array($position, $validPositions)) {
39+
// @phpstan-ignore-next-line
40+
if (!in_array($position, $validPositions) || !is_array($blockIds)) {
4041
continue;
4142
}
4243

43-
$validatedLayoutData[$position] = array_intersect($blockIds, $validIds);
44+
$validatedLayoutData[$position] = array_filter($blockIds, function ($id) use ($validIds) {
45+
return is_string($id) && in_array($id, $validIds);
46+
});
4447
}
4548

4649
$settingKey = $this->getSettingKey($location);

app/View/ViewBlocks/ShelvesShowTags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ShelvesShowTags extends ViewBlock
1414
public function withData(array $viewData): array
1515
{
1616
return [
17-
'book' => $viewData['book'],
17+
'shelf' => $viewData['shelf'],
1818
];
1919
}
2020
}

tests/User/UserMyAccountTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function test_update_interface_preferences()
357357
$resp->assertOk();
358358

359359
$resp->assertSeeText('Preferred Language');
360-
$resp->assertSeeText('Customize UI Layout');
360+
$resp->assertSeeText('UI Layout Preferences');
361361

362362
$resp = $this->put('/my-account/interface', ['language' => 'fr', 'display_mode' => 'dark']);
363363
$resp->assertRedirect('/my-account/interface');

tests/View/LayoutEditTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ public function test_update()
121121
], $storedData);
122122
}
123123

124+
public function test_malformed_update()
125+
{
126+
$layout = [
127+
'left' => 'a',
128+
'right' => ['builtin_books-show-activity', 5],
129+
];
130+
131+
$resp = $this->asEditor()->put('/layouts/books-show', ['layout' => json_encode($layout)]);
132+
$resp->assertRedirect('/layouts/books-show');
133+
134+
$userPreferenceString = setting()->getForCurrentUser('view-layout#books-show');
135+
$this->assertJson($userPreferenceString);
136+
$storedData = json_decode($userPreferenceString, true);
137+
138+
$this->assertEquals([
139+
'right' => [
140+
'builtin_books-show-activity',
141+
],
142+
], $storedData);
143+
}
144+
124145
public function test_layout_changes_take_effect()
125146
{
126147
$editor = $this->users->editor();

0 commit comments

Comments
 (0)