Skip to content
Snippets Groups Projects
Commit f60d1730 authored by Steven Ayers's avatar Steven Ayers
Browse files

Issue #3410613 by bluegeek9: Services Dependency Injection

parent a9310f97
No related branches found
No related tags found
1 merge request!7Issue #3410613 by bluegeek9: Services Dependency Injection
Pipeline #69563 passed
......@@ -7,10 +7,16 @@ services:
common.ldap_sso_auth:
class: Drupal\ldap_sso_auth\LdapSsoAuthAuthentication
arguments: ['@service_container']
arguments:
- '@config.factory'
- '@entity_type.manager'
- '@ldap_authentication.login_validator_sso'
- '@ldap.detail_log'
ldap_sso_auth.page_cache_request_policy.ldap_sso_auth_login_name:
class: Drupal\ldap_sso_auth\RequestPolicy\PageCache\LdapSsoAuthLoginName
arguments: ['@session_configuration', '@service_container']
arguments:
- '@session_configuration'
- '@config.factory'
tags:
- { name: page_cache_request_policy }
......@@ -3,7 +3,10 @@
namespace Drupal\ldap_sso_auth;
use Drupal\Component\Utility\Html;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\ldap_authentication\Controller\LoginValidatorSso;
use Drupal\ldap_servers\Logger\LdapDetailLog;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -11,13 +14,6 @@ use Symfony\Component\HttpFoundation\Request;
*/
class LdapSsoAuthAuthentication implements LdapSsoAuthAuthenticationInterface {
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* The config factory.
*
......@@ -62,15 +58,28 @@ class LdapSsoAuthAuthentication implements LdapSsoAuthAuthenticationInterface {
/**
* Constructs a new LdapSsoAuthAuthentication object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\ldap_authentication\Controller\LoginValidatorSso $login_validator_sso
* The login validator.
* @param \Drupal\ldap_servers\Logger\LdapDetailLog $ldap_detail_log
* The detail logger.
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;
$config = $container->get('config.factory');
$this->config = $config->get('ldap_sso_auth.settings');
$this->frontpage = $config->get('system.site')->get('frontpage');
$this->entityTypeManager = $container->get('entity_type.manager');
$this->validator = $container->get('ldap_authentication.login_validator_sso');
$this->detailLog = $container->get('ldap.detail_log');
public function __construct(
ConfigFactoryInterface $config_factory,
EntityTypeManagerInterface $entity_type_manager,
LoginValidatorSso $login_validator_sso,
LdapDetailLog $ldap_detail_log) {
$this->entityTypeManager = $entity_type_manager;
$this->validator = $login_validator_sso;
$this->detailLog = $ldap_detail_log;
$this->config = $config_factory->get('ldap_sso_auth.settings');
$this->frontpage = $config_factory->get('system.site')->get('frontpage');
}
/**
......
......@@ -2,9 +2,9 @@
namespace Drupal\ldap_sso_auth\RequestPolicy\PageCache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\Session\SessionConfigurationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -27,23 +27,23 @@ class LdapSsoAuthLoginName implements RequestPolicyInterface {
protected $sessionConfiguration;
/**
* The services container.
* The ldap_sso_auth settings.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $container;
protected $settings;
/**
* Constructs a new page cache session policy.
*
* @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
* The session configuration.
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The services container.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(SessionConfigurationInterface $session_configuration, ContainerInterface $container) {
public function __construct(SessionConfigurationInterface $session_configuration, ConfigFactoryInterface $config_factory) {
$this->sessionConfiguration = $session_configuration;
$this->container = $container;
$this->settings = $config_factory->get('ldap_sso_auth.settings');
}
/**
......@@ -52,9 +52,7 @@ class LdapSsoAuthLoginName implements RequestPolicyInterface {
public function check(Request $request) {
if (!$this->sessionConfiguration->hasSession($request)) {
// No session stored check header sso variable of user name.
$sso_variable = $this->container->get('config.factory')
->get('ldap_sso_auth.settings')
->get('ssoVariable');
$sso_variable = $this->settings->get('ssoVariable');
if ($request->server->get($sso_variable) === NULL) {
// Not contains name in sso variable allow to get page from cache.
......
......@@ -36,17 +36,11 @@ class LdapSsoAuthAuthenticationTest extends UnitTestCase {
'frontpage' => '/frontpage',
],
];
$configFactory = $this->getConfigFactoryStub($config);
$container->set('config.factory', $configFactory);
$config_factory = $this->getConfigFactoryStub($config);
$entity_type_manager = $this->createMock('Drupal\Core\Entity\EntityTypeManagerInterface');
$container->set('entity_type.manager', $entity_type_manager);
$ldap_authentication_login_validator = $this->createMock('Drupal\ldap_authentication\Controller\LoginValidatorSso');
$container->set('ldap_authentication.login_validator_sso', $ldap_authentication_login_validator);
$ldap_log = $this->createMock('Drupal\ldap_servers\Logger\LdapDetailLog');
$container->set('ldap.detail_log', $ldap_log);
$request = $this->createMock(Request::class);
$request->method('getSession')->willReturn($session);
......@@ -54,7 +48,12 @@ class LdapSsoAuthAuthenticationTest extends UnitTestCase {
$request->server = $this->createMock('Symfony\Component\HttpFoundation\ServerBag');
$request->server->method('get')->with($ssoVariable)->willReturn($ssoValue);
$service = new LdapSsoAuthAuthentication($container);
$service = new LdapSsoAuthAuthentication(
$config_factory,
$entity_type_manager,
$ldap_authentication_login_validator,
$ldap_log
);
$this->assertEquals($expectedResult, $service->applies($request));
}
......
......@@ -8,7 +8,6 @@ use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\Session\SessionConfigurationInterface;
use Drupal\ldap_sso_auth\RequestPolicy\PageCache\LdapSsoAuthLoginName;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
......@@ -28,27 +27,39 @@ class LdapSsoAuthLoginNameTest extends TestCase {
protected $sessionConfiguration;
/**
* The mocked container.
* The policy object under test.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject
* @var \Drupal\ldap_sso_auth\RequestPolicy\PageCache\LdapSsoAuthLoginName
*/
protected $container;
protected $policy;
/**
* The policy object under test.
* The mocked config factory.
*
* @var \Drupal\ldap_sso_auth\RequestPolicy\PageCache\LdapSsoAuthLoginName
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $policy;
protected $configFactory;
/**
* The mocked config object.
*
* @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit\Framework\MockObject\MockObject
*/
protected $config;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->sessionConfiguration = $this->createMock(SessionConfigurationInterface::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->configFactory = $this->createMock(ConfigFactoryInterface::class);
$this->config = $this->createMock(ImmutableConfig::class);
$this->configFactory->expects($this->any())
->method('get')
->with('ldap_sso_auth.settings')
->willReturn($this->config);
$this->policy = new LdapSsoAuthLoginName($this->sessionConfiguration, $this->container);
$this->policy = new LdapSsoAuthLoginName($this->sessionConfiguration, $this->configFactory);
}
/**
......@@ -69,20 +80,11 @@ class LdapSsoAuthLoginNameTest extends TestCase {
->method('hasSession')
->with($request)
->willReturn(FALSE);
$configFactory = $this->createMock(ConfigFactoryInterface::class);
$config = $this->createMock(ImmutableConfig::class);
$config->expects($this->once())
$this->config->expects($this->once())
->method('get')
->with('ssoVariable')
->willReturn('REMOTE_USER');
$configFactory->expects($this->once())
->method('get')
->with('ldap_sso_auth.settings')
->willReturn($config);
$this->container->expects($this->once())
->method('get')
->with('config.factory')
->willReturn($configFactory);
$this->assertSame(RequestPolicyInterface::DENY, $this->policy->check($request));
}
......@@ -121,12 +123,9 @@ class LdapSsoAuthLoginNameTest extends TestCase {
->with($request)
->willReturn(TRUE);
// Create a mock container object.
$container = $this->createMock(ContainerInterface::class);
// Create an instance of the LdapSsoAuthLoginName object and invoke the
// check method with the request object.
$loginNamePolicy = new LdapSsoAuthLoginName($sessionConfiguration, $container);
$loginNamePolicy = new LdapSsoAuthLoginName($sessionConfiguration, $this->configFactory);
$result = $loginNamePolicy->check($request);
// Assert that the check method returns the expected value.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment