1
0

LDAPTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\LDAP;
  8. use Test\TestCase;
  9. class LDAPTest extends TestCase {
  10. /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
  11. private $ldap;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->ldap = $this->getMockBuilder(LDAP::class)
  15. ->setMethods(['invokeLDAPMethod'])
  16. ->getMock();
  17. }
  18. public function errorProvider() {
  19. return [
  20. [
  21. 'ldap_search(): Partial search results returned: Sizelimit exceeded at /srv/http/nextcloud/master/apps/user_ldap/lib/LDAP.php#292',
  22. false
  23. ],
  24. [
  25. 'Some other error', true
  26. ]
  27. ];
  28. }
  29. /**
  30. * @param string $errorMessage
  31. * @param bool $passThrough
  32. * @dataProvider errorProvider
  33. */
  34. public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough): void {
  35. $wasErrorHandlerCalled = false;
  36. $errorHandler = function ($number, $message, $file, $line) use (&$wasErrorHandlerCalled): void {
  37. $wasErrorHandlerCalled = true;
  38. };
  39. set_error_handler($errorHandler);
  40. $this->ldap
  41. ->expects($this->once())
  42. ->method('invokeLDAPMethod')
  43. ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything())
  44. ->willReturnCallback(function () use ($errorMessage): void {
  45. trigger_error($errorMessage);
  46. });
  47. $fakeResource = ldap_connect();
  48. $this->ldap->search($fakeResource, 'base', 'filter', []);
  49. $this->assertSame($wasErrorHandlerCalled, $passThrough);
  50. restore_error_handler();
  51. }
  52. public function testModReplace(): void {
  53. $link = $this->createMock(LDAP::class);
  54. $userDN = 'CN=user';
  55. $password = 'MyPassword';
  56. $this->ldap
  57. ->expects($this->once())
  58. ->method('invokeLDAPMethod')
  59. ->with('mod_replace', $link, $userDN, ['userPassword' => $password])
  60. ->willReturn(true);
  61. $this->assertTrue($this->ldap->modReplace($link, $userDN, $password));
  62. }
  63. }