LDAPTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\User_LDAP\Tests;
  28. use OCA\User_LDAP\LDAP;
  29. use Test\TestCase;
  30. class LDAPTest extends TestCase {
  31. /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
  32. private $ldap;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->ldap = $this->getMockBuilder(LDAP::class)
  36. ->setMethods(['invokeLDAPMethod'])
  37. ->getMock();
  38. }
  39. public function errorProvider() {
  40. return [
  41. [
  42. 'ldap_search(): Partial search results returned: Sizelimit exceeded at /srv/http/nextcloud/master/apps/user_ldap/lib/LDAP.php#292',
  43. false
  44. ],
  45. [
  46. 'Some other error', true
  47. ]
  48. ];
  49. }
  50. /**
  51. * @param string $errorMessage
  52. * @param bool $passThrough
  53. * @dataProvider errorProvider
  54. */
  55. public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough) {
  56. $wasErrorHandlerCalled = false;
  57. $errorHandler = function ($number, $message, $file, $line) use (&$wasErrorHandlerCalled) {
  58. $wasErrorHandlerCalled = true;
  59. };
  60. set_error_handler($errorHandler);
  61. $this->ldap
  62. ->expects($this->once())
  63. ->method('invokeLDAPMethod')
  64. ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything())
  65. ->willReturnCallback(function () use ($errorMessage) {
  66. trigger_error($errorMessage);
  67. });
  68. $fakeResource = ldap_connect();
  69. $this->ldap->search($fakeResource, 'base', 'filter', []);
  70. $this->assertSame($wasErrorHandlerCalled, $passThrough);
  71. restore_error_handler();
  72. }
  73. public function testModReplace() {
  74. $link = $this->createMock(LDAP::class);
  75. $userDN = 'CN=user';
  76. $password = 'MyPassword';
  77. $this->ldap
  78. ->expects($this->once())
  79. ->method('invokeLDAPMethod')
  80. ->with('mod_replace', $link, $userDN, ['userPassword' => $password])
  81. ->willReturn(true);
  82. $this->assertTrue($this->ldap->modReplace($link, $userDN, $password));
  83. }
  84. }