-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathClientContextTest.php
89 lines (71 loc) · 2.93 KB
/
ClientContextTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Office365;
use Exception;
use Office365\Runtime\Auth\UserCredentials;
use Office365\Runtime\Http\RequestOptions;
use Office365\SharePoint\ClientContext;
class ClientContextTest extends SharePointTestCase
{
public function testIfSingleRequestProcessed()
{
try{
$listTitle = "Orders_" . rand(1,100000);
$list = self::$context->getWeb()->getLists()->getByTitle($listTitle);
self::$context->load($list);
self::$context->executeQuery();
self::assertFalse(self::$context->hasPendingRequest());
}
catch(Exception $e){
self::assertFalse(self::$context->hasPendingRequest());
}
}
public function testIfMultipleRequestsProcessed()
{
$numOfQueries = 2;
try {
for ($i = 0;$i < $numOfQueries; $i++) {
$listTitle = "Orders_" . rand(1, 100000);
$list = self::$context->getWeb()->getLists()->getByTitle($listTitle);
self::$context->load($list);
}
self::$context->executeQuery();
self::assertFalse(self::$context->hasPendingRequest());
} catch (Exception $e) {
self::assertTrue(self::$context->hasPendingRequest());
self::$context->clearActions();
}
}
public function testBuildGetRequest(){
$request = self::$context->getWeb()->getCurrentUser()->get()->buildRequest();
self::assertInstanceOf(RequestOptions::class, $request);
self::$context->clearActions();
}
public function testBuildUpdateRequest(){
$request = self::$context->getWeb()->getCurrentUser()->update()->buildRequest();
self::assertInstanceOf(RequestOptions::class, $request);
self::$context->clearActions();
}
public function testBuildDeleteRequest(){
$request = self::$context->getWeb()->deleteObject()->buildRequest();
self::assertInstanceOf(RequestOptions::class, $request);
self::$context->clearActions();
}
public function testEnsureScalarProperty(){
$web = self::$context->getWeb();
$web->ensureProperty("Title", function () use ($web){
self::assertEquals("Title",$web->getQueryOptions()->Select);
});
self::$context->executeQuery();
}
public function testEnsureNavigationProperty(){
$web = self::$context->getWeb()->ensureProperty("CurrentUser")->executeQuery();
self::assertTrue($web->isPropertyAvailable("CurrentUser"));
}
public function testInitClientFromAbsUrl(){
$pageAbsUrl = self::$settings["Url"] . "/sites/team/SitePages/Home.aspx";
$credentials = new UserCredentials(self::$settings['UserName'],self::$settings['Password']);
$ctx = ClientContext::fromUrl($pageAbsUrl)->withCredentials($credentials);
$me = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
self::assertNotEmpty($me->getEmail());
}
}