StatusControllerTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Controller;
  8. use OCA\Encryption\Controller\StatusController;
  9. use OCA\Encryption\Session;
  10. use OCP\Encryption\IManager;
  11. use OCP\IL10N;
  12. use OCP\IRequest;
  13. use Test\TestCase;
  14. class StatusControllerTest extends TestCase {
  15. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  16. private $requestMock;
  17. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  18. private $l10nMock;
  19. /** @var Session|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $sessionMock;
  21. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  22. protected $encryptionManagerMock;
  23. /** @var StatusController */
  24. protected $controller;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->sessionMock = $this->getMockBuilder(Session::class)
  28. ->disableOriginalConstructor()->getMock();
  29. $this->requestMock = $this->createMock(IRequest::class);
  30. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  31. ->disableOriginalConstructor()->getMock();
  32. $this->l10nMock->expects($this->any())
  33. ->method('t')
  34. ->willReturnCallback(function ($message) {
  35. return $message;
  36. });
  37. $this->encryptionManagerMock = $this->createMock(IManager::class);
  38. $this->controller = new StatusController('encryptionTest',
  39. $this->requestMock,
  40. $this->l10nMock,
  41. $this->sessionMock,
  42. $this->encryptionManagerMock);
  43. }
  44. /**
  45. * @dataProvider dataTestGetStatus
  46. *
  47. * @param string $status
  48. * @param string $expectedStatus
  49. */
  50. public function testGetStatus($status, $expectedStatus): void {
  51. $this->sessionMock->expects($this->once())
  52. ->method('getStatus')->willReturn($status);
  53. $result = $this->controller->getStatus();
  54. $data = $result->getData();
  55. $this->assertSame($expectedStatus, $data['status']);
  56. }
  57. public function dataTestGetStatus() {
  58. return [
  59. [Session::INIT_EXECUTED, 'interactionNeeded'],
  60. [Session::INIT_SUCCESSFUL, 'success'],
  61. [Session::NOT_INITIALIZED, 'interactionNeeded'],
  62. ['unknown', 'error'],
  63. ];
  64. }
  65. }