Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/Admin/Members/MemberValuesMetaBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Amber\Admin\Members;

// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}

use Unity\Core\Interfaces\Configuration;
use Unity\Members\Interfaces\Member;
use Unity\Members\Interfaces\MemberRepository;
use WP_Post;

use function add_action;
use function add_meta_box;
use function current_user_can;
use function esc_html;

/**
* "Extended values" box on the member edit screen.
*
* Lists the member's Unity extended values: what other plugins keep against
* a member beyond Unity's own fields, such as Reach's `AppleUserId`. They are
* stored outside ACF, so without this box an administrator has no way to see
* them at all.
*
* <p><b>Read-only.</b> Values are written by the plugin that owns them, which
* knows what they mean; an edit box here would let somebody break a sign-in
* link without knowing it was one.</p>
*/
class MemberValuesMetaBox
{
public const BOX_ID = 'amber-member-extended-values';

private readonly string $postType;

public function __construct(
Configuration $configuration,
private readonly MemberRepository $members,
) {
$config = $configuration->getConfig(Member::class) ?? [];
$this->postType = is_string($config['POST_TYPE'] ?? null) ? $config['POST_TYPE'] : '';

if ($this->postType !== '') {
add_action('add_meta_boxes_' . $this->postType, [$this, 'addMetaBox']);
}
}

public function addMetaBox(WP_Post $post): void
{
if (!current_user_can('edit_post', $post->ID)) {
return;
}

add_meta_box(self::BOX_ID, 'Extended values', [$this, 'render'], $this->postType, 'side', 'low');
}

public function render(WP_Post $post): void
{
$values = $this->members->findById($post->ID)?->allUnityExtendedValues() ?? [];

if ($values === []) {
echo '<p class="description">No extended values.</p>';
return;
}

echo '<table class="widefat striped"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
foreach ($values as $key => $value) {
echo '<tr><td><code>' . esc_html($key) . '</code></td>'
. '<td style="word-break: break-all;"><code>' . esc_html($value) . '</code></td></tr>';
}
echo '</tbody></table>';
}
}
8 changes: 8 additions & 0 deletions src/Core/AmberServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Amber\Admin\Members\DirectoryDashboard;
use Amber\Admin\Members\MemberAdmin;
use Amber\Admin\Members\MemberPasswordAdmin;
use Amber\Admin\Members\MemberValuesMetaBox;
use Amber\Admin\Members\AnonymousNameValidator;
use Amber\Admin\Members\PersonalEmailValidator;
use Amber\Admin\Positions\PositionAdmin;
Expand Down Expand Up @@ -129,6 +130,13 @@ private function registerAdminServices(Container $container): void
);
});

$container->register(MemberValuesMetaBox::class, function (ContainerInterface $c) {
return new MemberValuesMetaBox(
$c->get(Configuration::class),
$c->get(MemberRepository::class)
);
});

$container->register(AnonymousNameValidator::class, function (ContainerInterface $c) {
return new AnonymousNameValidator(
$c->get(Configuration::class)
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Amber\Admin\Members\DirectoryDashboard;
use Amber\Admin\Members\MemberPasswordAdmin;
use Amber\Admin\Members\MemberAdmin;
use Amber\Admin\Members\MemberValuesMetaBox;
use Amber\Admin\Members\AnonymousNameValidator;
use Amber\Admin\Members\PersonalEmailValidator;
use Amber\Admin\Positions\PositionAdmin;
Expand Down Expand Up @@ -119,6 +120,7 @@ public static function init(Container $unityContainer): void
self::$container->get(PositionAdmin::class);
self::$container->get(PositionNameValidator::class);
self::$container->get(MemberAdmin::class);
self::$container->get(MemberValuesMetaBox::class);
self::$container->get(AnonymousNameValidator::class);
self::$container->get(PersonalEmailValidator::class);
self::$container->get(MeetingAdmin::class);
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/Admin/Members/MemberValuesMetaBoxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Amber\Tests\Unit\Admin\Members;

use Amber\Admin\Members\MemberValuesMetaBox;
use BleedingDeacons\WpMocks\WpState;
use Brain\Monkey\Functions;
use Unity\Core\Interfaces\Configuration;
use Unity\Members\Interfaces\Member;
use Unity\Testing\Doubles\InMemoryMemberRepository;
use Unity\Testing\Doubles\MemberStub;
use WP_Post;

/*
* Tests for the "Extended values" box on the member edit screen.
*
* The values it lists are written by other plugins outside ACF, so this box
* is the only place an administrator sees them. What matters is that every
* value is shown, escaped, and that nothing is shown to somebody who cannot
* edit the member.
*/

covers(MemberValuesMetaBox::class);

beforeEach(function () {
$config = $this->createMock(Configuration::class);
$config->method('getConfig')->willReturnCallback(
static fn (string $key): array => $key === Member::class ? ['POST_TYPE' => 'intergroup-member'] : []
);

$this->repository = new InMemoryMemberRepository([
new MemberStub(id: 42, extendedValues: ['Zeta' => 'z', 'AppleUserId' => '001234.abcdef.5678']),
new MemberStub(id: 43),
new MemberStub(id: 44, extendedValues: ['Hostile' => '<script>alert(1)</script>']),
]);

$this->box = new MemberValuesMetaBox($config, $this->repository);

$this->render = function (int $id): string {
ob_start();
$this->box->render(new WP_Post(['ID' => $id]));

return (string) ob_get_clean();
};
});

it('hooks the member post type\'s meta boxes', function () {
expect(has_action('add_meta_boxes_intergroup-member', [$this->box, 'addMetaBox']))->not->toBeFalse();
});

it('adds a side box for someone who can edit the member', function () {
Functions\expect('add_meta_box')
->once()
->with(MemberValuesMetaBox::BOX_ID, 'Extended values', [$this->box, 'render'], 'intergroup-member', 'side', 'low');

$this->box->addMetaBox(new WP_Post(['ID' => 42]));
});

it('adds nothing for someone who cannot edit the member', function () {
WpState::$deniedCaps = ['edit_post'];
Functions\expect('add_meta_box')->never();

$this->box->addMetaBox(new WP_Post(['ID' => 42]));
});

it('lists every extended value, sorted by key', function () {
$html = ($this->render)(42);

expect($html)->toContain('AppleUserId')
->and($html)->toContain('001234.abcdef.5678')
->and($html)->toContain('Zeta')
->and(strpos($html, 'AppleUserId'))->toBeLessThan(strpos($html, 'Zeta'));
});

it('escapes what it shows', function () {
$html = ($this->render)(44);

expect($html)->not->toContain('<script>')
->and($html)->toContain('&lt;script&gt;');
});

it('says so when there are none', function () {
expect(($this->render)(43))->toContain('No extended values.');
});

it('says so for a post that is not a member', function () {
expect(($this->render)(999))->toContain('No extended values.');
});
2 changes: 2 additions & 0 deletions tests/Unit/Core/AmberServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Amber\Admin\Members\AnonymousNameValidator;
use Amber\Admin\Members\DirectoryDashboard;
use Amber\Admin\Members\MemberAdmin;
use Amber\Admin\Members\MemberValuesMetaBox;
use Amber\Admin\Positions\PositionAdmin;
use Amber\Admin\Positions\PositionDashboard;
use Amber\Admin\Positions\PositionNameValidator;
Expand Down Expand Up @@ -49,6 +50,7 @@
ShortcodeService::class => ShortcodeService::class,
FrontPageManager::class => FrontPageManager::class,
MemberAdmin::class => MemberAdmin::class,
MemberValuesMetaBox::class => MemberValuesMetaBox::class,
AnonymousNameValidator::class => AnonymousNameValidator::class,
PositionAdmin::class => PositionAdmin::class,
PositionNameValidator::class => PositionNameValidator::class,
Expand Down