NavigationControllerTest.php 5.7 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->exactly(2))
  68. ->method('getAbsoluteURL')
  69. ->withConsecutive(['/index.php/apps/files'], ['icon'])
  70. ->willReturnOnConsecutiveCalls(
  71. 'http://localhost/index.php/apps/files',
  72. 'http://localhost/icon'
  73. );
  74. $actual = $this->controller->getAppsNavigation($absolute);
  75. $this->assertInstanceOf(DataResponse::class, $actual);
  76. $this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']);
  77. $this->assertEquals('http://localhost/icon', $actual->getData()[0]['icon']);
  78. } else {
  79. $actual = $this->controller->getAppsNavigation($absolute);
  80. $this->assertInstanceOf(DataResponse::class, $actual);
  81. $this->assertEquals('/index.php/apps/files', $actual->getData()[0]['href']);
  82. $this->assertEquals('icon', $actual->getData()[0]['icon']);
  83. }
  84. }
  85. /** @dataProvider dataGetNavigation */
  86. public function testGetSettingsNavigation($absolute) {
  87. $this->navigationManager->expects($this->once())
  88. ->method('getAll')
  89. ->with('settings')
  90. ->willReturn(['settings' => ['id' => 'settings', 'href' => '/index.php/settings/user', 'icon' => '/core/img/settings.svg'] ]);
  91. if ($absolute) {
  92. $this->urlGenerator->expects($this->any())
  93. ->method('getBaseURL')
  94. ->willReturn('http://localhost/');
  95. $this->urlGenerator->expects($this->exactly(2))
  96. ->method('getAbsoluteURL')
  97. ->withConsecutive(
  98. ['/index.php/settings/user'],
  99. ['/core/img/settings.svg']
  100. )
  101. ->willReturnOnConsecutiveCalls(
  102. 'http://localhost/index.php/settings/user',
  103. 'http://localhost/core/img/settings.svg'
  104. );
  105. $actual = $this->controller->getSettingsNavigation($absolute);
  106. $this->assertInstanceOf(DataResponse::class, $actual);
  107. $this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']);
  108. $this->assertEquals('http://localhost/core/img/settings.svg', $actual->getData()[0]['icon']);
  109. } else {
  110. $actual = $this->controller->getSettingsNavigation($absolute);
  111. $this->assertInstanceOf(DataResponse::class, $actual);
  112. $this->assertEquals('/index.php/settings/user', $actual->getData()[0]['href']);
  113. $this->assertEquals('/core/img/settings.svg', $actual->getData()[0]['icon']);
  114. }
  115. }
  116. public function testGetAppNavigationEtagMatch() {
  117. $navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  118. $this->request->expects($this->once())
  119. ->method('getHeader')
  120. ->with('If-None-Match')
  121. ->willReturn(md5(json_encode($navigation)));
  122. $this->navigationManager->expects($this->once())
  123. ->method('getAll')
  124. ->with('link')
  125. ->willReturn($navigation);
  126. $actual = $this->controller->getAppsNavigation();
  127. $this->assertInstanceOf(DataResponse::class, $actual);
  128. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  129. }
  130. public function testGetSettingsNavigationEtagMatch() {
  131. $navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
  132. $this->request->expects($this->once())
  133. ->method('getHeader')
  134. ->with('If-None-Match')
  135. ->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ])));
  136. $this->navigationManager->expects($this->once())
  137. ->method('getAll')
  138. ->with('settings')
  139. ->willReturn($navigation);
  140. $actual = $this->controller->getSettingsNavigation();
  141. $this->assertInstanceOf(DataResponse::class, $actual);
  142. $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
  143. }
  144. }