You are here

class OauthTest in Media: Acquia DAM 8

Oauth test.

@group media_acquiadam

Hierarchy

Expanded class hierarchy of OauthTest

File

tests/src/Unit/OauthTest.php, line 22

Namespace

Drupal\Tests\media_acquiadam\Unit
View 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

Namesort descending Modifiers Type Description Overrides
AcquiadamConfigTrait::getConfigFactoryStub public function
OauthTest::$container protected property Container builder helper.
OauthTest::$oAuthClient protected property Media: Acquia DAM oAuth client.
OauthTest::setUp public function Overrides UnitTestCase::setUp
OauthTest::testGetAccessToken public function Validates that the access token response has the necessary keys.
OauthTest::testGetAuthLink public function Validates the auth link that gets created.
OauthTest::testGetSetAuthFinishRedirect public function Validates that the redirect URL gets generated correctly.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.