Skip to content

Commit b225d62

Browse files
committed
refactor: Use IAppConfig for setting cron type
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent ff5c492 commit b225d62

File tree

7 files changed

+64
-64
lines changed

7 files changed

+64
-64
lines changed

apps/settings/lib/Settings/Admin/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getForm() {
4141
$cliBasedCronUser = $cliBasedCronPossible ? (posix_getpwuid($ownerConfigFile)['name'] ?? '') : '';
4242

4343
// Background jobs
44-
$this->initialStateService->provideInitialState('backgroundJobsMode', $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax'));
44+
$this->initialStateService->provideInitialState('backgroundJobsMode', $this->appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax'));
4545
$this->initialStateService->provideInitialState('lastCron', $this->appConfig->getValueInt('core', 'lastcron', 0));
4646
$this->initialStateService->provideInitialState('cronMaxAge', $this->cronMaxAge());
4747
$this->initialStateService->provideInitialState('cronErrors', $this->config->getAppValue('core', 'cronErrors'));

apps/settings/tests/Settings/Admin/ServerTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@
2525
* @group DB
2626
*/
2727
class ServerTest extends TestCase {
28-
/** @var Server */
29-
private $admin;
3028
/** @var IDBConnection */
3129
private $connection;
32-
/** @var IInitialState */
30+
/** @var Server&MockObject */
31+
private $admin;
32+
/** @var IInitialState&MockObject */
3333
private $initialStateService;
34-
/** @var ProfileManager */
34+
/** @var ProfileManager&MockObject */
3535
private $profileManager;
36-
/** @var ITimeFactory|MockObject */
36+
/** @var ITimeFactory&MockObject */
3737
private $timeFactory;
38-
/** @var IConfig|MockObject */
38+
/** @var IConfig&MockObject */
3939
private $config;
40-
/** @var IAppConfig|MockObject */
40+
/** @var IAppConfig&MockObject */
4141
private $appConfig;
42-
/** @var IL10N|MockObject */
42+
/** @var IL10N&MockObject */
4343
private $l10n;
44-
/** @var IUrlGenerator|MockObject */
44+
/** @var IUrlGenerator&MockObject */
4545
private $urlGenerator;
4646

4747
protected function setUp(): void {
@@ -78,10 +78,15 @@ public function testGetForm(): void {
7878
->expects($this->any())
7979
->method('getAppValue')
8080
->willReturnMap([
81-
['core', 'backgroundjobs_mode', 'ajax', 'ajax'],
8281
['core', 'lastcron', '0', '0'],
8382
['core', 'cronErrors', ''],
8483
]);
84+
$this->appConfig
85+
->expects($this->any())
86+
->method('getValueString')
87+
->willReturnMap([
88+
['core', 'backgroundjobs_mode', 'ajax', 'ajax'],
89+
]);
8590
$this->profileManager
8691
->expects($this->exactly(2))
8792
->method('isProfileEnabled')

apps/user_ldap/lib/Access.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCA\User_LDAP\User\Manager;
1717
use OCA\User_LDAP\User\OfflineUser;
1818
use OCP\HintException;
19+
use OCP\IAppConfig;
1920
use OCP\IConfig;
2021
use OCP\IUserManager;
2122
use Psr\Log\LoggerInterface;
@@ -30,10 +31,6 @@
3031
class Access extends LDAPUtility {
3132
public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];
3233

33-
/** @var \OCA\User_LDAP\Connection */
34-
public $connection;
35-
/** @var Manager */
36-
public $userManager;
3734
/**
3835
* never ever check this var directly, always use getPagedSearchResultState
3936
* @var ?bool
@@ -45,27 +42,17 @@ class Access extends LDAPUtility {
4542

4643
/** @var ?AbstractMapping */
4744
protected $groupMapper;
48-
49-
/**
50-
* @var \OCA\User_LDAP\Helper
51-
*/
52-
private $helper;
53-
/** @var IConfig */
54-
private $config;
55-
/** @var IUserManager */
56-
private $ncUserManager;
57-
/** @var LoggerInterface */
58-
private $logger;
5945
private string $lastCookie = '';
6046

6147
public function __construct(
62-
Connection $connection,
6348
ILDAPWrapper $ldap,
64-
Manager $userManager,
65-
Helper $helper,
66-
IConfig $config,
67-
IUserManager $ncUserManager,
68-
LoggerInterface $logger
49+
public Connection $connection,
50+
public Manager $userManager,
51+
private Helper $helper,
52+
private IConfig $config,
53+
private IUserManager $ncUserManager,
54+
private LoggerInterface $logger,
55+
private IAppConfig $appConfig,
6956
) {
7057
parent::__construct($ldap);
7158
$this->connection = $connection;
@@ -822,8 +809,7 @@ public function fetchListOfUsers(string $filter, array $attr, ?int $limit = null
822809
$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
823810
$recordsToUpdate = $ldapRecords;
824811
if (!$forceApplyAttributes) {
825-
$isBackgroundJobModeAjax = $this->config
826-
->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
812+
$isBackgroundJobModeAjax = $this->appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
827813
$listOfDNs = array_reduce($ldapRecords, function ($listOfDNs, $entry) {
828814
$listOfDNs[] = $entry['dn'][0];
829815
return $listOfDNs;

apps/user_ldap/lib/AccessFactory.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
namespace OCA\User_LDAP;
77

88
use OCA\User_LDAP\User\Manager;
9+
use OCP\IAppConfig;
910
use OCP\IConfig;
1011
use OCP\IUserManager;
1112
use OCP\Server;
1213
use Psr\Log\LoggerInterface;
1314

1415
class AccessFactory {
15-
private ILDAPWrapper $ldap;
16-
private Helper $helper;
17-
private IConfig $config;
18-
private IUserManager $ncUserManager;
19-
private LoggerInterface $logger;
2016

2117
public function __construct(
22-
ILDAPWrapper $ldap,
23-
Helper $helper,
24-
IConfig $config,
25-
IUserManager $ncUserManager,
26-
LoggerInterface $logger) {
18+
private ILDAPWrapper $ldap,
19+
private Helper $helper,
20+
private IConfig $config,
21+
private IAppConfig $appConfig,
22+
private IUserManager $ncUserManager,
23+
private LoggerInterface $logger,
24+
) {
2725
$this->ldap = $ldap;
2826
$this->helper = $helper;
2927
$this->config = $config;
@@ -34,13 +32,14 @@ public function __construct(
3432
public function get(Connection $connection): Access {
3533
/* Each Access instance gets its own Manager instance, see OCA\User_LDAP\AppInfo\Application::register() */
3634
return new Access(
37-
$connection,
3835
$this->ldap,
36+
$connection,
3937
Server::get(Manager::class),
4038
$this->helper,
4139
$this->config,
4240
$this->ncUserManager,
43-
$this->logger
41+
$this->logger,
42+
$this->appConfig,
4443
);
4544
}
4645
}

apps/user_ldap/tests/AccessTest.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
use OCA\User_LDAP\User\Manager;
1919
use OCA\User_LDAP\User\OfflineUser;
2020
use OCA\User_LDAP\User\User;
21+
use OCP\IAppConfig;
2122
use OCP\IAvatarManager;
2223
use OCP\IConfig;
2324
use OCP\Image;
2425
use OCP\IUserManager;
2526
use OCP\Notification\IManager as INotificationManager;
2627
use OCP\Share\IManager;
28+
use PHPUnit\Framework\MockObject\MockObject;
2729
use Psr\Log\LoggerInterface;
2830
use Test\TestCase;
2931

@@ -53,10 +55,12 @@ class AccessTest extends TestCase {
5355
private $config;
5456
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
5557
private $ncUserManager;
56-
/** @var LoggerInterface|MockObject */
57-
private $logger;
58-
/** @var Access */
59-
private $access;
58+
59+
private LoggerInterface&MockObject $logger;
60+
61+
private IAppConfig&MockObject $appConfig;
62+
63+
private Access $access;
6064

6165
protected function setUp(): void {
6266
$this->connection = $this->createMock(Connection::class);
@@ -69,28 +73,33 @@ protected function setUp(): void {
6973
$this->ncUserManager = $this->createMock(IUserManager::class);
7074
$this->shareManager = $this->createMock(IManager::class);
7175
$this->logger = $this->createMock(LoggerInterface::class);
76+
$this->appConfig = $this->createMock(IAppConfig::class);
7277

7378
$this->access = new Access(
74-
$this->connection,
7579
$this->ldap,
80+
$this->connection,
7681
$this->userManager,
7782
$this->helper,
7883
$this->config,
7984
$this->ncUserManager,
80-
$this->logger
85+
$this->logger,
86+
$this->appConfig,
8187
);
8288
$this->access->setUserMapper($this->userMapper);
8389
$this->access->setGroupMapper($this->groupMapper);
8490
}
8591

8692
private function getConnectorAndLdapMock() {
93+
/** @var ILDAPWrapper&MockObject */
8794
$lw = $this->createMock(ILDAPWrapper::class);
95+
/** @var Connection&MockObject */
8896
$connector = $this->getMockBuilder(Connection::class)
8997
->setConstructorArgs([$lw, '', null])
9098
->getMock();
9199
$connector->expects($this->any())
92100
->method('getConnectionResource')
93101
->willReturn(ldap_connect('ldap://example.com'));
102+
/** @var Manager&MockObject */
94103
$um = $this->getMockBuilder(Manager::class)
95104
->setConstructorArgs([
96105
$this->createMock(IConfig::class),
@@ -220,7 +229,7 @@ public function testStringResemblesDN($case) {
220229
[$lw, $con, $um, $helper] = $this->getConnectorAndLdapMock();
221230
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
222231
$config = $this->createMock(IConfig::class);
223-
$access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger);
232+
$access = new Access($lw, $con, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->appConfig);
224233

225234
$lw->expects($this->exactly(1))
226235
->method('explodeDN')
@@ -243,7 +252,7 @@ public function testStringResemblesDNLDAPmod($case) {
243252
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
244253
$config = $this->createMock(IConfig::class);
245254
$lw = new LDAP();
246-
$access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger);
255+
$access = new Access($lw, $con, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->appConfig);
247256

248257
if (!function_exists('ldap_explode_dn')) {
249258
$this->markTestSkipped('LDAP Module not available');
@@ -429,7 +438,7 @@ public function testSanitizeDN($attribute) {
429438
$attribute => ['count' => 1, $dnFromServer]
430439
]);
431440

432-
$access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger);
441+
$access = new Access($lw, $con, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->appConfig);
433442
$values = $access->readAttribute('uid=whoever,dc=example,dc=org', $attribute);
434443
$this->assertSame($values[0], strtolower($dnFromServer));
435444
}

core/Command/Background/Base.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace OC\Core\Command\Background;
77

8-
use OCP\IConfig;
8+
use OCP\IAppConfig;
99

1010
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Input\InputInterface;
@@ -17,18 +17,17 @@
1717
* Subclasses will override the getMode() function to specify the mode to configure.
1818
*/
1919
abstract class Base extends Command {
20-
abstract protected function getMode();
20+
abstract protected function getMode(): string;
2121

2222
public function __construct(
23-
protected IConfig $config,
23+
protected IAppConfig $config,
2424
) {
2525
parent::__construct();
2626
}
2727

2828
protected function configure(): void {
2929
$mode = $this->getMode();
30-
$this
31-
->setName("background:$mode")
30+
$this->setName("background:$mode")
3231
->setDescription("Use $mode to run background jobs");
3332
}
3433

@@ -43,7 +42,7 @@ protected function configure(): void {
4342
*/
4443
protected function execute(InputInterface $input, OutputInterface $output): int {
4544
$mode = $this->getMode();
46-
$this->config->setAppValue('core', 'backgroundjobs_mode', $mode);
45+
$this->config->setValueString('core', 'backgroundjobs_mode', $mode);
4746
$output->writeln("Set mode for background jobs to '$mode'");
4847
return 0;
4948
}

core/Listener/BeforeTemplateRenderedListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
1212
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
1313
use OCP\AppFramework\Http\TemplateResponse;
14+
use OCP\AppFramework\Services\IAppConfig;
1415
use OCP\EventDispatcher\Event;
1516
use OCP\EventDispatcher\IEventListener;
16-
use OCP\IConfig;
1717
use OCP\Util;
1818

1919
/** @template-implements IEventListener<BeforeLoginTemplateRenderedEvent|BeforeTemplateRenderedEvent> */
2020
class BeforeTemplateRenderedListener implements IEventListener {
21-
public function __construct(private IConfig $config) {
21+
public function __construct(
22+
private IAppConfig $appConfig,
23+
) {
2224
}
2325

2426
public function handle(Event $event): void {
@@ -53,7 +55,7 @@ public function handle(Event $event): void {
5355

5456

5557
// If installed and background job is set to ajax, add dedicated script
56-
if ($this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
58+
if ($this->appConfig->getAppValueString('backgroundjobs_mode', 'ajax') === 'ajax') {
5759
Util::addScript('core', 'ajax-cron');
5860
}
5961
}

0 commit comments

Comments
 (0)