HelperTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\User_LDAP\Tests;
  27. use OCA\User_LDAP\Helper;
  28. use OCP\IConfig;
  29. /**
  30. * @group DB
  31. */
  32. class HelperTest extends \Test\TestCase {
  33. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  34. private $config;
  35. /** @var Helper */
  36. private $helper;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->helper = new Helper($this->config, \OC::$server->getDatabaseConnection());
  41. }
  42. public function testGetServerConfigurationPrefixes() {
  43. $this->config->method('getAppKeys')
  44. ->with($this->equalTo('user_ldap'))
  45. ->willReturn([
  46. 'foo',
  47. 'ldap_configuration_active',
  48. 's1ldap_configuration_active',
  49. ]);
  50. $result = $this->helper->getServerConfigurationPrefixes(false);
  51. $this->assertEquals(['', 's1'], $result);
  52. }
  53. public function testGetServerConfigurationPrefixesActive() {
  54. $this->config->method('getAppKeys')
  55. ->with($this->equalTo('user_ldap'))
  56. ->willReturn([
  57. 'foo',
  58. 'ldap_configuration_active',
  59. 's1ldap_configuration_active',
  60. ]);
  61. $this->config->method('getAppValue')
  62. ->willReturnCallback(function ($app, $key, $default) {
  63. if ($app !== 'user_ldap') {
  64. $this->fail('wrong app');
  65. }
  66. if ($key === 's1ldap_configuration_active') {
  67. return '1';
  68. }
  69. return $default;
  70. });
  71. $result = $this->helper->getServerConfigurationPrefixes(true);
  72. $this->assertEquals(['s1'], $result);
  73. }
  74. public function testGetServerConfigurationHost() {
  75. $this->config->method('getAppKeys')
  76. ->with($this->equalTo('user_ldap'))
  77. ->willReturn([
  78. 'foo',
  79. 'ldap_host',
  80. 's1ldap_host',
  81. 's02ldap_host',
  82. ]);
  83. $this->config->method('getAppValue')
  84. ->willReturnCallback(function ($app, $key, $default) {
  85. if ($app !== 'user_ldap') {
  86. $this->fail('wrong app');
  87. }
  88. if ($key === 'ldap_host') {
  89. return 'example.com';
  90. }
  91. if ($key === 's1ldap_host') {
  92. return 'foo.bar.com';
  93. }
  94. return $default;
  95. });
  96. $result = $this->helper->getServerConfigurationHosts();
  97. $this->assertEquals([
  98. '' => 'example.com',
  99. 's1' => 'foo.bar.com',
  100. 's02' => '',
  101. ], $result);
  102. }
  103. }