Skip to content

Commit

Permalink
Merge pull request #58 from 5pm-HDH/feat/group-hierarchie
Browse files Browse the repository at this point in the history
GroupHierarchie
  • Loading branch information
DumbergerL authored Jan 29, 2022
2 parents 48ada0b + 00e3a2d commit fea39e8
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Group-Hierarchie: [add Group-Hierachie-API](https://github.com/5pm-HDH/churchtools-api/pull/58) to request Parents and Children of Groups.

### Changed

### Fixed
Expand Down
6 changes: 6 additions & 0 deletions docs/GroupAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ echo ($group->getFollowUp());
echo ($group->getRoles());
// OUTPUT: [{}]

echo ("GroupHierarchie\n");
// OUTPUT: GroupHierarchie

$childGroups = $group->requestGroupChildren()?->get();
$parentGroups = $group->requestGroupParents()?->get();

echo ("GroupSettings\n");
// OUTPUT: GroupSettings

Expand Down
4 changes: 4 additions & 0 deletions docs/src/ressources/GroupAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ dd($group->getInformation());
dd($group->getFollowUp());
dd($group->getRoles());

dd("GroupHierarchie\n");
$childGroups = $group->requestGroupChildren()?->get();
$parentGroups = $group->requestGroupParents()?->get();

dd("GroupSettings\n");
dd($group->getSettings()?->getIsHidden());
dd($group->getSettings()?->getIsOpenForMembers());
Expand Down
20 changes: 20 additions & 0 deletions src/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


use CTApi\Models\Traits\FillWithData;
use CTApi\Requests\GroupHierarchieChildrenRequest;
use CTApi\Requests\GroupHierarchieParentsRequest;
use CTApi\Requests\GroupMemberRequestBuilder;

class Group
Expand Down Expand Up @@ -60,6 +62,24 @@ public function requestMembers(): ?GroupMemberRequestBuilder
}
}

public function requestGroupParents(): ?GroupHierarchieParentsRequest
{
if ($this->getId() != null) {
return new GroupHierarchieParentsRequest((int)$this->getId());
} else {
return null;
}
}

public function requestGroupChildren(): ?GroupHierarchieChildrenRequest
{
if ($this->getId() != null) {
return new GroupHierarchieChildrenRequest((int)$this->getId());
} else {
return null;
}
}

/**
* @return string|null
*/
Expand Down
31 changes: 31 additions & 0 deletions src/Requests/GroupHierarchieAbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace CTApi\Requests;


use CTApi\CTClient;
use CTApi\Utils\CTResponseUtil;

abstract class GroupHierarchieAbstractRequest
{
private $groupId;

public function __construct(int $groupId)
{
$this->groupId = $groupId;
}

protected function requestHierarchieObject()
{
$client = CTClient::getClient();
$response = $client->get('api/groups/hierarchies', [
'json' => [
'ids' => [$this->groupId]
]
]);
return CTResponseUtil::dataAsArray($response);
}

abstract public function get(): array;
}
25 changes: 25 additions & 0 deletions src/Requests/GroupHierarchieChildrenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace CTApi\Requests;


class GroupHierarchieChildrenRequest extends GroupHierarchieAbstractRequest
{
public function get(): array
{
$hierarchie = $this->requestHierarchieObject();
$children = [];
foreach ($hierarchie as $hierarchieItem) {
if (array_key_exists("children", $hierarchieItem)) {
foreach ($hierarchieItem["children"] as $childId) {
$group = GroupRequest::find($childId);
if ($group != null) {
$children[] = $group;
}
}
}
}
return $children;
}
}
25 changes: 25 additions & 0 deletions src/Requests/GroupHierarchieParentsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace CTApi\Requests;


class GroupHierarchieParentsRequest extends GroupHierarchieAbstractRequest
{
public function get(): array
{
$hierarchie = $this->requestHierarchieObject();
$parents = [];
foreach ($hierarchie as $hierarchieItem) {
if (array_key_exists("parents", $hierarchieItem)) {
foreach ($hierarchieItem["parents"] as $parentId) {
$group = GroupRequest::find($parentId);
if ($group != null) {
$parents[] = $group;
}
}
}
}
return $parents;
}
}
77 changes: 77 additions & 0 deletions tests/integration/Requests/GroupHierarchieRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php


namespace Tests\Integration\Requests;


use CTApi\Requests\GroupRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class GroupHierarchieRequestTest extends TestCaseAuthenticated
{

private $groupId = "";
private $groupName = "";
private $groupParentId = "";
private $groupParentName = "";
private $groupChildId = "";
private $groupChildName = "";

protected function setUp(): void
{
if (!TestData::getValue("GROUP_HIERARCHIE_SHOULD_TEST") == "YES") {
$this->markTestSkipped("Test suite is disabled in testdata.ini");
} else {
$this->groupId = TestData::getValue("GROUP_HIERARCHIE_ID");
$this->groupName = TestData::getValue("GROUP_HIERARCHIE_NAME");
$this->groupParentId = TestData::getValue("GROUP_HIERARCHIE_PARENT_ID");
$this->groupParentName = TestData::getValue("GROUP_HIERARCHIE_PARENT_NAME");
$this->groupChildId = TestData::getValue("GROUP_HIERARCHIE_CHILD_ID");
$this->groupChildName = TestData::getValue("GROUP_HIERARCHIE_CHILD_NAME");
}
}

public function testRequestGroup()
{
$group = GroupRequest::findOrFail($this->groupId);
$this->assertEquals($this->groupName, $group->getName());
}

public function testRequestGroupParents()
{
$group = GroupRequest::findOrFail($this->groupId);

$parents = $group->requestGroupParents()?->get();
$this->assertNotNull($parents);

$foundParent = null;
foreach ($parents as $parent) {
if ($parent->getId() == $this->groupParentId) {
$foundParent = $parent;
}
}

$this->assertNotNull($foundParent);
$this->assertEquals($this->groupParentName, $foundParent->getName());
}

public function testRequestGroupChildren()
{
$group = GroupRequest::findOrFail($this->groupId);

$children = $group->requestGroupChildren()?->get();
$this->assertNotNull($children);

$foundChild = null;
foreach ($children as $child) {
if ($child->getId() == $this->groupChildId) {
$foundChild = $child;
}
}

$this->assertNotNull($foundChild);
$this->assertEquals($this->groupChildName, $foundChild->getName());
}

}
11 changes: 10 additions & 1 deletion tests/integration/testdata.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ WIKI_PAGE_HAS_FILES = true
# enable GROUP testsuite with YES and disable with NO
GROUP_SHOULD_TEST = NO
GROUP_ID = 291
GROUP_NAME = Test Group
GROUP_NAME = Test Group

# enable GROUP_HIERARCHIE testsuite with YES and disable with NO
GROUP_HIERARCHIE_SHOULD_TEST = YES
GROUP_HIERARCHIE_ID = 11
GROUP_HIERARCHIE_NAME = Test
GROUP_HIERARCHIE_CHILD_ID = 14
GROUP_HIERARCHIE_CHILD_NAME = Test Child
GROUP_HIERARCHIE_PARENT_ID = 13
GROUP_HIERARCHIE_PARENT_NAME = Test Parent

0 comments on commit fea39e8

Please sign in to comment.