NavigationControllerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Controller;
  7. use OC\Core\Controller\NavigationController;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\DataResponse;
  10. use OCP\INavigationManager;
  11. use OCP\IRequest;
  12. use OCP\IURLGenerator;
  13. use Test\TestCase;
  14. class NavigationControllerTest extends TestCase {
  15. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  16. private $request;
  17. /** @var INavigationManager|\PHPUnit\Framework\MockObject\MockObject */
  18. private $navigationManager;
  19. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  20. private $urlGenerator;
  21. /** @var NavigationController */
  22. private $controller;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->request = $this->createMock(IRequest::class);
  26. $this->navigationManager = $this->createMock(INavigationManager::class);
  27. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  28. $this->controller = new NavigationController(
  29. 'core',
  30. $this->request,
  31. $this->navigationManager,
  32. $this->urlGenerator
  33. );
  34. }
  35. public function dataGetNavigation() {
  36. return [
  37. [false], [true]
  38. ];
  39. }
  40. /** @dataProvider dataGetNavigation */
  41. public function testGetAppNavigation($absolute) {
  42. $this->navigationManager->expects($this->once())
  43. ->method('getAll')
  44. ->with('link')
  45. ->willReturn(['files' => ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]);
  46. if ($absolute) {
  47. $this->urlGenerator->expects($this->any())
  48. ->method('getBaseURL')
  49. ->willReturn('http://localhost/');
  50. $this->urlGenerator->expects($this->exactly(2))
  51. ->method('getAbsoluteURL')
  52. ->withConsecutive(['/index.php/apps/files'], ['icon'])
  53. ->willReturnOnConsecutiveCalls(
  54. 'http://localhost/index.php/apps/files',
  55. 'http://localhost/icon'
  56. );
  57. $actual = $this->controller->getAppsNavigation($absolute);
  58. $this->assertInstanceOf(DataResponse::class, $actual);
  59. $this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']);
  60. $this->assertEquals('http://localhost/icon', $actual->getData()[0]['icon']);
  61. } else {
  62. $actual = $this->controller->getAppsNavigation($absolute);
  63. $this->assertInstanceOf(DataResponse::class, $actual);
  64. $this->assertEquals('/index.php/apps/files', $actual->getData()[0]['href']);
  65. $this->assertEquals('icon', $actual->getData()[0]['icon']);
  66. }
  67. }
  68. /** @dataProvider dataGetNavigation */
  69. public function testGetSettingsNavigation($absolute) {
  70. $this->navigationManager->expects($this->once())
  71. ->method('getAll')
  72. ->with('settings')
  73. ->willReturn(['settings' => ['id' => 'settings', 'href' => '/index.php/settings/user', 'icon' => '/core/img/settings.svg'] ]);
  74. if ($absolute) {
  75. $this->urlGenerator->expects($this->any())
  76. ->method('getBaseURL')
  77. ->willReturn('http://localhost/');
  78. $this->urlGenerator->expects($this->exactly(2))
  79. ->method('getAbsoluteURL')
  80. ->withConsecutive(
  81. ['/index.php/settings/user'],
  82. ['/core/img/settings.svg']
  83. )
  84. ->willReturnOnConsecutiveCalls(
  85. 'http://localhost/index.php/settings/user',
  86. 'http://localhost/core/img/settings.svg'
  87. );
  88. $actual = $this->controller->getSettingsNavigation($absolute);
  89. $this->assertInstanceOf(DataResponse::class, $actual);
  90. $this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']);
  91. $this->assertEquals('http://localhost/core/img/settings.svg', $actual->getData()[0]['icon']);
  92. } else {
  93. $actual = $this->controller->getSettingsNavigation($absolute);
  94. $this->assertInstanceOf(DataResponse::class, $actual);
  95. $this->assertEquals('/index.php/settings/user', $actual->getData()[0]['href']);
  96. $this->assertEquals('/core/img/settings.svg', $actual->getData()[0]['icon']);
  97. }
  98. }
  99. public function testGetAppNavigationEtagMatch() {
  100. $navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  101. $this->request->expects($this->once())
  102. ->method('getHeader')
  103. ->with('If-None-Match')
  104. ->willReturn(md5(json_encode($navigation)));
  105. $this->navigationManager->expects($this->once())
  106. ->method('getAll')
  107. ->with('link')
  108. ->willReturn($navigation);
  109. $actual = $this->controller->getAppsNavigation();
  110. $this->assertInstanceOf(DataResponse::class, $actual);
  111. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  112. }
  113. public function testGetSettingsNavigationEtagMatch() {
  114. $navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  115. $this->request->expects($this->once())
  116. ->method('getHeader')
  117. ->with('If-None-Match')
  118. ->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ])));
  119. $this->navigationManager->expects($this->once())
  120. ->method('getAll')
  121. ->with('settings')
  122. ->willReturn($navigation);
  123. $actual = $this->controller->getSettingsNavigation();
  124. $this->assertInstanceOf(DataResponse::class, $actual);
  125. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  126. }
  127. }