diff --git a/src/Admin/ExtendedValues/CommitteeExtendedValuesField.php b/src/Admin/ExtendedValues/CommitteeExtendedValuesField.php new file mode 100644 index 0000000..b56ba86 --- /dev/null +++ b/src/Admin/ExtendedValues/CommitteeExtendedValuesField.php @@ -0,0 +1,53 @@ +getConfig(Committee::class) ?? []; + $taxonomy = $config['TAXONOMY'] ?? null; + + if (is_string($taxonomy) && $taxonomy !== '') { + add_action($taxonomy . '_edit_form_fields', [$this, 'render'], 99); + } + } + + public function render(WP_Term $term): void + { + if (!current_user_can('edit_term', $term->term_id)) { + return; + } + + $values = $this->committees->findById($term->term_id)?->allUnityExtendedValues() ?? []; + + echo 'Extended values' + . ExtendedValuesTable::html($values) + . '

Kept by other plugins; read-only.

'; + } +} diff --git a/src/Admin/ExtendedValues/ExtendedValuesMetaBox.php b/src/Admin/ExtendedValues/ExtendedValuesMetaBox.php new file mode 100644 index 0000000..50a4987 --- /dev/null +++ b/src/Admin/ExtendedValues/ExtendedValuesMetaBox.php @@ -0,0 +1,90 @@ +Read-only. 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.

+ * + *

A model gets the box only when its implementation publishes a + * `POST_TYPE` in Unity's configuration. TSML publishes none for locations, + * which it edits inside the meeting screen, so locations have no box. A post + * type with no edit screen of its own simply never fires the hook.

+ */ +final class ExtendedValuesMetaBox +{ + public const BOX_ID = 'amber-extended-values'; + + /** @var array Post type => find the record by id. */ + private array $lookups = []; + + /** + * @param array $lookups + * Unity model interface => find that model by post id. + */ + public function __construct(Configuration $configuration, array $lookups) + { + foreach ($lookups as $model => $find) { + $config = $configuration->getConfig($model) ?? []; + $postType = $config['POST_TYPE'] ?? null; + + if (is_string($postType) && $postType !== '') { + $this->lookups[$postType] = $find; + } + } + + if ($this->lookups !== []) { + add_action('add_meta_boxes', [$this, 'addMetaBox'], 10, 2); + } + } + + /** + * @param string $postType The screen's post type. + * @param mixed $post The post being edited; a comment on the comment screen. + */ + public function addMetaBox(string $postType, mixed $post): void + { + if (!isset($this->lookups[$postType]) || !$post instanceof WP_Post) { + return; + } + + if (!current_user_can('edit_post', $post->ID)) { + return; + } + + add_meta_box(self::BOX_ID, 'Extended values', [$this, 'render'], $postType, 'side', 'low'); + } + + public function render(WP_Post $post): void + { + $find = $this->lookups[$post->post_type] ?? null; + $values = $find !== null ? ($find($post->ID)?->allUnityExtendedValues() ?? []) : []; + + echo ExtendedValuesTable::html($values); + } +} diff --git a/src/Admin/ExtendedValues/ExtendedValuesTable.php b/src/Admin/ExtendedValues/ExtendedValuesTable.php new file mode 100644 index 0000000..c44c484 --- /dev/null +++ b/src/Admin/ExtendedValues/ExtendedValuesTable.php @@ -0,0 +1,41 @@ + $values Already sorted by key, as the store returns them. */ + public static function html(array $values): string + { + if ($values === []) { + return '

No extended values.

'; + } + + $html = ''; + foreach ($values as $key => $value) { + $html .= '' + . ''; + } + + return $html . '
KeyValue
' . esc_html($key) . '' . esc_html($value) . '
'; + } +} diff --git a/src/Admin/Members/MemberValuesMetaBox.php b/src/Admin/Members/MemberValuesMetaBox.php deleted file mode 100644 index bdd6b66..0000000 --- a/src/Admin/Members/MemberValuesMetaBox.php +++ /dev/null @@ -1,77 +0,0 @@ -Read-only. 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.

- */ -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 '

No extended values.

'; - return; - } - - echo ''; - foreach ($values as $key => $value) { - echo '' - . ''; - } - echo '
KeyValue
' . esc_html($key) . '' . esc_html($value) . '
'; - } -} diff --git a/src/Core/AmberServiceProvider.php b/src/Core/AmberServiceProvider.php index a32689e..c914782 100644 --- a/src/Core/AmberServiceProvider.php +++ b/src/Core/AmberServiceProvider.php @@ -10,6 +10,8 @@ } use Amber\Admin\DeveloperDashboard; +use Amber\Admin\ExtendedValues\CommitteeExtendedValuesField; +use Amber\Admin\ExtendedValues\ExtendedValuesMetaBox; use Amber\Admin\IntergroupMeetings\IntergroupAttendanceAdmin; use Amber\Admin\IntergroupMeetings\IntergroupMeetingAdmin; use Amber\Admin\IntergroupMeetings\IntergroupMeetingAttendanceDashboard; @@ -20,7 +22,6 @@ 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; @@ -42,18 +43,24 @@ use Unity\Committees\Interfaces\CommitteeRepository; use Unity\Core\Interfaces\Configuration; use Unity\Core\Interfaces\Container; +use Unity\Core\Interfaces\KeyValueStoreInterface; +use Unity\Groups\Interfaces\Group; use Unity\Groups\Interfaces\GroupFactory; use Unity\Groups\Interfaces\GroupRepository; use Unity\Groups\Interfaces\GroupViewFactory; +use Unity\IntergroupMeetings\Interfaces\IntergroupMeeting; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingFactory; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingRepository; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingGroupAttendanceFactory; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingGroupAttendanceRepository; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingOfficerAttendanceFactory; use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingOfficerAttendanceRepository; +use Unity\Meetings\Interfaces\Meeting; use Unity\Meetings\Interfaces\MeetingRepository; +use Unity\Members\Interfaces\Member; use Unity\Members\Interfaces\MemberRepository; use Unity\Members\Interfaces\MemberRevisor; +use Unity\Positions\Interfaces\Position; use Unity\Positions\Interfaces\PositionFactory; use Unity\Positions\Interfaces\PositionRepository; use Unity\Positions\Interfaces\PositionViewFactory; @@ -130,10 +137,36 @@ private function registerAdminServices(Container $container): void ); }); - $container->register(MemberValuesMetaBox::class, function (ContainerInterface $c) { - return new MemberValuesMetaBox( + // One box for every Unity model kept as a post. Each lookup is + // feature-detected on its repository binding, as the committee + // screens are, and resolved only when a box is drawn. + $container->register(ExtendedValuesMetaBox::class, function (ContainerInterface $c) { + $repositories = [ + Member::class => MemberRepository::class, + Meeting::class => MeetingRepository::class, + Group::class => GroupRepository::class, + Position::class => PositionRepository::class, + IntergroupMeeting::class => IntergroupMeetingRepository::class, + ]; + $lookups = []; + + foreach ($repositories as $model => $repository) { + if ($c->has($repository)) { + $lookups[$model] = static function (int $id) use ($c, $repository): ?KeyValueStoreInterface { + $found = self::resolve($c, $repository)?->findById($id); + + return $found instanceof KeyValueStoreInterface ? $found : null; + }; + } + } + + return new ExtendedValuesMetaBox($c->get(Configuration::class), $lookups); + }); + + $container->register(CommitteeExtendedValuesField::class, function (ContainerInterface $c) { + return new CommitteeExtendedValuesField( $c->get(Configuration::class), - $c->get(MemberRepository::class) + $c->get(CommitteeRepository::class) ); }); @@ -305,4 +338,18 @@ private function registerReconciler(Container $container): void ); }); } + + /** + * A service from the container, or null when it is not the type asked for. + * + * @template T of object + * @param class-string $id + * @return T|null + */ + private static function resolve(ContainerInterface $c, string $id): ?object + { + $service = $c->get($id); + + return $service instanceof $id ? $service : null; + } } diff --git a/src/Plugin.php b/src/Plugin.php index 8c034bc..f9e956d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -10,6 +10,8 @@ } use Amber\Admin\DeveloperDashboard; +use Amber\Admin\ExtendedValues\CommitteeExtendedValuesField; +use Amber\Admin\ExtendedValues\ExtendedValuesMetaBox; use Amber\Admin\IntergroupMeetings\IntergroupAttendanceAdmin; use Amber\Admin\IntergroupMeetings\IntergroupMeetingAdmin; use Amber\Admin\IntergroupMeetings\IntergroupMeetingDashboard; @@ -19,7 +21,6 @@ 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; @@ -120,7 +121,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(ExtendedValuesMetaBox::class); self::$container->get(AnonymousNameValidator::class); self::$container->get(PersonalEmailValidator::class); self::$container->get(MeetingAdmin::class); @@ -146,6 +147,7 @@ public static function init(Container $unityContainer): void if (self::$container->has(CommitteeRepository::class)) { self::$container->get(CommitteeTree::class); self::$container->get(CommitteeAssignmentController::class); + self::$container->get(CommitteeExtendedValuesField::class); } // Run metadata migrations once per version upgrade (deferred to admin_init diff --git a/tests/Pest.php b/tests/Pest.php index bff5233..8cfbaeb 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -25,6 +25,7 @@ pest()->extend(AmberTestCase::class)->in( 'Unit/Admin/Committees', + 'Unit/Admin/ExtendedValues', 'Unit/Admin/Meetings', 'Unit/Admin/Members', 'Unit/Admin/Positions', diff --git a/tests/Unit/Admin/ExtendedValues/CommitteeExtendedValuesFieldTest.php b/tests/Unit/Admin/ExtendedValues/CommitteeExtendedValuesFieldTest.php new file mode 100644 index 0000000..6ebc8bf --- /dev/null +++ b/tests/Unit/Admin/ExtendedValues/CommitteeExtendedValuesFieldTest.php @@ -0,0 +1,71 @@ +createMock(Configuration::class); + $config->method('getConfig')->willReturnCallback( + static fn (string $key): array => $key === Committee::class ? ['TAXONOMY' => 'committee'] : [] + ); + + $this->field = new CommitteeExtendedValuesField($config, new InMemoryCommitteeRepository([ + new CommitteeStub(id: 5, slug: 'telephones', extendedValues: ['Zeta' => 'z', 'Alpha' => 'a']), + new CommitteeStub(id: 6, slug: 'public-information'), + ])); + + $this->render = function (int $id): string { + ob_start(); + $this->field->render(new WP_Term(['term_id' => $id, 'taxonomy' => 'committee'])); + + return (string) ob_get_clean(); + }; +}); + +it('hooks the committee taxonomy\'s edit form', function () { + expect(has_action('committee_edit_form_fields', [$this->field, 'render']))->not->toBeFalse(); +}); + +it('hooks nothing when no committee taxonomy is configured', function () { + $config = $this->createMock(Configuration::class); + $config->method('getConfig')->willReturn([]); + + $field = new CommitteeExtendedValuesField($config, new InMemoryCommitteeRepository([])); + + expect(has_action('_edit_form_fields', [$field, 'render']))->toBeFalse(); +}); + +it('lists the committee\'s values as a form row, sorted and escaped', function () { + $html = ($this->render)(5); + + expect($html)->toStartWith('') + ->and($html)->toContain('Extended values') + ->and(strpos($html, 'Alpha'))->toBeLessThan(strpos($html, 'Zeta')) + ->and($html)->not->toContain('a') + ->and($html)->toContain('<b>a</b>'); +}); + +it('says so when the committee has none', function () { + expect(($this->render)(6))->toContain('No extended values.'); +}); + +it('shows nothing to someone who cannot edit the committee', function () { + WpState::$deniedCaps = ['edit_term']; + + expect(($this->render)(5))->toBe(''); +}); diff --git a/tests/Unit/Admin/ExtendedValues/ExtendedValuesMetaBoxTest.php b/tests/Unit/Admin/ExtendedValues/ExtendedValuesMetaBoxTest.php new file mode 100644 index 0000000..da4bc1b --- /dev/null +++ b/tests/Unit/Admin/ExtendedValues/ExtendedValuesMetaBoxTest.php @@ -0,0 +1,142 @@ + 'intergroup-member', + Meeting::class => 'tsml_meeting', + Group::class => 'tsml_group', + Position::class => 'intergroup-position', + ]; + + $config = $this->createMock(Configuration::class); + $config->method('getConfig')->willReturnCallback( + static fn (string $key): array => isset($postTypes[$key]) ? ['POST_TYPE' => $postTypes[$key]] : [] + ); + + $members = new InMemoryMemberRepository([ + new MemberStub(id: 42, extendedValues: ['Zeta' => 'z', 'AppleUserId' => '001234.abcdef.5678']), + new MemberStub(id: 43), + new MemberStub(id: 44, extendedValues: ['Hostile' => '']), + ]); + $meetings = new InMemoryMeetingRepository([new MeetingStub(id: 42, extendedValues: ['MeetingKey' => 'meeting-42'])]); + $groups = new InMemoryGroupRepository([new GroupStub(id: 42, extendedValues: ['GroupKey' => 'group-42'])]); + $positions = new InMemoryPositionRepository([new PositionStub(id: 42, extendedValues: ['PositionKey' => 'position-42'])]); + + $this->box = new ExtendedValuesMetaBox($config, [ + Member::class => static fn (int $id) => $members->findById($id), + Meeting::class => static fn (int $id) => $meetings->findById($id), + Group::class => static fn (int $id) => $groups->findById($id), + Position::class => static fn (int $id) => $positions->findById($id), + // Published by no implementation, so it must get no box. + Location::class => static fn (int $id) => null, + ]); + + $this->render = function (int $id, string $postType = 'intergroup-member'): string { + ob_start(); + $this->box->render(new WP_Post(['ID' => $id, 'post_type' => $postType])); + + return (string) ob_get_clean(); + }; +}); + +it('hooks the meta boxes once for every post type', function () { + expect(has_action('add_meta_boxes', [$this->box, 'addMetaBox']))->not->toBeFalse(); +}); + +it('adds a side box on each configured post type for someone who can edit it', function (string $postType) { + Functions\expect('add_meta_box') + ->once() + ->with(ExtendedValuesMetaBox::BOX_ID, 'Extended values', [$this->box, 'render'], $postType, 'side', 'low'); + + $this->box->addMetaBox($postType, new WP_Post(['ID' => 42, 'post_type' => $postType])); +})->with(['intergroup-member', 'tsml_meeting', 'tsml_group', 'intergroup-position']); + +it('adds nothing on a post type with no model, or with no configuration', function (string $postType) { + Functions\expect('add_meta_box')->never(); + + $this->box->addMetaBox($postType, new WP_Post(['ID' => 42, 'post_type' => $postType])); +})->with(['post', 'tsml_location', '']); + +it('adds nothing for someone who cannot edit the post', function () { + WpState::$deniedCaps = ['edit_post']; + Functions\expect('add_meta_box')->never(); + + $this->box->addMetaBox('intergroup-member', new WP_Post(['ID' => 42, 'post_type' => 'intergroup-member'])); +}); + +it('adds nothing on the comment screen, which hands it a comment', function () { + Functions\expect('add_meta_box')->never(); + + $this->box->addMetaBox('intergroup-member', new \stdClass()); +}); + +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('reads each post type\'s own model, not another with the same id', function (string $postType, string $key) { + $html = ($this->render)(42, $postType); + + expect($html)->toContain($key) + ->and($html)->not->toContain('AppleUserId'); +})->with([ + ['tsml_meeting', 'MeetingKey'], + ['tsml_group', 'GroupKey'], + ['intergroup-position', 'PositionKey'], +]); + +it('escapes what it shows', function () { + $html = ($this->render)(44); + + expect($html)->not->toContain('']), - ]); - - $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('