ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ilLDAPServerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\MockObject\MockObject;
23use PHPUnit\Framework\TestCase;
24
28class ilLDAPServerTest extends TestCase
29{
30 private ?Container $dic = null;
31
32 protected function setUp(): void
33 {
34 global $DIC;
35
36 $this->dic = is_object($DIC) ? clone $DIC : $DIC;
37
38 $DIC = new Container();
39
40 $this->setGlobalVariable('lng', $this->getLanguageMock());
41 $this->setGlobalVariable(
42 'ilDB',
43 $this->getMockBuilder(ilDBInterface::class)->disableAutoReturnValueGeneration()->getMock()
44 );
45
46 $this->setGlobalVariable(
47 'ilSetting',
48 $this->getMockBuilder(\ILIAS\Administration\Setting::class)->getMock()
49 );
50 $this->setGlobalVariable(
51 'ilErr',
52 $this->getMockBuilder(ilErrorHandling::class)->disableOriginalConstructor()->getMock()
53 );
54
55 $logger = $this->getMockBuilder(ilLogger::class)->disableOriginalConstructor()->getMockForAbstractClass();
56 $logger_factory = $this->getMockBuilder(ilLoggerFactory::class)->disableOriginalConstructor()->getMock();
57 $logger_factory->method('getComponentLogger')->willReturn($logger);
58 $this->setGlobalVariable(
59 ilLoggerFactory::class,
60 $logger_factory
61 );
62
63 parent::setUp();
64 }
65
66 protected function tearDown(): void
67 {
68 global $DIC;
69
71
72 parent::tearDown();
73 }
74
75 protected function setGlobalVariable(string $name, mixed $value): void
76 {
77 global $DIC;
78
79 $GLOBALS[$name] = $value;
80
81 unset($DIC[$name]);
82 $DIC[$name] = static fn(Container $c) => $GLOBALS[$name];
83 }
84
88 protected function getLanguageMock(): ilLanguage
89 {
90 $lng = $this
91 ->getMockBuilder(ilLanguage::class)
92 ->disableOriginalConstructor()
93 ->onlyMethods(['txt', 'getInstalledLanguages', 'loadLanguageModule'])
94 ->getMock();
95
96 return $lng;
97 }
98
99 public function testConstructorWithoutParam(): void
100 {
101 global $DIC;
102
103 //setup some method calls
105 $setting = $DIC['ilSetting'];
106 $setting->method("get")->willReturnCallback(
107 function ($arg) {
108 if ($arg === 'session_statistics') {
109 return "0";
110 }
111
112 throw new \RuntimeException($arg);
113 }
114 );
116 $data = null;
117 $ilDB = $DIC['ilDB'];
118 $ilDB->expects($this->never())->method("quote");
119
120 $server = new ilLDAPServer();
121 $this->assertFalse($server->isActive());
122 }
123
124 public function testConstructorWithParameter(): void
125 {
126 global $DIC;
127
128 //setup some method calls
130 $setting = $DIC['ilSetting'];
131 $setting->method("get")->willReturnCallback(
132 function ($arg) {
133 if ($arg === 'session_statistics') {
134 return "0";
135 }
136
137 throw new \RuntimeException($arg);
138 }
139 );
140
142 $ilDB = $DIC['ilDB'];
143 $ilDB->expects($this->once())->method("quote")->with(1)->willReturn("1");
144
145 $res = $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock();
146 $ilDB->method("query")->with(
147 "SELECT * FROM ldap_server_settings WHERE server_id = 1"
148 )->
149 willReturn($res);
150 $res->expects($this->exactly(2))->method("fetchRow")->willReturnOnConsecutiveCalls((object) array(
151 'active' => "true",
152 'name' => "testserver",
153 'url' => "ldap://testurl:389",
154 'version' => "3",
155 'base_dn' => "true",
156 'referrals' => "false",
157 'tls' => "false",
158 'bind_type' => "1",
159 'bind_user' => "nobody",
160 'bind_pass' => "password",
161 'search_base' => "dc=de",
162 'user_scope' => "1",
163 'user_attribute' => "user",
164 'filter' => ".",
165 'group_dn' => "dc=group",
166 'group_scope' => "1",
167 'group_filter' => "",
168 'group_member' => "",
169 'group_attribute' => "",
170 'group_optional' => "false",
171 'group_user_filter' => ".*",
172 'group_memberisdn' => "true",
173 'group_name' => "",
174 'sync_on_login' => "true",
175 'sync_per_cron' => "false",
176 'role_sync_active' => "true",
177 'role_bind_dn' => "rolebind",
178 'role_bind_pass' => "rolebindpwd",
179 'migration' => "true",
180 'authentication' => "true",
181 'authentication_type' => "1",
182 'username_filter' => ".*",
183 'escape_dn' => "false"
184 ), null);
185
186 $server = new ilLDAPServer(1);
187 $this->assertTrue($server->isActive());
188 }
189}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilSessionTest.
setGlobalVariable(string $name, mixed $value)
language handling
$c
Definition: deliver.php:25
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$server
Definition: shib_login.php:28
$GLOBALS["DIC"]
Definition: wac.php:54