Skip to content

Commit

Permalink
additional test for editing component data
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 29, 2023
1 parent 5cafc0f commit 5f6b468
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/models/ComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ public function offsetExists(mixed $offset): bool
return true;
}

public function get(mixed $offset): mixed
{
return $this->offsetGet($offset);
}

public function offsetGet(mixed $offset): mixed
public function get(mixed $offset, bool $raw=false): mixed
{
if ($this->hasAttribute($offset)) {
return $this->getAttribute($offset);
Expand All @@ -131,13 +126,18 @@ public function offsetGet(mixed $offset): mixed

$value = $this->getData()[$offset] ?? null;

if ($this->normalizer) {
if ($raw === false && $this->normalizer) {
return ($this->normalizer)($value, $offset);
}

return $value;
}

public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->merge([$offset => $value]);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{ renderField({
id: field.handle,
label: field.name,
}, field.getInputHtml(field.normalizeValue(component.data[field.handle]))) }}
}, field.getInputHtml(field.normalizeValue(component.data.get(field.handle, true)))) }}
{% endfor %}
{% endnamespace %}
</div>
Expand Down
11 changes: 11 additions & 0 deletions tests/ComponentDataTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use craft\helpers\UrlHelper;
use markhuot\craftpest\factories\Entry;
use markhuot\keystone\actions\DuplicateComponentTree;
use markhuot\keystone\actions\EditComponentData;
Expand Down Expand Up @@ -67,3 +68,13 @@
expect($data['foo'])->toBeNull();
expect($data->getData())->toBeEmpty();
});

it('loads component edit route with raw values', function () {
$component = Component::factory()->type('keystone/text')->create();
$component->data->merge(['text' => '{foo}'])->save();

$this->actingAsAdmin()
->get(UrlHelper::cpUrl('keystone/components/edit?'.http_build_query($component->getQueryCondition())))
->assertSee('{foo}')
->assertOk();
});

0 comments on commit 5f6b468

Please sign in to comment.