ErrorHandlerTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. namespace Test;
  9. use OC\Log\ErrorHandler;
  10. use OCP\ILogger;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use Psr\Log\LoggerInterface;
  13. class ErrorHandlerTest extends TestCase {
  14. /** @var MockObject */
  15. private LoggerInterface $logger;
  16. private ErrorHandler $errorHandler;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->logger = $this->createMock(LoggerInterface::class);
  20. $this->errorHandler = new ErrorHandler(
  21. $this->logger
  22. );
  23. }
  24. /**
  25. * provide username, password combinations for testRemovePassword
  26. * @return array
  27. */
  28. public function passwordProvider() {
  29. return [
  30. ['us:er', 'pass@word'],
  31. ['us:er', 'password'],
  32. ['user', '-C:R,w)@6*}'],
  33. ['user', 'pass:word'],
  34. ['user', 'pass@word'],
  35. ['user', 'password'],
  36. ['user:test@cloud', 'password'],
  37. ['user@owncloud.org', 'password'],
  38. ['user@test@owncloud.org', 'password'],
  39. ];
  40. }
  41. /**
  42. * @dataProvider passwordProvider
  43. * @param string $username
  44. * @param string $password
  45. */
  46. public function testRemovePasswordFromError($username, $password): void {
  47. $url = 'http://' . $username . ':' . $password . '@owncloud.org';
  48. $expectedResult = 'http://xxx:xxx@owncloud.org';
  49. $this->logger->expects(self::once())
  50. ->method('log')
  51. ->with(
  52. ILogger::ERROR,
  53. 'Could not reach ' . $expectedResult . ' at file#4',
  54. ['app' => 'PHP'],
  55. );
  56. $result = $this->errorHandler->onError(E_USER_ERROR, 'Could not reach ' . $url, 'file', 4);
  57. self::assertTrue($result);
  58. }
  59. }