HelperTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Tests;
  7. use OCA\User_LDAP\Helper;
  8. use OCP\IConfig;
  9. /**
  10. * @group DB
  11. */
  12. class HelperTest extends \Test\TestCase {
  13. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  14. private $config;
  15. /** @var Helper */
  16. private $helper;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->config = $this->createMock(IConfig::class);
  20. $this->helper = new Helper($this->config, \OC::$server->getDatabaseConnection());
  21. }
  22. public function testGetServerConfigurationPrefixes() {
  23. $this->config->method('getAppKeys')
  24. ->with($this->equalTo('user_ldap'))
  25. ->willReturn([
  26. 'foo',
  27. 'ldap_configuration_active',
  28. 's1ldap_configuration_active',
  29. ]);
  30. $result = $this->helper->getServerConfigurationPrefixes(false);
  31. $this->assertEquals(['', 's1'], $result);
  32. }
  33. public function testGetServerConfigurationPrefixesActive() {
  34. $this->config->method('getAppKeys')
  35. ->with($this->equalTo('user_ldap'))
  36. ->willReturn([
  37. 'foo',
  38. 'ldap_configuration_active',
  39. 's1ldap_configuration_active',
  40. ]);
  41. $this->config->method('getAppValue')
  42. ->willReturnCallback(function ($app, $key, $default) {
  43. if ($app !== 'user_ldap') {
  44. $this->fail('wrong app');
  45. }
  46. if ($key === 's1ldap_configuration_active') {
  47. return '1';
  48. }
  49. return $default;
  50. });
  51. $result = $this->helper->getServerConfigurationPrefixes(true);
  52. $this->assertEquals(['s1'], $result);
  53. }
  54. public function testGetServerConfigurationHost() {
  55. $this->config->method('getAppKeys')
  56. ->with($this->equalTo('user_ldap'))
  57. ->willReturn([
  58. 'foo',
  59. 'ldap_host',
  60. 's1ldap_host',
  61. 's02ldap_host',
  62. ]);
  63. $this->config->method('getAppValue')
  64. ->willReturnCallback(function ($app, $key, $default) {
  65. if ($app !== 'user_ldap') {
  66. $this->fail('wrong app');
  67. }
  68. if ($key === 'ldap_host') {
  69. return 'example.com';
  70. }
  71. if ($key === 's1ldap_host') {
  72. return 'foo.bar.com';
  73. }
  74. return $default;
  75. });
  76. $result = $this->helper->getServerConfigurationHosts();
  77. $this->assertEquals([
  78. '' => 'example.com',
  79. 's1' => 'foo.bar.com',
  80. 's02' => '',
  81. ], $result);
  82. }
  83. }