Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Fix bug with no LinkedIn email addresses #355

Merged
merged 1 commit into from
Mar 29, 2019
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: 2 additions & 2 deletions src/Two/LinkedInProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function getBasicProfile($token)
],
]);

return json_decode($response->getBody(), true);
return (array) json_decode($response->getBody(), true);
}

/**
Expand All @@ -95,7 +95,7 @@ protected function getEmailAddress($token)
],
]);

return json_decode($response->getBody(), true)['elements'][0]['handle~'];
return (array) Arr::get((array) json_decode($response->getBody(), true), 'elements.0.handle~');
}

/**
Expand Down
62 changes: 62 additions & 0 deletions tests/LinkedInProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Laravel\Socialite\Tests;

use Mockery as m;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Laravel\Socialite\Two\User;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Laravel\Socialite\Two\LinkedInProvider;

class LinkedInProviderTest extends TestCase
{
protected function tearDown()
{
parent::tearDown();

m::close();
}

public function test_it_can_map_a_user_without_an_email_address()
{
$request = m::mock(Request::class);
$request->shouldReceive('input')->with('code')->andReturn('fake-code');

$accessTokenResponse = m::mock(ResponseInterface::class);
$accessTokenResponse->shouldReceive('getBody')->andReturn(json_encode(['access_token' => 'fake-token']));

$basicProfileResponse = m::mock(ResponseInterface::class);
$basicProfileResponse->shouldReceive('getBody')->andReturn(json_encode(['id' => $userId = 1]));

// Make sure email address response contains no values.
$emailAddressResponse = m::mock(ResponseInterface::class);
$emailAddressResponse->shouldReceive('getBody')->andReturn(json_encode(['elements' => []]));

$guzzle = m::mock(Client::class);
$guzzle->shouldReceive('post')->once()->andReturn($accessTokenResponse);
$guzzle->shouldReceive('get')->with('https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))', [
'headers' => [
'Authorization' => 'Bearer fake-token',
'X-RestLi-Protocol-Version' => '2.0.0',
],
])->andReturn($basicProfileResponse);
$guzzle->shouldReceive('get')->with('https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))', [
'headers' => [
'Authorization' => 'Bearer fake-token',
'X-RestLi-Protocol-Version' => '2.0.0',
],
])->andReturn($emailAddressResponse);

$provider = new LinkedInProvider($request, 'client_id', 'client_secret', 'redirect');
$provider->stateless();
$provider->setHttpClient($guzzle);

$user = $provider->user();

$this->assertInstanceOf(User::class, $user);
$this->assertEquals($userId, $user->getId());
$this->assertEquals(null, $user->getEmail());
}
}