NavigationControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  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 Tests\Core\Controller;
  24. use OC\Core\Controller\NavigationController;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\INavigationManager;
  28. use OCP\IRequest;
  29. use OCP\IURLGenerator;
  30. use Test\TestCase;
  31. class NavigationControllerTest extends TestCase {
  32. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  33. private $request;
  34. /** @var INavigationManager|\PHPUnit\Framework\MockObject\MockObject */
  35. private $navigationManager;
  36. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  37. private $urlGenerator;
  38. /** @var NavigationController */
  39. private $controller;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->navigationManager = $this->createMock(INavigationManager::class);
  44. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  45. $this->controller = new NavigationController(
  46. 'core',
  47. $this->request,
  48. $this->navigationManager,
  49. $this->urlGenerator
  50. );
  51. }
  52. public function dataGetNavigation() {
  53. return [
  54. [false], [true]
  55. ];
  56. }
  57. /** @dataProvider dataGetNavigation */
  58. public function testGetAppNavigation($absolute) {
  59. $this->navigationManager->expects($this->once())
  60. ->method('getAll')
  61. ->with('link')
  62. ->willReturn(['files' => ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]);
  63. if ($absolute) {
  64. $this->urlGenerator->expects($this->any())
  65. ->method('getBaseURL')
  66. ->willReturn('http://localhost/');
  67. $this->urlGenerator->expects($this->at(1))
  68. ->method('getAbsoluteURL')
  69. ->with('/index.php/apps/files')
  70. ->willReturn('http://localhost/index.php/apps/files');
  71. $this->urlGenerator->expects($this->at(3))
  72. ->method('getAbsoluteURL')
  73. ->with('icon')
  74. ->willReturn('http://localhost/icon');
  75. $actual = $this->controller->getAppsNavigation($absolute);
  76. $this->assertInstanceOf(DataResponse::class, $actual);
  77. $this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']);
  78. $this->assertEquals('http://localhost/icon', $actual->getData()[0]['icon']);
  79. } else {
  80. $actual = $this->controller->getAppsNavigation($absolute);
  81. $this->assertInstanceOf(DataResponse::class, $actual);
  82. $this->assertEquals('/index.php/apps/files', $actual->getData()[0]['href']);
  83. $this->assertEquals('icon', $actual->getData()[0]['icon']);
  84. }
  85. }
  86. /** @dataProvider dataGetNavigation */
  87. public function testGetSettingsNavigation($absolute) {
  88. $this->navigationManager->expects($this->once())
  89. ->method('getAll')
  90. ->with('settings')
  91. ->willReturn(['settings' => ['id' => 'settings', 'href' => '/index.php/settings/user', 'icon' => '/core/img/settings.svg'] ]);
  92. if ($absolute) {
  93. $this->urlGenerator->expects($this->any())
  94. ->method('getBaseURL')
  95. ->willReturn('http://localhost/');
  96. $this->urlGenerator->expects($this->at(1))
  97. ->method('getAbsoluteURL')
  98. ->with('/index.php/settings/user')
  99. ->willReturn('http://localhost/index.php/settings/user');
  100. $this->urlGenerator->expects($this->at(3))
  101. ->method('getAbsoluteURL')
  102. ->with('/core/img/settings.svg')
  103. ->willReturn('http://localhost/core/img/settings.svg');
  104. $actual = $this->controller->getSettingsNavigation($absolute);
  105. $this->assertInstanceOf(DataResponse::class, $actual);
  106. $this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']);
  107. $this->assertEquals('http://localhost/core/img/settings.svg', $actual->getData()[0]['icon']);
  108. } else {
  109. $actual = $this->controller->getSettingsNavigation($absolute);
  110. $this->assertInstanceOf(DataResponse::class, $actual);
  111. $this->assertEquals('/index.php/settings/user', $actual->getData()[0]['href']);
  112. $this->assertEquals('/core/img/settings.svg', $actual->getData()[0]['icon']);
  113. }
  114. }
  115. public function testGetAppNavigationEtagMatch() {
  116. $navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  117. $this->request->expects($this->once())
  118. ->method('getHeader')
  119. ->with('If-None-Match')
  120. ->willReturn(md5(json_encode($navigation)));
  121. $this->navigationManager->expects($this->once())
  122. ->method('getAll')
  123. ->with('link')
  124. ->willReturn($navigation);
  125. $actual = $this->controller->getAppsNavigation();
  126. $this->assertInstanceOf(DataResponse::class, $actual);
  127. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  128. }
  129. public function testGetSettingsNavigationEtagMatch() {
  130. $navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  131. $this->request->expects($this->once())
  132. ->method('getHeader')
  133. ->with('If-None-Match')
  134. ->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ])));
  135. $this->navigationManager->expects($this->once())
  136. ->method('getAll')
  137. ->with('settings')
  138. ->willReturn($navigation);
  139. $actual = $this->controller->getSettingsNavigation();
  140. $this->assertInstanceOf(DataResponse::class, $actual);
  141. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  142. }
  143. }