Skip to content

Commit 0514f54

Browse files
committed
Planner API new types and methods
1 parent d9d8614 commit 0514f54

File tree

10 files changed

+189
-15
lines changed

10 files changed

+189
-15
lines changed

src/Directory/Groups/Group.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
use Office365\Directory\DirectoryObject;
99
use Office365\Directory\Licenses\LicenseProcessingState;
10+
use Office365\Directory\Users\User;
11+
use Office365\EntityCollection;
1012
use Office365\OneDrive\Drives\Drive;
1113
use Office365\OneDrive\Drives\DriveCollection;
1214
use Office365\OneDrive\Sites\SiteCollection;
1315
use Office365\OneNote\Onenote;
1416
use Office365\Outlook\Calendars\Calendar;
1517
use Office365\Outlook\ProfilePhoto;
1618
use Office365\Planner\PlannerGroup;
19+
use Office365\Runtime\Actions\InvokePostMethodQuery;
1720
use Office365\Runtime\ResourcePath;
1821
use Office365\Teams\Team;
1922

@@ -23,6 +26,32 @@
2326
class Group extends DirectoryObject
2427
{
2528

29+
/**
30+
@param string $uid
31+
* Add members
32+
*/
33+
public function addMember($uid)
34+
{
35+
$payload = [
36+
"@odata.id" => "https://graph.microsoft.com/v1.0/directoryObjects/{$uid}",
37+
];
38+
$qry = new InvokePostMethodQuery($this->getMembers(),"\$ref",null,null, $payload);
39+
$this->getContext()->addQuery($qry);
40+
return $this;
41+
}
42+
43+
/**
44+
* @param User $user
45+
* @return $this
46+
*/
47+
public function addMemberUser(User $user): Group
48+
{
49+
$user->ensureProperty("Id",function () use($user){
50+
$this->addMember($user->getId());
51+
});
52+
return $this;
53+
}
54+
2655
/**
2756
* Create a new team under a group.
2857
*/
@@ -35,7 +64,7 @@ public function addTeam(){
3564
* Describes a classification for the group (such as low, medium or high business impact). Valid values for this property are defined by creating a ClassificationList [setting](groupsetting.md) value, based on the [template definition](groupsettingtemplate.md).<br><br>Returned by default.
3665
* @return string
3766
*/
38-
public function getClassification()
67+
public function getClassification(): string
3968
{
4069
return $this->getProperty("Classification");
4170
}
@@ -520,4 +549,26 @@ public function setHideFromAddressLists($value)
520549
{
521550
$this->setProperty("HideFromAddressLists", $value, true);
522551
}
552+
553+
/**
554+
* List group members
555+
* @return EntityCollection
556+
*/
557+
public function getMembers()
558+
{
559+
return $this->getProperty("members",
560+
new EntityCollection($this->getContext(),
561+
new ResourcePath("members",$this->getResourcePath()),DirectoryObject::class));
562+
}
563+
564+
/**
565+
* List group owners
566+
* @return EntityCollection
567+
*/
568+
public function getOwners()
569+
{
570+
return $this->getProperty("owners",
571+
new EntityCollection($this->getContext(),
572+
new ResourcePath("owners",$this->getResourcePath()),DirectoryObject::class));
573+
}
523574
}

src/Directory/Users/UserCollection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use Office365\EntityCollection;
8+
use Office365\Runtime\ClientObject;
89
use Office365\Runtime\ClientRuntimeContext;
910
use Office365\Runtime\ResourcePath;
1011

@@ -16,4 +17,15 @@ public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePa
1617
parent::__construct($ctx, $resourcePath, User::class);
1718
}
1819

20+
/**
21+
* @param string $value User principal name
22+
* @return User
23+
*/
24+
function getByUserPrincipalName($value)
25+
{
26+
/** @var User $user */
27+
$user = $this->createType(new ResourcePath($value, $this->getResourcePath()));
28+
return $user;
29+
}
30+
1931
}

src/GraphServiceClient.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Office365\OneDrive\DriveItems\DriveItem;
1212
use Office365\OneDrive\Drives\DriveCollection;
1313
use Office365\OneDrive\Sites\Site;
14+
use Office365\Planner\Planner;
1415
use Office365\Reports\ReportRoot;
1516
use Office365\Runtime\Auth\AADTokenProvider;
1617
use Office365\Runtime\Auth\ClientCredential;
@@ -163,6 +164,15 @@ public function getGroupSettings(){
163164
return new EntityCollection($this,new ResourcePath("groupSettings"), GroupSetting::class);
164165
}
165166

167+
/**
168+
* Entry-point to the Planner resource. Read-only.
169+
* @return Planner
170+
*/
171+
public function getPlanner()
172+
{
173+
return new Planner($this, new ResourcePath("planner"));
174+
}
175+
166176
/**
167177
* @return TeamCollection
168178
*/

src/Planner/Planner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
namespace Office365\Planner;
77

88
use Office365\Entity;
9+
use Office365\Planner\Plans\PlannerPlanCollection;
10+
use Office365\Runtime\ResourcePath;
911

1012
class Planner extends Entity
1113
{
14+
/**
15+
* @return PlannerPlanCollection
16+
*/
17+
public function getPlans()
18+
{
19+
return $this->getProperty("plans",
20+
new PlannerPlanCollection($this->getContext(),
21+
new ResourcePath("plans", $this->getResourcePath()), $this));
22+
}
1223
}

src/Planner/Plans/PlannerPlan.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77

88
use Office365\Directory\Identities\IdentitySet;
99
use Office365\Entity;
10+
use Office365\Runtime\Http\RequestOptions;
1011
use Office365\Runtime\ResourcePath;
1112
/**
1213
* The **plannerPlan** resource represents a plan in Office 365. A plan can be owned by a [group](group.md) and contains a collection of [plannerTasks](plannertask.md). It can also have a collection of [plannerBuckets](plannerbucket.md). Each plan object has a [details](plannerplandetails.md) object that can contain more information about the plan. For more information about the relationships between groups, plans, and tasks, see [Planner](planner-overview.md).
1314
*/
1415
class PlannerPlan extends Entity
1516
{
17+
18+
/**
19+
* @return string
20+
*/
21+
public function getEtag()
22+
{
23+
return $this->getProperty("Etag");
24+
}
25+
1626
/**
1727
* Read-only. The user who created the plan.
1828
* @return IdentitySet
@@ -70,4 +80,13 @@ public function getDetails()
7080
return $this->getProperty("Details",
7181
new PlannerPlanDetails($this->getContext(),new ResourcePath("Details", $this->getResourcePath())));
7282
}
83+
84+
public function deleteObject()
85+
{
86+
parent::deleteObject();
87+
$this->getContext()->getPendingRequest()->beforeExecuteRequestOnce(function (RequestOptions $request) {
88+
$request->Headers["If-Match"] = $this->getEtag();
89+
});
90+
return $this;
91+
}
7392
}

src/Planner/Plans/PlannerPlanCollection.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use Office365\EntityCollection;
77
use Office365\Runtime\ClientRuntimeContext;
8-
use Office365\Runtime\Http\RequestOptions;
98
use Office365\Runtime\ResourcePath;
109

1110
class PlannerPlanCollection extends EntityCollection {
@@ -32,12 +31,9 @@ public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePa
3231
public function create($title){
3332
/** @var PlannerPlan $returnType */
3433
$containerUrl = $this->getContainerUrl();
35-
$returnType = $this->add();
34+
$returnType = $this->getContext()->getPlanner()->getPlans()->add();
3635
$returnType->setTitle($title);
3736
$returnType->setProperty("container", ["url" => $containerUrl]);
38-
$this->getContext()->getPendingRequest()->beforeExecuteRequestOnce(function (RequestOptions $request){
39-
$request->Url = $this->getContext()->getServiceRootUrl() . "/planner/plans";
40-
});
4137
return $returnType;
4238
}
4339

src/Runtime/OData/ODataRequest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private function nextProperty($json, $format)
264264
* @param ODataFormat $format
265265
* @return bool
266266
*/
267-
private function isValidProperty($key,$value,$format)
267+
private function isValidProperty(&$key,$value,$format)
268268
{
269269
if ($format instanceof JsonLightFormat) {
270270
if (is_array($value) && array_key_exists($format->DeferredTag, $value))
@@ -274,6 +274,12 @@ private function isValidProperty($key,$value,$format)
274274
);
275275
return !in_array($key, $propsToExclude);
276276
} else if ($format instanceof JsonFormat) {
277+
278+
if ($key === "$format->ControlFamilyTag.etag"){
279+
$key = "etag";
280+
return true;
281+
}
282+
277283
return fnmatch("$format->ControlFamilyTag.*", $key) !== true
278284
&& fnmatch("*$format->ControlFamilyTag.*", $key) !== true
279285
&& fnmatch("$format->TypeTag.*", $key) !== true

src/SharePoint/OneDriveItemCollection.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
*/
66
namespace Office365\SharePoint;
77

8-
use Office365\Runtime\ClientObject;
9-
use Office365\Runtime\Actions\DeleteEntityQuery;
10-
use Office365\Runtime\ResourcePath;
11-
use Office365\Runtime\Actions\UpdateEntityQuery;
128
class OneDriveItemCollection extends BaseEntity
139
{
1410
}

tests/directory/GroupsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Office365;
4+
5+
6+
use Office365\Directory\Groups\Group;
7+
use Office365\Runtime\Http\RequestException;
8+
9+
class GroupsTest extends GraphTestCase
10+
{
11+
12+
/**
13+
* @var Group
14+
*/
15+
private static $targetGroup;
16+
17+
public function testCreateM365Group()
18+
{
19+
$grpName = "TestGroup_" . rand(1, 100000);
20+
self::$targetGroup = self::$graphClient->getGroups()->createM365($grpName)->executeQuery();
21+
self::assertNotNull(self::$targetGroup->getResourcePath());
22+
}
23+
24+
public function testListGroups()
25+
{
26+
$result = self::$graphClient->getGroups()->get()->top(10)->executeQuery();
27+
self::assertNotNull($result->getResourcePath());
28+
self::assertNotEmpty($result->getData());
29+
}
30+
31+
32+
public function testListOwners()
33+
{
34+
$result = self::$targetGroup->getOwners()->get()->top(10)->executeQuery();
35+
self::assertNotNull($result->getResourcePath());
36+
self::assertNotEmpty($result->getData());
37+
}
38+
39+
public function testAddMember()
40+
{
41+
$user = self::$graphClient->getUsers()->getByUserPrincipalName(self::$testAccountName);
42+
$result = self::$targetGroup->addMemberUser($user)->executeQuery();
43+
self::assertNotNull($result->getResourcePath());
44+
}
45+
46+
public function testListMembers()
47+
{
48+
$result = self::$targetGroup->getMembers()->get()->top(10)->executeQuery();
49+
self::assertNotNull($result->getResourcePath());
50+
self::assertNotEmpty($result->getData());
51+
}
52+
53+
54+
public function testDeleteGroup()
55+
{
56+
self::$targetGroup->deleteObject()->executeQuery();
57+
58+
$this->expectException(RequestException::class);
59+
$this->expectExceptionCode(404);
60+
self::$targetGroup->get()->executeQuery();
61+
}
62+
63+
64+
}

tests/planner/PlannerTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Office365\Directory\Groups\Group;
77
use Office365\GraphTestCase;
88
use Office365\Planner\Plans\PlannerPlan;
9+
use Office365\Runtime\Http\RequestException;
910

1011
class PlannerTest extends GraphTestCase
1112
{
@@ -18,7 +19,11 @@ public static function setUpBeforeClass(): void
1819
{
1920
$grpName = "TestGroup_" . rand(1, 100000);
2021
parent::setUpBeforeClass();
22+
2123
self::$targetGroup = self::$graphClient->getGroups()->createM365($grpName)->executeQuery();
24+
$user = self::$graphClient->getMe();
25+
self::$targetGroup->addMemberUser($user)->executeQuery();
26+
sleep(3);
2227
}
2328

2429
public static function tearDownAfterClass(): void
@@ -40,13 +45,13 @@ public function testGetMyTasks()
4045
self::assertNotNull($result->getResourcePath());
4146
}
4247

43-
/*public function testCreateGroupPlan()
48+
public function testCreateGroupPlan()
4449
{
4550
$planTitle = "TestPlan_" . rand(1, 100000);
4651
$result = self::$targetGroup->getPlanner()->getPlans()->create($planTitle)->executeQuery();
4752
self::assertNotNull($result->getResourcePath());
4853
return $result;
49-
}*/
54+
}
5055

5156
public function testGetGroupPlans()
5257
{
@@ -58,9 +63,13 @@ public function testGetGroupPlans()
5863
* @depends testCreateGroupPlan
5964
* @param PlannerPlan $plan
6065
*/
61-
/*public function testDeleteGroupPlan(PlannerPlan $plan)
66+
public function testDeleteGroupPlan(PlannerPlan $plan)
6267
{
6368
$plan->deleteObject()->executeQuery();
64-
}*/
69+
70+
$this->expectException(RequestException::class);
71+
$this->expectExceptionCode(404);
72+
$plan->get()->executeQuery();
73+
}
6574

6675
}

0 commit comments

Comments
 (0)