class OauthTest in Media: Acquia DAM 8
Oauth test.
@group media_acquiadam
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\media_acquiadam\Unit\OauthTest uses AcquiadamConfigTrait
Expanded class hierarchy of OauthTest
File
- tests/
src/ Unit/ OauthTest.php, line 22
Namespace
Drupal\Tests\media_acquiadam\UnitView source
class OauthTest extends UnitTestCase {
use AcquiadamConfigTrait, AcquiadamLoggerFactoryTrait;
/**
* Container builder helper.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* Media: Acquia DAM oAuth client.
*
* @var \Drupal\media_acquiadam\Oauth
*/
protected $oAuthClient;
/**
* Validates the auth link that gets created.
*/
public function testGetAuthLink() {
$authUrl = $this->oAuthClient
->getAuthLink();
$this
->assertStringContainsString('some/url/test', $authUrl);
$this
->assertStringContainsString('testToken112233', $authUrl);
$this
->assertStringContainsString('WDclient-id', $authUrl);
$this
->assertStringContainsString('/oauth2/authorize', $authUrl);
}
/**
* Validates that the redirect URL gets generated correctly.
*/
public function testGetSetAuthFinishRedirect() {
$this
->assertNull($this->oAuthClient
->getAuthFinishRedirect());
$this->oAuthClient
->setAuthFinishRedirect('https://example.com/sub/path?original_path=should-be-dropped&extra=1');
$this
->assertSame('https://example.com/sub/path?extra=1', $this->oAuthClient
->getAuthFinishRedirect());
}
/**
* Validates that the access token response has the necessary keys.
*/
public function testGetAccessToken() {
$token = $this->oAuthClient
->getAccessToken('somedummycode123');
$this
->assertArrayHasKey('expire_time', $token);
$this
->assertArrayHasKey('access_token', $token);
$this
->assertNotEmpty($token['access_token']);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$csrf_token = $this
->getMockBuilder(CsrfTokenGenerator::class)
->disableOriginalConstructor()
->getMock();
$csrf_token
->expects($this
->any())
->method('get')
->willReturn('testToken112233');
$csrf_token
->expects($this
->any())
->method('validate')
->withAnyParameters()
->willReturn(FALSE);
$csrf_token
->expects($this
->any())
->method('validate')
->with('testToken112233')
->willReturn(TRUE);
$url_generator = $this
->getMockBuilder(UrlGeneratorInterface::class)
->disableOriginalConstructor()
->getMock();
$url_generator
->expects($this
->any())
->method('generateFromRoute')
->willReturn('some/url/test');
$unrouted_url_assembler = $this
->getMockBuilder(UnroutedUrlAssemblerInterface::class)
->disableOriginalConstructor()
->getMock();
// @BUG: Forcing the UnroutedUrlAssembler return here forces a pass.
// UnroutedUrlAssembler is called by toString() in setAuthFinishRedirect
// and is overly complicated to mock/replace.
$unrouted_url_assembler
->expects($this
->any())
->method('assemble')
->willReturn('https://example.com/sub/path?extra=1');
$response = $this
->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()
->getMock();
$response
->expects($this
->any())
->method('getBody')
->willReturn('{"access_token":"ACCESS_TOKEN", "token_type":"bearer", "expires_in":3600, "refresh_token": "refresh_token"}');
$http_client = $this
->getMockBuilder(GuzzleClient::class)
->disableOriginalConstructor()
->setMethods([
'post',
])
->getMock();
$http_client
->expects($this
->any())
->method('post')
->willReturn($response);
$current_user = $this
->getMockBuilder(AccountProxyInterface::class)
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
$this->container
->set('config.factory', $this
->getConfigFactoryStub());
$this->container
->set('csrf_token', $csrf_token);
$this->container
->set('unrouted_url_assembler', $unrouted_url_assembler);
$this->container
->set('url_generator.non_bubbling', $url_generator);
$this->container
->set('http_client', $http_client);
$this->container
->set('logger.factory', $this
->getLoggerFactoryStub());
$this->container
->set('current_user', $current_user);
\Drupal::setContainer($this->container);
$this->oAuthClient = Oauth::create($this->container);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiadamConfigTrait:: |
public | function | ||
OauthTest:: |
protected | property | Container builder helper. | |
OauthTest:: |
protected | property | Media: Acquia DAM oAuth client. | |
OauthTest:: |
public | function |
Overrides UnitTestCase:: |
|
OauthTest:: |
public | function | Validates that the access token response has the necessary keys. | |
OauthTest:: |
public | function | Validates the auth link that gets created. | |
OauthTest:: |
public | function | Validates that the redirect URL gets generated correctly. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |