You are here

class AuthmapTest in External Authentication 8

Same name in this branch
  1. 8 tests/src/Unit/AuthmapTest.php \Drupal\Tests\externalauth\Unit\AuthmapTest
  2. 8 tests/src/Kernel/AuthmapTest.php \Drupal\Tests\externalauth\Kernel\AuthmapTest
Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/AuthmapTest.php \Drupal\Tests\externalauth\Unit\AuthmapTest

Authmap unit tests.

@group externalauth

@coversDefaultClass \Drupal\externalauth\Authmap

Hierarchy

Expanded class hierarchy of AuthmapTest

File

tests/src/Unit/AuthmapTest.php, line 17

Namespace

Drupal\Tests\externalauth\Unit
View source
class AuthmapTest extends UnitTestCase {

  /**
   * The mocked database connection.
   *
   * @var \Drupal\Core\Database\Connection|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $connection;

  /**
   * Mock statement.
   *
   * @var \Drupal\Core\Database\Statement|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $statement;

  /**
   * Mock select interface.
   *
   * @var \Drupal\Core\Database\Query\SelectInterface
   */
  protected $select;

  /**
   * Mock delete class.
   *
   * @var \Drupal\Core\Database\Query\Delete
   */
  protected $delete;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create a Mock database connection object.
    $this->connection = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Connection')
      ->disableOriginalConstructor()
      ->getMock();

    // Create a Mock Statement object.
    $this->statement = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Driver\\sqlite\\Statement')
      ->disableOriginalConstructor()
      ->getMock();

    // Create a Mock Select object and set expectations.
    $this->select = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Query\\Select')
      ->disableOriginalConstructor()
      ->getMock();
    $this->select
      ->expects($this
      ->any())
      ->method('fields')
      ->will($this
      ->returnSelf());
    $this->select
      ->expects($this
      ->any())
      ->method('condition')
      ->will($this
      ->returnSelf());
    $this->select
      ->expects($this
      ->any())
      ->method('range')
      ->will($this
      ->returnSelf());
    $this->select
      ->expects($this
      ->any())
      ->method('orderBy')
      ->will($this
      ->returnSelf());
    $this->select
      ->expects($this
      ->any())
      ->method('execute')
      ->will($this
      ->returnValue($this->statement));
    $this->connection
      ->expects($this
      ->any())
      ->method('select')
      ->will($this
      ->returnValue($this->select));

    // Create a Mock Delete object and set expectations.
    $this->delete = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Query\\Delete')
      ->disableOriginalConstructor()
      ->getMock();
    $this->delete
      ->expects($this
      ->any())
      ->method('condition')
      ->will($this
      ->returnSelf());
    $this->delete
      ->expects($this
      ->any())
      ->method('execute')
      ->will($this
      ->returnValue($this->statement));
  }

  /**
   * Test save() method.
   *
   * @covers ::save
   * @covers ::__construct
   */
  public function testSave() {
    $account = $this
      ->createMock('Drupal\\user\\UserInterface');
    $merge = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Query\\Merge')
      ->disableOriginalConstructor()
      ->getMock();
    $merge
      ->expects($this
      ->any())
      ->method('keys')
      ->will($this
      ->returnSelf());
    $merge
      ->expects($this
      ->any())
      ->method('fields')
      ->will($this
      ->returnSelf());
    $merge
      ->expects($this
      ->any())
      ->method('execute')
      ->will($this
      ->returnValue($this->statement));
    $this->connection
      ->expects($this
      ->once())
      ->method('merge')
      ->with($this
      ->equalTo('authmap'))
      ->will($this
      ->returnValue($merge));
    $authmap = new Authmap($this->connection);
    $authmap
      ->save($account, "test_provider", "test_authname");
  }

  /**
   * Test get() method.
   *
   * @covers ::get
   * @covers ::__construct
   */
  public function testGet() {
    $actual_data = (object) [
      'authname' => "test_authname",
    ];
    $this->statement
      ->expects($this
      ->any())
      ->method('fetchObject')
      ->will($this
      ->returnValue($actual_data));
    $authmap = new Authmap($this->connection);
    $result = $authmap
      ->get(2, "test_provider");
    $this
      ->assertEquals($result, "test_authname");
  }

  /**
   * Test getAuthData() method.
   *
   * @covers ::getAuthData
   * @covers ::__construct
   */
  public function testGetAuthData() {
    $actual_data = [
      'authname' => "test_authname",
      'data' => "test_data",
    ];
    $this->statement
      ->expects($this
      ->any())
      ->method('fetchAssoc')
      ->will($this
      ->returnValue($actual_data));
    $authmap = new Authmap($this->connection);
    $result = $authmap
      ->getAuthData(2, "test_provider");
    $this
      ->assertEquals([
      'authname' => "test_authname",
      "data" => "test_data",
    ], $result);
  }

  /**
   * Test getAll() method.
   *
   * @covers ::getAll
   * @covers ::__construct
   */
  public function testGetAll() {
    $actual_data = [
      'test_provider' => (object) [
        "authname" => "test_authname",
        "provider" => "test_provider",
      ],
      'test_provider2' => (object) [
        "authname" => "test_authname2",
        "provider" => "test_provider2",
      ],
    ];
    $this->statement
      ->expects($this
      ->any())
      ->method('fetchAllAssoc')
      ->will($this
      ->returnValue($actual_data));
    $authmap = new Authmap($this->connection);
    $result = $authmap
      ->getAll(2);
    $expected_data = [
      "test_provider" => "test_authname",
      "test_provider2" => "test_authname2",
    ];
    $this
      ->assertEquals($expected_data, $result);
  }

  /**
   * Test getUid() method.
   *
   * @covers ::getUid
   * @covers ::__construct
   */
  public function testGetUid() {
    $actual_data = (object) [
      "uid" => 2,
    ];
    $this->statement
      ->expects($this
      ->any())
      ->method('fetchObject')
      ->will($this
      ->returnValue($actual_data));
    $authmap = new Authmap($this->connection);
    $result = $authmap
      ->getUid(2, "test_provider");
    $this
      ->assertEquals(2, $result);
  }

  /**
   * Test delete() method.
   *
   * @covers ::delete
   * @covers ::__construct
   */
  public function testDelete() {
    $this->connection
      ->expects($this
      ->once())
      ->method('delete')
      ->with($this
      ->equalTo('authmap'))
      ->will($this
      ->returnValue($this->delete));
    $authmap = new Authmap($this->connection);
    $authmap
      ->delete(2);
  }

  /**
   * Test delete() method, when passing in $provider.
   *
   * @covers ::delete
   * @covers ::__construct
   */
  public function testDeleteWithProvider() {

    // Create a Mock Delete object and set expectations.
    $this->delete = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Query\\Delete')
      ->disableOriginalConstructor()
      ->getMock();
    $this->delete
      ->expects($this
      ->exactly(2))
      ->method('condition')
      ->will($this
      ->returnSelf());
    $this->delete
      ->expects($this
      ->any())
      ->method('execute')
      ->will($this
      ->returnValue($this->statement));
    $this->connection
      ->expects($this
      ->once())
      ->method('delete')
      ->with($this
      ->equalTo('authmap'))
      ->will($this
      ->returnValue($this->delete));
    $authmap = new Authmap($this->connection);
    $authmap
      ->delete(2, 'some_provider');
  }

  /**
   * Test deleteProviders() method.
   *
   * @covers ::deleteProvider
   * @covers ::__construct
   */
  public function testDeleteProviders() {
    $this->connection
      ->expects($this
      ->once())
      ->method('delete')
      ->with($this
      ->equalTo('authmap'))
      ->will($this
      ->returnValue($this->delete));
    $authmap = new Authmap($this->connection);
    $authmap
      ->deleteProvider("test_provider");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthmapTest::$connection protected property The mocked database connection.
AuthmapTest::$delete protected property Mock delete class.
AuthmapTest::$select protected property Mock select interface.
AuthmapTest::$statement protected property Mock statement.
AuthmapTest::setUp protected function Overrides UnitTestCase::setUp
AuthmapTest::testDelete public function Test delete() method.
AuthmapTest::testDeleteProviders public function Test deleteProviders() method.
AuthmapTest::testDeleteWithProvider public function Test delete() method, when passing in $provider.
AuthmapTest::testGet public function Test get() method.
AuthmapTest::testGetAll public function Test getAll() method.
AuthmapTest::testGetAuthData public function Test getAuthData() method.
AuthmapTest::testGetUid public function Test getUid() method.
AuthmapTest::testSave public function Test save() method.
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::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
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.