Skip to content

Commit 5dd7016

Browse files
committed
Planner API new types and methods, introduced unit tests
1 parent 13d81aa commit 5dd7016

File tree

6 files changed

+123
-20
lines changed

6 files changed

+123
-20
lines changed

examples/Planner/ExportPlans.php renamed to examples/Planner/GetAssignedTasksReport.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Office365\Directory\Groups\Group;
1616
use Office365\GraphServiceClient;
1717
use Office365\Planner\Plans\PlannerPlan;
18+
use Office365\Planner\Tasks\PlannerTask;
1819

1920
require_once '../vendor/autoload.php';
2021

@@ -26,12 +27,33 @@
2627

2728
/** @var Group $grp */
2829
foreach ($groups as $grp) {
29-
echo sprintf("\nProcessing group: %s \n", $grp->getProperty("DisplayName"));
30+
$groupName = $grp->getProperty("DisplayName");
31+
echo "\nChecking group: {$groupName}\n";
32+
3033
$plans = $grp->getPlanner()->getPlans()->get()->executeQuery();
3134

3235
/** @var PlannerPlan $plan */
3336
foreach ($plans as $plan) {
34-
echo sprintf("\tPlan: %s \n", $plan->getProperty("Title"));
37+
$planName = $plan->getProperty("Title");
38+
echo " - Plan: {$planName}\n";
39+
40+
// Get all tasks in this plan
41+
$tasks = $plan->getTasks()->get()->executeQuery();
42+
43+
/** @var PlannerTask $task */
44+
foreach ($tasks as $task) {
45+
$assignments = $task->getAssignments();
46+
47+
// Check each assignment to see if it matches our target users
48+
foreach ($assignments as $userId => $assignment) {
49+
50+
echo " - Assignment: {$userId}\n";
51+
52+
}
53+
}
54+
55+
56+
3557
}
3658
}
3759

src/Planner/PlannerUser.php

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

88
use Office365\Entity;
9-
use Office365\EntityCollection;
10-
use Office365\Planner\Plans\PlannerPlan;
9+
use Office365\Planner\Plans\PlannerPlanCollection;
1110
use Office365\Planner\Tasks\PlannerTaskCollection;
1211
use Office365\Runtime\ResourcePath;
1312

@@ -24,12 +23,12 @@ public function getTasks()
2423
}
2524

2625
/**
27-
* @return EntityCollection
26+
* @return PlannerPlanCollection
2827
*/
2928
public function getPlans()
3029
{
3130
return $this->getProperty("Plans",
32-
new EntityCollection($this->getContext(),
33-
new ResourcePath("Plans", $this->getResourcePath()),PlannerPlan::class));
31+
new PlannerPlanCollection($this->getContext(),
32+
new ResourcePath("Plans", $this->getResourcePath()),$this));
3433
}
3534
}

src/Planner/Plans/PlannerPlanCollection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePa
3030
*/
3131
public function create($title){
3232
/** @var PlannerPlan $returnType */
33-
$containerUrl = $this->getContainerUrl();
3433
$returnType = $this->getContext()->getPlanner()->getPlans()->add();
3534
$returnType->setTitle($title);
36-
$returnType->setProperty("container", ["url" => $containerUrl]);
35+
$returnType->setProperty("container", $this->getContainer());
3736
return $returnType;
3837
}
3938

40-
public function getContainerUrl()
39+
public function getContainer()
4140
{
4241
if ($this->parent === null) {
4342
throw new \RuntimeException("Parent resource is not available");
@@ -48,7 +47,7 @@ public function getContainerUrl()
4847
$resourceUrl = substr($resourceUrl, 0, -8);
4948
}
5049

51-
return $resourceUrl;
50+
return ["url" => $resourceUrl];
5251
}
5352

5453
}

src/Planner/Tasks/PlannerTaskCollection.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,58 @@
22

33
namespace Office365\Planner\Tasks;
44

5+
use Office365\Entity;
56
use Office365\EntityCollection;
7+
use Office365\Planner\Plans\PlannerPlan;
68
use Office365\Runtime\ClientRuntimeContext;
79
use Office365\Runtime\ResourcePath;
810

911
class PlannerTaskCollection extends EntityCollection {
1012

11-
public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePath = null)
13+
/**
14+
* @var Entity|null
15+
*/
16+
private $parent;
17+
18+
public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePath = null, ?Entity $parent=null)
1219
{
1320
parent::__construct($ctx, $resourcePath, PlannerTask::class);
21+
$this->parent = $parent;
22+
}
23+
24+
/**
25+
* Create a new plannerTask.
26+
* @param string $title
27+
* @param string|null $planId
28+
* @param string|null $bucketId
29+
* @param array $assignments
30+
* @return PlannerTask
31+
* @throws \Exception
32+
*/
33+
public function create($title, $planId=null, $bucketId=null, $assignments=[]){
34+
/** @var PlannerTask $returnType */
35+
if ($this->parent === null && $planId === null) {
36+
throw new \Exception("planId is mandatory when creating a task without a parent");
37+
}
38+
39+
$returnType = $this->getContext()->getPlanner()->getTasks()->add();
40+
$returnType->setTitle($title);
41+
42+
if ($this->parent instanceof PlannerPlan) {
43+
$this->parent->ensureProperty("Id", function () use ($returnType) {
44+
$returnType->setProperty("planId", $this->parent->getId());
45+
});
46+
}
47+
else {
48+
$returnType->setProperty("planId", null);
49+
}
50+
51+
52+
$returnType->setProperty("bucketId", null);
53+
if (!empty($assignments)) {
54+
$returnType->setProperty("assignments", $assignments);
55+
}
56+
return $returnType;
1457
}
1558

1659
}

tests/planner/PlannerTasksTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Office365;
4+
5+
6+
use Office365\Directory\Groups\Group;
7+
use Office365\GraphTestCase;
8+
use Office365\Planner\Plans\PlannerPlan;
9+
use Office365\Runtime\Http\RequestException;
10+
11+
class PlannerTasksTest extends GraphTestCase
12+
{
13+
14+
/**
15+
* @var PlannerPlan
16+
*/
17+
private static $targetPlan;
18+
19+
public static function setUpBeforeClass(): void
20+
{
21+
$title = "TestPlan_" . rand(1, 100000);
22+
parent::setUpBeforeClass();
23+
$currentUser = self::$graphClient->getUsers()->getByUserPrincipalName(self::$settings['UserName']);
24+
self::$targetPlan = $currentUser->getPlanner()->getPlans()->create($title)->executeQuery();
25+
}
26+
27+
public static function tearDownAfterClass(): void
28+
{
29+
self::$targetPlan->deleteObject()->executeQuery();
30+
parent::tearDownAfterClass();
31+
}
32+
33+
public function testCreateMyTask()
34+
{
35+
$result = self::$targetPlan->getTasks()->create("Update client list")->executeQuery();
36+
self::assertNotNull($result->getResourcePath());
37+
}
38+
39+
40+
public function testGetMyTasks()
41+
{
42+
$result = self::$targetPlan->getTasks()->get()->executeQuery();
43+
self::assertNotNull($result->getResourcePath());
44+
self::assertNotEmpty($result->getData());
45+
}
46+
47+
}

tests/planner/PlannerTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function setUpBeforeClass(): void
2323
self::$targetGroup = self::$graphClient->getGroups()->createM365($grpName)->executeQuery();
2424
$user = self::$graphClient->getMe();
2525
self::$targetGroup->addMemberUser($user)->executeQuery();
26-
sleep(3);
26+
sleep(5);
2727
}
2828

2929
public static function tearDownAfterClass(): void
@@ -38,13 +38,6 @@ public function testGetMyPlans()
3838
self::assertNotNull($result->getResourcePath());
3939
}
4040

41-
42-
public function testGetMyTasks()
43-
{
44-
$result = self::$graphClient->getMe()->getPlanner()->getTasks()->get()->executeQuery();
45-
self::assertNotNull($result->getResourcePath());
46-
}
47-
4841
public function testCreateGroupPlan()
4942
{
5043
$planTitle = "TestPlan_" . rand(1, 100000);

0 commit comments

Comments
 (0)