TemplateLayoutTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\InitialStateService;
  9. use OC\TemplateLayout;
  10. use OCP\App\IAppManager;
  11. use OCP\AppFramework\Http\TemplateResponse;
  12. use OCP\IConfig;
  13. class TemplateLayoutTest extends \Test\TestCase {
  14. /** @dataProvider dataVersionHash */
  15. public function testVersionHash($path, $file, $installed, $debug, $expected): void {
  16. $appManager = $this->createMock(IAppManager::class);
  17. $appManager->expects(self::any())
  18. ->method('getAppVersion')
  19. ->willReturnCallback(fn ($appId) => match ($appId) {
  20. 'shippedApp' => 'shipped_1',
  21. 'otherApp' => 'other_2',
  22. default => "$appId",
  23. });
  24. $appManager->expects(self::any())
  25. ->method('isShipped')
  26. ->willReturnCallback(fn (string $app) => $app === 'shippedApp');
  27. $config = $this->createMock(IConfig::class);
  28. $config->expects(self::atLeastOnce())
  29. ->method('getSystemValueBool')
  30. ->willReturnMap([
  31. ['installed', false, $installed],
  32. ['debug', false, $debug],
  33. ]);
  34. $config->expects(self::any())
  35. ->method('getAppValue')
  36. ->with('theming', 'cachebuster', '0')
  37. ->willReturn('42');
  38. $initialState = $this->createMock(InitialStateService::class);
  39. $this->overwriteService(IConfig::class, $config);
  40. $this->overwriteService(IAppManager::class, $appManager);
  41. $this->overwriteService(InitialStateService::class, $initialState);
  42. $layout = $this->getMockBuilder(TemplateLayout::class)
  43. ->onlyMethods(['getAppNamefromPath'])
  44. ->setConstructorArgs([TemplateResponse::RENDER_AS_ERROR])
  45. ->getMock();
  46. self::invokePrivate(TemplateLayout::class, 'versionHash', ['version_hash']);
  47. $layout->expects(self::any())
  48. ->method('getAppNamefromPath')
  49. ->willReturnCallback(fn ($appName) => match($appName) {
  50. 'apps/shipped' => 'shippedApp',
  51. 'other/app.css' => 'otherApp',
  52. default => false,
  53. });
  54. $hash = self::invokePrivate($layout, 'getVersionHashSuffix', [$path, $file]);
  55. self::assertEquals($expected, $hash);
  56. }
  57. public static function dataVersionHash() {
  58. return [
  59. 'no hash if in debug mode' => ['apps/shipped', 'style.css', true, true, ''],
  60. 'only version hash if not installed' => ['apps/shipped', 'style.css', false, false, '?v=version_hash'],
  61. 'version hash with cache buster if app not found' => ['unknown/path', '', true, false, '?v=version_hash-42'],
  62. 'version hash with cache buster if neither path nor file provided' => [false, false, true, false, '?v=version_hash-42'],
  63. 'app version hash if external app' => ['', 'other/app.css', true, false, '?v=' . substr(md5('other_2'), 0, 8) . '-42'],
  64. 'app version and version hash if shipped app' => ['apps/shipped', 'style.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
  65. 'prefer path over file' => ['apps/shipped', 'other/app.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
  66. ];
  67. }
  68. }