1
0

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 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\LDAP;
  28. use Test\TestCase;
  29. class LDAPTest extends TestCase {
  30. /** @var LDAP|\PHPUnit_Framework_MockObject_MockObject */
  31. private $ldap;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->ldap = $this->getMockBuilder(LDAP::class)
  35. ->setMethods(['invokeLDAPMethod'])
  36. ->getMock();
  37. }
  38. public function errorProvider() {
  39. return [
  40. [
  41. 'ldap_search(): Partial search results returned: Sizelimit exceeded at /srv/http/nextcloud/master/apps/user_ldap/lib/LDAP.php#292',
  42. false
  43. ],
  44. [
  45. 'Some other error', true
  46. ]
  47. ];
  48. }
  49. /**
  50. * @param string $errorMessage
  51. * @param bool $passThrough
  52. * @dataProvider errorProvider
  53. */
  54. public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough) {
  55. $wasErrorHandlerCalled = false;
  56. $errorHandler = function($number, $message, $file, $line) use (&$wasErrorHandlerCalled) {
  57. $wasErrorHandlerCalled = true;
  58. };
  59. set_error_handler($errorHandler);
  60. $this->ldap
  61. ->expects($this->once())
  62. ->method('invokeLDAPMethod')
  63. ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything())
  64. ->willReturnCallback(function() use($errorMessage) {
  65. trigger_error($errorMessage);
  66. });
  67. $this->ldap->search('pseudo-resource', 'base', 'filter', []);
  68. $this->assertSame($wasErrorHandlerCalled, $passThrough);
  69. restore_error_handler();
  70. }
  71. public function testModReplace() {
  72. $link = $this->createMock(LDAP::class);
  73. $userDN = 'CN=user';
  74. $password = 'MyPassword';
  75. $this->ldap
  76. ->expects($this->once())
  77. ->method('invokeLDAPMethod')
  78. ->with('mod_replace', $link, $userDN, ['userPassword' => $password])
  79. ->willReturn(true);
  80. $this->assertTrue($this->ldap->modReplace($link, $userDN, $password));
  81. }
  82. }