diff --git a/src/MsGraph.php b/src/MsGraph.php index e21e139..cae0207 100755 --- a/src/MsGraph.php +++ b/src/MsGraph.php @@ -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(); } diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index 30dc852..60ab58e 100644 --- a/src/Resources/Contacts.php +++ b/src/Resources/Contacts.php @@ -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([ @@ -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'; } diff --git a/tests/Console/Commands/MsGraphAdminKeepAliveCommandTest.php b/tests/Console/Commands/MsGraphAdminKeepAliveCommandTest.php new file mode 100644 index 0000000..17d2de7 --- /dev/null +++ b/tests/Console/Commands/MsGraphAdminKeepAliveCommandTest.php @@ -0,0 +1,22 @@ +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); +}); diff --git a/tests/Console/Commands/MsGraphKeepAliveCommandTest.php b/tests/Console/Commands/MsGraphKeepAliveCommandTest.php new file mode 100644 index 0000000..bae2320 --- /dev/null +++ b/tests/Console/Commands/MsGraphKeepAliveCommandTest.php @@ -0,0 +1,22 @@ +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); +}); diff --git a/tests/Events/NewMicrosoft365SignInEventTest.php b/tests/Events/NewMicrosoft365SignInEventTest.php new file mode 100644 index 0000000..e66fa54 --- /dev/null +++ b/tests/Events/NewMicrosoft365SignInEventTest.php @@ -0,0 +1,26 @@ + '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; + }); +}); diff --git a/tests/PaginatorTest.php b/tests/Helpers/PaginatorTest.php similarity index 100% rename from tests/PaginatorTest.php rename to tests/Helpers/PaginatorTest.php diff --git a/tests/MsGraphAdminAuthenticatedTest.php b/tests/MsGraphAdminAuthenticatedTest.php new file mode 100644 index 0000000..a48abea --- /dev/null +++ b/tests/MsGraphAdminAuthenticatedTest.php @@ -0,0 +1,31 @@ +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']); +}); diff --git a/tests/MsGraphAuthenticatedTest.php b/tests/MsGraphAuthenticatedTest.php new file mode 100644 index 0000000..67c50c9 --- /dev/null +++ b/tests/MsGraphAuthenticatedTest.php @@ -0,0 +1,31 @@ +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']); +}); diff --git a/tests/Resources/ContactsTest.php b/tests/Resources/ContactsTest.php new file mode 100644 index 0000000..556651c --- /dev/null +++ b/tests/Resources/ContactsTest.php @@ -0,0 +1,183 @@ +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(''); +});