Skip to content

Commit

Permalink
Merge pull request #192 from 5pm-HDH/fix/183/group-hierarchie
Browse files Browse the repository at this point in the history
fix group hierarchie #183
  • Loading branch information
DumbergerL authored Jan 9, 2024
2 parents 3cb801e + 5f7350a commit 165be32
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 81 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- PHPUnit and PHP8.1 compatibility ([PR181](https://github.com/5pm-HDH/churchtools-api/pull/181))

- Fix GroupHierarchie ([PR192](https://github.com/5pm-HDH/churchtools-api/pull/192))

## [2.0.0]

Expand Down
31 changes: 0 additions & 31 deletions src/Models/Groups/Group/GroupHierarchieAbstractRequest.php

This file was deleted.

29 changes: 10 additions & 19 deletions src/Models/Groups/Group/GroupHierarchieChildrenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@
namespace CTApi\Models\Groups\Group;


use CTApi\CTClient;
use CTApi\Exceptions\CTPermissionException;
use CTApi\Utils\CTResponseUtil;

class GroupHierarchieChildrenRequest extends GroupHierarchieAbstractRequest
class GroupHierarchieChildrenRequest
{
public function __construct(private readonly int $groupId)
{
}

public function get(): array
{
$hierarchie = $this->requestHierarchieObject();
$children = [];
foreach ($hierarchie as $hierarchieItem) {
if (array_key_exists("children", $hierarchieItem)) {
foreach ($hierarchieItem["children"] as $childId) {
$group = null;
try{
$group = GroupRequest::find($childId);
}catch (CTPermissionException $exception){
// user has no permission to access group
}
if ($group != null) {
$children[] = $group;
}
}
}
}
return $children;
$response = CTClient::getClient()->get('api/groups/' . $this->groupId . '/children');
$data = CTResponseUtil::dataAsArray($response);
return Group::createModelsFromArray($data);
}
}
25 changes: 11 additions & 14 deletions src/Models/Groups/Group/GroupHierarchieParentsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
namespace CTApi\Models\Groups\Group;


class GroupHierarchieParentsRequest extends GroupHierarchieAbstractRequest
use CTApi\CTClient;
use CTApi\Utils\CTResponseUtil;

class GroupHierarchieParentsRequest
{
public function __construct(private readonly int $groupId)
{
}

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;
$response = CTClient::getClient()->get('api/groups/' . $this->groupId . '/parents');
$data = CTResponseUtil::dataAsArray($response);
return Group::createModelsFromArray($data);
}
}
22 changes: 6 additions & 16 deletions tests/Integration/Requests/GroupHierarchieRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class GroupHierarchieRequestTest extends TestCaseAuthenticated

private $groupId = "";
private $groupName = "";
private $groupParentId = ""; /** @phpstan-ignore-line */
private $groupParentName = ""; /** @phpstan-ignore-line */
private $groupChildId = ""; /** @phpstan-ignore-line */
private $groupChildName = ""; /** @phpstan-ignore-line */
private $groupParentId = "";
private $groupParentName = "";
private $groupChildId = "";
private $groupChildName = "";

protected function setUp(): void
{
Expand All @@ -42,8 +42,7 @@ public function testRequestGroup()

public function testRequestGroupParents()
{
$this->markTestSkipped("ChurchTools API Endpoint is broken. Fix in issue: https://github.com/5pm-HDH/churchtools-api/issues/183");
$group = GroupRequest::findOrFail($this->groupId); /** @phpstan-ignore-line */
$group = GroupRequest::findOrFail($this->groupId);

$parents = $group->requestGroupParents()?->get();
$this->assertNotNull($parents);
Expand All @@ -61,19 +60,11 @@ public function testRequestGroupParents()

public function testRequestGroupChildren()
{
$this->markTestSkipped("ChurchTools API Endpoint is broken. Fix in issue: https://github.com/5pm-HDH/churchtools-api/issues/183");
$group = GroupRequest::findOrFail($this->groupId); /** @phpstan-ignore-line */

CTLog::enableConsoleLog();
CTConfig::enableDebugging();
CTLog::enableHttpLog();
echo "\n". AuthRequest::retrieveApiToken(12) . "\n";
$group = GroupRequest::findOrFail($this->groupId);

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

print_r($children);

$foundChild = null;
foreach ($children as $child) {
if ($child->getId() == $this->groupChildId) {
Expand All @@ -83,5 +74,4 @@ public function testRequestGroupChildren()
$this->assertNotNull($foundChild);
$this->assertEquals($this->groupChildName, $foundChild->getName());
}

}

0 comments on commit 165be32

Please sign in to comment.