Skip to content
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
4 changes: 4 additions & 0 deletions src/MsGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public function getTokenData(?string $id = null): ?MsGraphToken
{
$id = $this->getUserId($id);

if ($id === null) {
return null;
}

return MsGraphToken::where('user_id', $id)->where('refresh_token', '<>', '')->first();
}

Expand Down
9 changes: 3 additions & 6 deletions src/Resources/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ protected function getParams(array $params, int $perPage): string
{
$skip = $params['skip'] ?? 0;
$page = request('p', $skip);
if ($page > 0) {
$page--;
}

if ($params == []) {
$params = http_build_query([
Expand All @@ -62,15 +59,15 @@ protected function getParams(array $params, int $perPage): string
]);
} else {
// ensure $top, $skip and $count are part of params
if (! in_array('$top', $params)) {
if (! array_key_exists('$top', $params)) {
$params['$top'] = $perPage;
}

if (! in_array('$skip', $params)) {
if (! array_key_exists('$skip', $params)) {
$params['$skip'] = $page;
}

if (! in_array('$count', $params)) {
if (! array_key_exists('$count', $params)) {
$params['$count'] = 'true';
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Console/Commands/MsGraphAdminKeepAliveCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Dcblogdev\MsGraph\Facades\MsGraphAdmin;

test('refreshes admin token when MsGraphAdmin is connected', function () {
// Mock MsGraphAdmin::isConnected() to return true
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(true);
MsGraphAdmin::shouldReceive('getAccessToken')->once()->with(true);

$this->artisan('msgraphadmin:keep-alive')
->expectsOutput('connected')
->assertExitCode(0);
});

test('does nothing when MsGraphAdmin is not connected', function () {
// Mock MsGraphAdmin::isConnected() to return false
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(false);

$this->artisan('msgraphadmin:keep-alive')
->doesntExpectOutput('connected')
->assertExitCode(0);
});
22 changes: 22 additions & 0 deletions tests/Console/Commands/MsGraphKeepAliveCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Dcblogdev\MsGraph\Facades\MsGraph;

test('refreshes token when MsGraph is connected', function () {
// Mock MsGraph::isConnected() to return true
MsGraph::shouldReceive('isConnected')->once()->andReturn(true);
MsGraph::shouldReceive('getAccessToken')->once()->with(null, false);

$this->artisan('msgraph:keep-alive')
->expectsOutput('connected')
->assertExitCode(0);
});

test('does nothing when MsGraph is not connected', function () {
// Mock MsGraph::isConnected() to return false
MsGraph::shouldReceive('isConnected')->once()->andReturn(false);

$this->artisan('msgraph:keep-alive')
->doesntExpectOutput('connected')
->assertExitCode(0);
});
26 changes: 26 additions & 0 deletions tests/Events/NewMicrosoft365SignInEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Dcblogdev\MsGraph\Events\NewMicrosoft365SignInEvent;
use Illuminate\Support\Facades\Event;

test('NewMicrosoft365SignInEvent is dispatched with token data', function () {
Event::fake();

$tokenData = [
'accessToken' => 'fake_access_token',
'refreshToken' => 'fake_refresh_token',
'expires' => now()->addHour()->timestamp,
'info' => [
'mail' => 'test@example.com',
'displayName' => 'Test User',
],
];

// Dispatch the event
event(new NewMicrosoft365SignInEvent($tokenData));

// Assert event was dispatched
Event::assertDispatched(NewMicrosoft365SignInEvent::class, function ($event) use ($tokenData) {
return $event->token === $tokenData;
});
});
File renamed without changes.
31 changes: 31 additions & 0 deletions tests/MsGraphAdminAuthenticatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Dcblogdev\MsGraph\Facades\MsGraphAdmin;
use Dcblogdev\MsGraph\MsGraphAdminAuthenticated;
use Illuminate\Support\Facades\Route;

beforeEach(function () {
Route::middleware(MsGraphAdminAuthenticated::class)->get('/test-route', function () {
return response()->json(['message' => 'Access granted']);
});
});

test('redirects to MsGraphAdmin::connect() when not connected', function () {
// Mock MsGraphAdmin::isConnected() to return false
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(false);
MsGraphAdmin::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));

$response = $this->get('/test-route');

$response->assertRedirect('https://login.microsoftonline.com');
});

test('allows request when MsGraphAdmin is connected', function () {
// Mock MsGraphAdmin::isConnected() to return true
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(true);

$response = $this->get('/test-route');

$response->assertOk()
->assertJson(['message' => 'Access granted']);
});
31 changes: 31 additions & 0 deletions tests/MsGraphAuthenticatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Dcblogdev\MsGraph\Facades\MsGraph;
use Dcblogdev\MsGraph\MsGraphAuthenticated;
use Illuminate\Support\Facades\Route;

beforeEach(function () {
Route::middleware(MsGraphAuthenticated::class)->get('/test-route', function () {
return response()->json(['message' => 'Access granted']);
});
});

test('redirects to MsGraph::connect() when not connected', function () {
// Mock MsGraph::isConnected() to return false
MsGraph::shouldReceive('isConnected')->once()->andReturn(false);
MsGraph::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));

$response = $this->get('/test-route');

$response->assertRedirect('https://login.microsoftonline.com');
});

test('allows request when MsGraph is connected', function () {
// Mock MsGraph::isConnected() to return true
MsGraph::shouldReceive('isConnected')->once()->andReturn(true);

$response = $this->get('/test-route');

$response->assertOk()
->assertJson(['message' => 'Access granted']);
});
183 changes: 183 additions & 0 deletions tests/Resources/ContactsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

use Dcblogdev\MsGraph\Facades\MsGraph;
use Dcblogdev\MsGraph\Resources\Contacts;

beforeEach(function () {
MsGraph::shouldReceive('isConnected')->andReturn(true);
$this->contacts = new Contacts;
});

test('get contacts with params', function () {

$messageQueryParams = [
'$orderby' => 'displayName',
'$skip' => 0,
'$top' => 5,
'$count' => 'true',
];

MsGraph::shouldReceive('get')
->with('me/contacts?'.http_build_query($messageQueryParams))
->andReturn([
'@odata.count' => 10,
'value' => [
['id' => '1', 'displayName' => 'John Doe'],
['id' => '2', 'displayName' => 'Jane Doe'],
],
]);

$response = (new Contacts)->get($messageQueryParams);

expect($response)->toHaveKeys(['contacts', 'total', 'links', 'links_array'])
->and($response['contacts']['value'])->toBeArray()
->and($response['total'])->toBe(10);
});

test('getParams returns default values when no params provided', function () {

$contacts = new Contacts;
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
$response = $reflection->invoke($contacts, [], 25);

parse_str($response, $parsedParams);

expect($parsedParams)->toMatchArray([
'$orderby' => 'displayName',
'$top' => '25',
'$skip' => '0',
'$count' => 'true',
]);
});

test('getParams includes custom top parameter', function () {
$contacts = new Contacts;
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
$response = $reflection->invoke($contacts, ['$top' => 10], 25);

parse_str($response, $parsedParams);

expect($parsedParams)->toMatchArray([
'$top' => '10',
'$skip' => '0',
'$count' => 'true',
]);
});

test('getParams includes custom skip parameter', function () {
$contacts = new Contacts;
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
$response = $reflection->invoke($contacts, ['$skip' => 15], 25);

parse_str($response, $parsedParams);

expect($parsedParams)->toMatchArray([
'$top' => '25',
'$skip' => '15',
'$count' => 'true',
]);
});

test('getParams forces count to be true when missing', function () {
$contacts = new Contacts;
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
$response = $reflection->invoke($contacts, ['$top' => 10, '$skip' => 5], 25);

parse_str($response, $parsedParams);

expect($parsedParams)->toMatchArray([
'$top' => '10',
'$skip' => '5',
'$count' => 'true',
]);
});

test('find method retrieves a specific contact', function () {

MsGraph::shouldReceive('get')
->with('me/contacts/1')
->andReturn([
'id' => '1',
'displayName' => 'John Doe',
'email' => 'johndoe@example.com',
]);

$response = (new Contacts)->find('1');

expect($response)
->toHaveKeys(['id', 'displayName', 'email'])
->and($response['id'])->toBe('1')
->and($response['displayName'])->toBe('John Doe')
->and($response['email'])->toBe('johndoe@example.com');
});

test('can create contact', function () {

$data = [
'displayName' => 'John Doe',
'givenName' => 'John Doe',
'emailAddresses' => [
[
'address' => 'john@doe.com',
'name' => 'John Doe',
],
],
];

MsGraph::shouldReceive('post')
->with('me/contacts', $data)
->andReturn([
'id' => '1',
'displayName' => 'John Doe',
'email' => 'johndoe@example.com',
]);

$response = (new Contacts)->store($data);

expect($response)
->toHaveKeys(['id', 'displayName', 'email'])
->and($response['id'])->toBe('1')
->and($response['displayName'])->toBe('John Doe')
->and($response['email'])->toBe('johndoe@example.com');
});

test('can update contact', function () {

$data = [
'displayName' => 'John Doe',
'givenName' => 'John Doe',
'emailAddresses' => [
[
'address' => 'john@doe.com',
'name' => 'John Doe',
],
],
];

MsGraph::shouldReceive('patch')
->with('me/contacts/1', $data)
->andReturn([
'id' => '1',
'displayName' => 'John Doe',
'email' => 'johndoe@example.com',
]);

$response = (new Contacts)->update(1, $data);

expect($response)
->toHaveKeys(['id', 'displayName', 'email'])
->and($response['id'])->toBe('1')
->and($response['displayName'])->toBe('John Doe')
->and($response['email'])->toBe('johndoe@example.com');
});

test('can delete contact', function () {

MsGraph::shouldReceive('delete')
->with('me/contacts/1')
->andReturn('');

$response = (new Contacts)->delete(1);

expect($response)->toBe('');
});