1
0

CapabilitiesTest.php 6.5 KB

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