From 187159263745660bac548b5cf2b5d30e1b8b9412 Mon Sep 17 00:00:00 2001 From: colemanw Date: Fri, 26 May 2023 11:47:53 -0400 Subject: [PATCH] APIv4 - Add bridge entity metadata to GroupNesting entity --- Civi/Api4/GroupNesting.php | 19 ++++++++++++++++++- .../phpunit/api/v4/Entity/ConformanceTest.php | 4 ---- tests/phpunit/api/v4/Entity/GroupTest.php | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Civi/Api4/GroupNesting.php b/Civi/Api4/GroupNesting.php index 3dd4f366b3..74b7188324 100644 --- a/Civi/Api4/GroupNesting.php +++ b/Civi/Api4/GroupNesting.php @@ -14,10 +14,27 @@ namespace Civi\Api4; * GroupNesting entity. * * @see \Civi\Api4\Group - * @searchable none + * @searchable bridge * @since 5.19 * @package Civi\Api4 */ class GroupNesting extends Generic\DAOEntity { + use Generic\Traits\EntityBridge; + use Generic\Traits\ReadOnlyEntity; + + /** + * @return array + */ + public static function getInfo() { + $info = parent::getInfo(); + $info['bridge'] = [ + 'child_group_id' => [ + 'label' => ts('Children'), + 'description' => ts('Sub-groups nested under this group'), + 'to' => 'parent_group_id', + ], + ]; + return $info; + } } diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index bbce400a60..38932ace75 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -178,10 +178,6 @@ class ConformanceTest extends Api4TestBase implements HookInterface { $this->assertNotEmpty($info['primary_key']); $this->assertRegExp(';^\d\.\d+$;', $info['since']); $this->assertContains($info['searchable'], ['primary', 'secondary', 'bridge', 'none']); - // Bridge must be between exactly 2 entities - if (in_array('EntityBridge', $info['type'], TRUE)) { - $this->assertCount(2, $info['bridge']); - } } /** diff --git a/tests/phpunit/api/v4/Entity/GroupTest.php b/tests/phpunit/api/v4/Entity/GroupTest.php index 798541ad48..5e987e7a66 100644 --- a/tests/phpunit/api/v4/Entity/GroupTest.php +++ b/tests/phpunit/api/v4/Entity/GroupTest.php @@ -124,6 +124,20 @@ class GroupTest extends Api4TestBase { ->execute()->single(); $this->assertEquals([$child1['title'], $child2['title']], $get['children:label']); + $joined = Group::get(FALSE) + ->addWhere('id', 'IN', [$parent1['id'], $parent2['id'], $child1['id'], $child2['id']]) + ->addSelect('id', 'child_group.id', 'child_group.title') + ->addJoin('Group AS child_group', 'INNER', 'GroupNesting', ['id', '=', 'child_group.parent_group_id']) + ->addOrderBy('id') + ->execute(); + + $this->assertCount(3, $joined); + $this->assertEquals($parent1['id'], $joined[0]['id']); + $this->assertEquals($child1['title'], $joined[0]['child_group.title']); + $this->assertEquals($parent2['id'], $joined[1]['id']); + $this->assertEquals($child1['title'], $joined[1]['child_group.title']); + $this->assertEquals($parent2['id'], $joined[2]['id']); + $this->assertEquals($child2['title'], $joined[2]['child_group.title']); } } -- 2.25.1