aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/TemplateLayoutTest.php
blob: 29e31b3f3911a88af23a34eb85667b09beeab9c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test;

use OC\InitialStateService;
use OC\TemplateLayout;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\INavigationManager;
use OCP\Template\ITemplateManager;
use PHPUnit\Framework\MockObject\MockObject;

class TemplateLayoutTest extends \Test\TestCase {
	private IConfig&MockObject $config;
	private IAppManager&MockObject $appManager;
	private InitialStateService&MockObject $initialState;
	private INavigationManager&MockObject $navigationManager;
	private ITemplateManager&MockObject $templateManager;

	private TemplateLayout $templateLayout;

	protected function setUp(): void {
		parent::setUp();

		$this->config = $this->createMock(IConfig::class);
		$this->appManager = $this->createMock(IAppManager::class);
		$this->initialState = $this->createMock(InitialStateService::class);
		$this->navigationManager = $this->createMock(INavigationManager::class);
		$this->templateManager = $this->createMock(ITemplateManager::class);
	}

	/** @dataProvider dataVersionHash */
	public function testVersionHash($path, $file, $installed, $debug, $expected): void {
		$this->appManager->expects(self::any())
			->method('getAppVersion')
			->willReturnCallback(fn ($appId) => match ($appId) {
				'shippedApp' => 'shipped_1',
				'otherApp' => 'other_2',
				default => "$appId",
			});
		$this->appManager->expects(self::any())
			->method('isShipped')
			->willReturnCallback(fn (string $app) => $app === 'shippedApp');

		$this->config->expects(self::atLeastOnce())
			->method('getSystemValueBool')
			->willReturnMap([
				['installed', false, $installed],
				['debug', false, $debug],
			]);
		$this->config->expects(self::any())
			->method('getAppValue')
			->with('theming', 'cachebuster', '0')
			->willReturn('42');

		$this->templateLayout = $this->getMockBuilder(TemplateLayout::class)
			->onlyMethods(['getAppNamefromPath'])
			->setConstructorArgs([
				$this->config,
				$this->appManager,
				$this->initialState,
				$this->navigationManager,
				$this->templateManager,
			])
			->getMock();

		$layout = $this->templateLayout->getPageTemplate(TemplateResponse::RENDER_AS_ERROR, '');

		self::invokePrivate(TemplateLayout::class, 'versionHash', ['version_hash']);

		$this->templateLayout->expects(self::any())
			->method('getAppNamefromPath')
			->willReturnCallback(fn ($appName) => match($appName) {
				'apps/shipped' => 'shippedApp',
				'other/app.css' => 'otherApp',
				default => false,
			});

		$hash = self::invokePrivate($this->templateLayout, 'getVersionHashSuffix', [$path, $file]);
		self::assertEquals($expected, $hash);
	}

	public static function dataVersionHash() {
		return [
			'no hash if in debug mode' => ['apps/shipped', 'style.css', true, true, ''],
			'only version hash if not installed' => ['apps/shipped', 'style.css', false, false, '?v=version_hash'],
			'version hash with cache buster if app not found' => ['unknown/path', '', true, false, '?v=version_hash-42'],
			'version hash with cache buster if neither path nor file provided' => [false, false, true, false, '?v=version_hash-42'],
			'app version hash if external app' => ['', 'other/app.css', true, false, '?v=' . substr(md5('other_2'), 0, 8) . '-42'],
			'app version and version hash if shipped app' => ['apps/shipped', 'style.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
			'prefer path over file' => ['apps/shipped', 'other/app.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
		];
	}

}