Skip to content

Commit f5c2604

Browse files
SharePoint API changes: Web new methods, adjust unit test for tenant settings
1 parent b318737 commit f5c2604

15 files changed

+109
-57
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ The following example demonstrates how to send a message via Outlook Mail API:
211211
$message->Subject = "Meet for lunch?";
212212
$message->Body = new ItemBody(BodyType::Text,"The new cafeteria is open.");
213213
$message->ToRecipients = array(
214-
new Recipient(new EmailAddress(null,"jdoe@contoso.onmicrosoft.com"))
214+
new Recipient(new EmailAddress(null,"{username}@{tenant_prefix}.onmicrosoft.com"))
215215
);
216216
$client->getMe()->sendEmail($message,true);
217217
$client->executeQuery();

Settings.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
<?php
22

33
$secure_vars = explode(";",getenv("phpSPO_secure_vars"));
4+
$parts = explode('@', $secure_vars[0]);
5+
$domain_parts = explode('.', $parts[1]);
6+
$tenant_prefix = $domain_parts[0];
7+
48
return array(
5-
'TenantName' => "mediadev8.onmicrosoft.com",
6-
'Url' => "https://mediadev8.sharepoint.com",
7-
'OneDriveUrl' => "https://mediadev8-my.sharepoint.com",
9+
'TenantName' => "{$tenant_prefix}.onmicrosoft.com",
10+
'Url' => "https://{$tenant_prefix}.sharepoint.com",
11+
'OneDriveUrl' => "https://{$tenant_prefix}-my.sharepoint.com",
12+
'AdminTenantUrl' => "https://{$tenant_prefix}-admin.sharepoint.com",
813
'Password' => $secure_vars[1],
914
'UserName' => $secure_vars[0],
1015
'ClientId' => "4b7eb3df-afc3-4b7d-ae1d-629f22a3fe42",
1116
'ClientSecret' => $secure_vars[2],
12-
'RedirectUrl' => "https://mediadev8.sharepoint.com"
17+
'RedirectUrl' => "https://{$tenant_prefix}.sharepoint.com",
18+
'TestAccountName' => "jdoe2@{$tenant_prefix}.onmicrosoft.com",
19+
'TestAltAccountName' => "wellis2@{$tenant_prefix}.onmicrosoft.com"
1320
);
1421

1522

@@ -18,3 +25,5 @@
1825

1926

2027

28+
29+

examples/SharePoint/CreateModernSite.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Description" => "Description",
2626
"WebTemplate" => "SITEPAGEPUBLISHING#0",
2727
"SiteDesignId" => "6142d2a0-63a5-4ba0-aede-d9fefca2c767",
28-
"Owner" => "[email protected]"
28+
"Owner" => $settings['TestAccounts'][0]
2929
)
3030
);
3131
$request->Data = json_encode($payload);

src/SharePoint/ListItem.php

+2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ public function update()
7575
$this->ensureTypeName($this->getParentList());
7676
$qry = new UpdateEntityQuery($this);
7777
$this->getContext()->addQueryAndResultObject($qry, $this);
78+
return $this;
7879
}
7980
public function deleteObject()
8081
{
8182
$qry = new DeleteEntityQuery($this);
8283
$this->getContext()->addQuery($qry);
84+
return $this;
8385
}
8486

8587

src/SharePoint/Principal.php

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Office365\SharePoint;
77

8+
use Office365\Runtime\ResourcePathServiceOperation;
9+
810
/**
911
* Specifies
1012
* a base type that represents a user or group that can be
@@ -130,4 +132,16 @@ public function setPrincipalType($value)
130132
{
131133
$this->setProperty("PrincipalType", $value, true);
132134
}
135+
136+
public function setProperty($name, $value, $persistChanges = true)
137+
{
138+
parent::setProperty($name, $value, $persistChanges);
139+
if (is_null($this->resourcePath)) {
140+
if ($name === "Id") {
141+
$this->resourcePath = new ResourcePathServiceOperation("GetById", array($value),
142+
$this->parentCollection->resourcePath);
143+
}
144+
}
145+
return $this;
146+
}
133147
}

src/SharePoint/Utilities/Utility.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ class Utility
2222
*/
2323
public static function createNewDiscussion(SPList $list, $title)
2424
{
25+
$ctx = $list->getContext();
2526
$discussionPayload = array(
2627
"Title" => $title,
2728
"FileSystemObjectType" => 1
2829
);
2930
$item = $list->addItem($discussionPayload);
30-
$item->getContext()->executeQuery();
31-
//fix discussion folder name
32-
$item->setProperty("FileLeafRef",$title);
33-
$item->update();
34-
$item->getContext()->executeQuery();
31+
$ctx->getPendingRequest()->afterExecuteRequest(function () use ($item,$title, $ctx){
32+
//fix discussion folder name
33+
$item->setProperty("FileLeafRef",$title);
34+
$item->update();
35+
},true);
3536
return $item;
3637
}
3738

src/SharePoint/Web.php

+13
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ public function deleteObject()
102102
$this->removeFromParentCollection();
103103
return $this;
104104
}
105+
106+
/**
107+
* @param string $logonName
108+
* @return User
109+
*/
110+
public function ensureUser($logonName){
111+
$returnType = new User($this->context);
112+
$this->getSiteUsers()->addChild($returnType);
113+
$qry = new InvokePostMethodQuery($this, "EnsureUser", [$logonName], null, null);
114+
$this->getContext()->addQueryAndResultObject($qry, $returnType);
115+
return $returnType;
116+
}
117+
105118
/**
106119
* Returns the collection of all changes from the change log that have occurred within the scope of the site,
107120
* based on the specified query.

tests/GraphTestCase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static function tearDownAfterClass()
3434

3535
/**
3636
* @param AuthenticationContext $authCtx
37-
* @param $clientId
38-
* @param $userName
39-
* @param $password
37+
* @param string $clientId
38+
* @param string $userName
39+
* @param string $password
4040
* @throws Exception
4141
*/
4242
public static function acquireToken(AuthenticationContext $authCtx, $clientId, $userName, $password)

tests/outlookservices/OutlookMailTest.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
use Office365\OutlookServices\OutlookEntity;
1212
use Office365\OutlookServices\Recipient;
1313

14-
//require_once('OutlookServicesTestCase.php');
15-
16-
1714
class OutlookMailTest extends OutlookServicesTestCase
1815
{
1916

@@ -128,7 +125,7 @@ public function testGetMessages(Message $message){
128125
public function testForwardMessage(Message $message)
129126
{
130127
$recipients = array(
131-
new Recipient(new EmailAddress("",self::$testUserAccount))
128+
new Recipient(new EmailAddress("",self::$testAccountName))
132129
);
133130
$message->forward("For your consideration",$recipients);
134131
self::$context->executeQuery();

tests/outlookservices/OutlookServicesTestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class OutlookServicesTestCase extends TestCase
1818
/**
1919
* @var string
2020
*/
21-
protected static $testUserAccount;
21+
protected static $testAccountName;
2222

2323
public static function setUpBeforeClass()
2424
{
@@ -33,7 +33,7 @@ public static function setUpBeforeClass()
3333
print("Failed to acquire token");
3434
}
3535
});
36-
self::$testUserAccount = "[email protected]";
36+
self::$testAccountName = $settings['TestAccountName'];
3737
}
3838

3939
public static function tearDownAfterClass()

tests/sharepoint/PeopleManagerTest.php

+31-17
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,76 @@
88
class PeopleManagerTest extends SharePointTestCase
99
{
1010

11-
private static $accountName = "i:0#.f|membership|[email protected]";
11+
/**
12+
* @var SharePoint\User
13+
*/
14+
private static $testUser;
15+
16+
public static function setUpBeforeClass()
17+
{
18+
parent::setUpBeforeClass();
19+
self::$testUser = self::$context->getWeb()->ensureUser(self::$testAccountName)->executeQuery();
20+
}
1221

13-
public function testGetMyProperties(){
22+
public function testGetMyProperties()
23+
{
1424
$peopleManager = new PeopleManager(self::$context);
1525
$properties = $peopleManager->getMyProperties()->get()->executeQuery();
1626
$this->assertNotNull($properties->getAccountName());
1727
}
1828

1929

20-
public function testGetUserProfilePropertyFor(){
30+
public function testGetUserProfilePropertyFor()
31+
{
2132
$peopleManager = new PeopleManager(self::$context);
22-
$result = $peopleManager->getUserProfilePropertyFor(self::$accountName,"AccountName");
33+
$result = $peopleManager->getUserProfilePropertyFor(self::$testUser->getLoginName(), "AccountName");
2334
self::$context->executeQuery();
2435
$this->assertNotNull($result->getValue());
2536
}
2637

2738

28-
public function testFollow(){
39+
public function testFollow()
40+
{
2941
$peopleManager = new PeopleManager(self::$context);
3042

31-
$result = $peopleManager->amIFollowing(self::$accountName);
43+
$result = $peopleManager->amIFollowing(self::$testUser->getLoginName());
3244
self::$context->executeQuery();
3345

34-
if($result->getValue() == false){
35-
$peopleManager->follow(self::$accountName);
46+
if ($result->getValue() == false) {
47+
$peopleManager->follow(self::$testUser->getLoginName());
3648
self::$context->executeQuery();
3749
}
3850

39-
$propertiesList = $peopleManager->getFollowersFor(self::$accountName);
51+
$propertiesList = $peopleManager->getFollowersFor(self::$testUser->getLoginName());
4052
self::$context->load($propertiesList);
4153
self::$context->executeQuery();
4254

43-
self::assertGreaterThanOrEqual(1,$propertiesList->getCount());
55+
self::assertGreaterThanOrEqual(1, $propertiesList->getCount());
4456
}
4557

46-
public function testStopFollowing(){
58+
public function testStopFollowing()
59+
{
4760
$peopleManager = new PeopleManager(self::$context);
4861

49-
$result = $peopleManager->amIFollowing(self::$accountName);
62+
$result = $peopleManager->amIFollowing(self::$testUser->getLoginName());
5063
self::$context->executeQuery();
5164

52-
if($result->getValue() == true){
53-
$peopleManager->stopFollowing(self::$accountName);
65+
if ($result->getValue() == true) {
66+
$peopleManager->stopFollowing(self::$testUser->getLoginName());
5467
self::$context->executeQuery();
5568
}
5669

5770

58-
$result2 = $peopleManager->amIFollowing(self::$accountName);
71+
$result2 = $peopleManager->amIFollowing(self::$testUser->getLoginName());
5972
self::$context->executeQuery();
6073
self::assertFalse($result2->getValue());
6174
}
6275

6376

64-
public function testAmIFollowedBy(){
77+
public function testAmIFollowedBy()
78+
{
6579
$peopleManager = new PeopleManager(self::$context);
66-
$result = $peopleManager->amIFollowedBy(self::$accountName);
80+
$result = $peopleManager->amIFollowedBy(self::$testUser->getLoginName());
6781
self::$context->executeQuery();
6882
self::assertNotNull($result->getValue());
6983
}

tests/sharepoint/RoleTest.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,13 @@ public function testGetRoleDefinition()
5454
* @return RoleAssignment
5555
*/
5656
public function testAddRoleAssignment(RoleDefinition $targetRole){
57-
//get site user
58-
$usersResult = self::$context->getWeb()->getSiteUsers()->filter("Title eq 'Marta Doe'");
59-
self::$context->load($usersResult);
60-
self::$context->executeQuery();
61-
self::assertEquals(1,$usersResult->getCount());
57+
$users = self::$context->getWeb()->getSiteUsers()->filter("Title eq 'Jon Doe'")->get()->executeQuery();
58+
self::assertGreaterThanOrEqual(1,$users->getCount());
6259

63-
self::$securedTargetObject->getRoleAssignments()->addRoleAssignment($usersResult->getItem(0)->getProperty("Id"),$targetRole->getProperty("Id"));
60+
self::$securedTargetObject->getRoleAssignments()->addRoleAssignment($users->getItem(0)->getProperty("Id"),$targetRole->getProperty("Id"));
6461
self::$context->executeQuery();
6562

66-
$roleAssignment = self::$securedTargetObject->getRoleAssignments()->getByPrincipalId($usersResult->getItem(0)->getProperty("Id"));
63+
$roleAssignment = self::$securedTargetObject->getRoleAssignments()->getByPrincipalId($users->getItem(0)->getProperty("Id"));
6764
self::$context->load($roleAssignment);
6865
self::$context->executeQuery();
6966
self::assertNotNull($roleAssignment);
@@ -78,20 +75,15 @@ public function testRemoveRoleAssignment(RoleAssignment $roleAssignment)
7875
{
7976
$roleDef = self::$context->getWeb()->getRoleDefinitions()->getByName("Edit"); //get role definition by name
8077
self::$context->load($roleDef);
81-
$roleAssignmentsBefore = self::$securedTargetObject->getRoleAssignments();
82-
self::$context->load($roleAssignmentsBefore);
83-
self::$context->executeQuery();
78+
$roleAssignmentsBefore = self::$securedTargetObject->getRoleAssignments()->get()->executeQuery();
8479
self::assertNotNull($roleDef);
8580

8681
$rolesCount = $roleAssignmentsBefore->getCount();
8782
self::$securedTargetObject->getRoleAssignments()->removeRoleAssignment($roleAssignment->getPrincipalId(),$roleDef->getProperty("Id"));
8883
self::$context->executeQuery();
8984

90-
$roleAssignmentsAfter = self::$securedTargetObject->getRoleAssignments();
91-
self::$context->load($roleAssignmentsAfter);
92-
self::$context->executeQuery();
85+
$roleAssignmentsAfter = self::$securedTargetObject->getRoleAssignments()->get()->executeQuery();
9386
self::assertEquals($roleAssignmentsAfter->getCount(),$rolesCount -1);
9487
}
9588

96-
9789
}

tests/sharepoint/SharePointTestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ abstract class SharePointTestCase extends TestCase
2121
*/
2222
protected static $context;
2323

24-
protected static $testLoginName = "i:0#.f|membership|[email protected]";
24+
protected static $testAccountName;
2525

2626
public static function setUpBeforeClass()
2727
{
2828
$settings = include(__DIR__ . '/../../Settings.php');
29+
self::$testAccountName = $settings['TestAccountName'];
2930
self::$context = (new ClientContext($settings['Url']))
3031
->withCredentials(new UserCredentials($settings['UserName'],$settings['Password']));
3132
}
@@ -131,5 +132,4 @@ public static function createListItem(SPList $list, array $itemProperties){
131132
$ctx->executeQuery();
132133
return $item;
133134
}
134-
135135
}

tests/sharepoint/UserTest.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ public function testFindGroup(Group $group)
6666
*/
6767
public function testAddUserIntoGroup(Group $group)
6868
{
69-
$user = $group->getUsers()->addUser(self::$testLoginName);
69+
$siteUser = self::$context->getWeb()->ensureUser(self::$testAccountName)->executeQuery();
70+
$user = $group->getUsers()->addUser($siteUser->getLoginName());
7071
self::$context->executeQuery();
7172
$this->assertNotNull($user->getId());
73+
}
7274

75+
/**
76+
* @depends testCreateGroup
77+
* @param Group $group
78+
* @throws Exception
79+
*/
80+
public function testFindUserInGroup(Group $group)
81+
{
7382
$groupUsers = $group->getUsers()->get()->executeQuery();
74-
$result = $groupUsers->findFirst("LoginName",self::$testLoginName);
83+
$result = $groupUsers->findFirst("UserPrincipalName",self::$testAccountName);
7584
$this->assertNotNull($result);
7685
}
7786

tests/sharepoint/UtilityTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public static function tearDownAfterClass()
3232
public function testCreateNewDiscussion()
3333
{
3434
$topicTitle = self::createUniqueName("Topic");
35-
$discussion = Utility::createNewDiscussion(self::$discussionsList,$topicTitle);
36-
self::assertEquals($discussion->getProperty("FileLeafRef"),$topicTitle);
35+
$discussion = Utility::createNewDiscussion(self::$discussionsList,$topicTitle)->executeQuery();
36+
$targetItem = $discussion->select("FileLeafRef")->get()->executeQuery();
37+
self::assertEquals($targetItem->getProperty("FileLeafRef"),$topicTitle);
3738
return $discussion;
3839
}
3940

0 commit comments

Comments
 (0)