CapabilitiesTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests;
  7. use OCA\Theming\Capabilities;
  8. use OCA\Theming\ImageManager;
  9. use OCA\Theming\ThemingDefaults;
  10. use OCA\Theming\Util;
  11. use OCP\App\IAppManager;
  12. use OCP\Files\IAppData;
  13. use OCP\IConfig;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserSession;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. /**
  19. * Class CapabilitiesTest
  20. *
  21. * @package OCA\Theming\Tests
  22. */
  23. class CapabilitiesTest extends TestCase {
  24. /** @var ThemingDefaults|MockObject */
  25. protected $theming;
  26. /** @var IURLGenerator|MockObject */
  27. protected $url;
  28. /** @var IConfig|MockObject */
  29. protected $config;
  30. /** @var Util|MockObject */
  31. protected $util;
  32. protected IUserSession $userSession;
  33. /** @var Capabilities */
  34. protected $capabilities;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->theming = $this->createMock(ThemingDefaults::class);
  38. $this->url = $this->createMock(IURLGenerator::class);
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->util = $this->createMock(Util::class);
  41. $this->userSession = $this->createMock(IUserSession::class);
  42. $this->capabilities = new Capabilities(
  43. $this->theming,
  44. $this->util,
  45. $this->url,
  46. $this->config,
  47. $this->userSession,
  48. );
  49. }
  50. public function dataGetCapabilities() {
  51. return [
  52. ['name', 'url', 'slogan', '#FFFFFF', '#000000', 'logo', 'background', '#fff', '#000', 'http://absolute/', true, [
  53. 'name' => 'name',
  54. 'url' => 'url',
  55. 'slogan' => 'slogan',
  56. 'color' => '#FFFFFF',
  57. 'color-text' => '#000000',
  58. 'color-element' => '#b3b3b3',
  59. 'color-element-bright' => '#b3b3b3',
  60. 'color-element-dark' => '#FFFFFF',
  61. 'logo' => 'http://absolute/logo',
  62. 'background' => 'http://absolute/background',
  63. 'background-text' => '#000',
  64. 'background-plain' => false,
  65. 'background-default' => false,
  66. 'logoheader' => 'http://absolute/logo',
  67. 'favicon' => 'http://absolute/logo',
  68. ]],
  69. ['name1', 'url2', 'slogan3', '#01e4a0', '#ffffff', 'logo5', 'background6', '#fff', '#000', 'http://localhost/', false, [
  70. 'name' => 'name1',
  71. 'url' => 'url2',
  72. 'slogan' => 'slogan3',
  73. 'color' => '#01e4a0',
  74. 'color-text' => '#ffffff',
  75. 'color-element' => '#01e4a0',
  76. 'color-element-bright' => '#01e4a0',
  77. 'color-element-dark' => '#01e4a0',
  78. 'logo' => 'http://localhost/logo5',
  79. 'background' => 'http://localhost/background6',
  80. 'background-text' => '#000',
  81. 'background-plain' => false,
  82. 'background-default' => true,
  83. 'logoheader' => 'http://localhost/logo5',
  84. 'favicon' => 'http://localhost/logo5',
  85. ]],
  86. ['name1', 'url2', 'slogan3', '#000000', '#ffffff', 'logo5', 'backgroundColor', '#000000', '#ffffff', 'http://localhost/', true, [
  87. 'name' => 'name1',
  88. 'url' => 'url2',
  89. 'slogan' => 'slogan3',
  90. 'color' => '#000000',
  91. 'color-text' => '#ffffff',
  92. 'color-element' => '#4d4d4d',
  93. 'color-element-bright' => '#4d4d4d',
  94. 'color-element-dark' => '#4d4d4d',
  95. 'logo' => 'http://localhost/logo5',
  96. 'background' => '#000000',
  97. 'background-text' => '#ffffff',
  98. 'background-plain' => true,
  99. 'background-default' => false,
  100. 'logoheader' => 'http://localhost/logo5',
  101. 'favicon' => 'http://localhost/logo5',
  102. ]],
  103. ['name1', 'url2', 'slogan3', '#000000', '#ffffff', 'logo5', 'backgroundColor', '#000000', '#ffffff', 'http://localhost/', false, [
  104. 'name' => 'name1',
  105. 'url' => 'url2',
  106. 'slogan' => 'slogan3',
  107. 'color' => '#000000',
  108. 'color-text' => '#ffffff',
  109. 'color-element' => '#4d4d4d',
  110. 'color-element-bright' => '#4d4d4d',
  111. 'color-element-dark' => '#4d4d4d',
  112. 'logo' => 'http://localhost/logo5',
  113. 'background' => '#000000',
  114. 'background-text' => '#ffffff',
  115. 'background-plain' => true,
  116. 'background-default' => true,
  117. 'logoheader' => 'http://localhost/logo5',
  118. 'favicon' => 'http://localhost/logo5',
  119. ]],
  120. ];
  121. }
  122. /**
  123. * @dataProvider dataGetCapabilities
  124. * @param string $name
  125. * @param string $url
  126. * @param string $slogan
  127. * @param string $color
  128. * @param string $textColor
  129. * @param string $logo
  130. * @param string $background
  131. * @param string $baseUrl
  132. * @param bool $backgroundThemed
  133. * @param string[] $expected
  134. */
  135. public function testGetCapabilities($name, $url, $slogan, $color, $textColor, $logo, $background, $backgroundColor, $backgroundTextColor, $baseUrl, $backgroundThemed, array $expected): void {
  136. $this->config->expects($this->once())
  137. ->method('getAppValue')
  138. ->willReturn($background);
  139. $this->theming->expects($this->once())
  140. ->method('getName')
  141. ->willReturn($name);
  142. $this->theming->expects($this->once())
  143. ->method('getBaseUrl')
  144. ->willReturn($url);
  145. $this->theming->expects($this->once())
  146. ->method('getSlogan')
  147. ->willReturn($slogan);
  148. $this->theming->expects($this->once())
  149. ->method('getColorBackground')
  150. ->willReturn($backgroundColor);
  151. $this->theming->expects($this->once())
  152. ->method('getTextColorBackground')
  153. ->willReturn($backgroundTextColor);
  154. $this->theming->expects($this->atLeast(1))
  155. ->method('getDefaultColorPrimary')
  156. ->willReturn($color);
  157. $this->theming->expects($this->exactly(3))
  158. ->method('getLogo')
  159. ->willReturn($logo);
  160. $util = new Util($this->config, $this->createMock(IAppManager::class), $this->createMock(IAppData::class), $this->createMock(ImageManager::class));
  161. $this->util->expects($this->exactly(3))
  162. ->method('elementColor')
  163. ->with($color)
  164. ->willReturnCallback(static function (string $color, ?bool $brightBackground = null) use ($util) {
  165. return $util->elementColor($color, $brightBackground);
  166. });
  167. $this->util->expects($this->any())
  168. ->method('invertTextColor')
  169. ->willReturnCallback(fn () => $textColor === '#000000');
  170. $this->util->expects($this->once())
  171. ->method('isBackgroundThemed')
  172. ->willReturn($backgroundThemed);
  173. if ($background !== 'backgroundColor') {
  174. $this->theming->expects($this->once())
  175. ->method('getBackground')
  176. ->willReturn($background);
  177. $this->url->expects($this->exactly(4))
  178. ->method('getAbsoluteURL')
  179. ->willReturnCallback(function ($url) use ($baseUrl) {
  180. return $baseUrl . $url;
  181. });
  182. } else {
  183. $this->url->expects($this->exactly(3))
  184. ->method('getAbsoluteURL')
  185. ->willReturnCallback(function ($url) use ($baseUrl) {
  186. return $baseUrl . $url;
  187. });
  188. }
  189. $this->assertEquals(['theming' => $expected], $this->capabilities->getCapabilities());
  190. }
  191. }