Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix group hierarchie #183 #192

Merged
merged 1 commit into from
Jan 9, 2024
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
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());
}

}
Loading