AuthmapTest.php in External Authentication 2.0.x
File
tests/src/Unit/AuthmapTest.php
View source
<?php
namespace Drupal\Tests\externalauth\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\externalauth\Authmap;
class AuthmapTest extends UnitTestCase {
protected $connection;
protected $statement;
protected $select;
protected $delete;
protected function setUp() {
parent::setUp();
$this->connection = $this
->getMockBuilder('Drupal\\Core\\Database\\Connection')
->disableOriginalConstructor()
->getMock();
$this->statement = $this
->getMockBuilder('Drupal\\Core\\Database\\Driver\\sqlite\\Statement')
->disableOriginalConstructor()
->getMock();
$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));
$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));
}
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");
}
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");
}
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);
}
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);
}
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);
}
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);
}
public function testDeleteWithProvider() {
$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');
}
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");
}
}