LDAPTest.php 2.6 KB

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