aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/App/InfoParserTest.php
blob: 449f827df439b992c16288b1211cf6bf8d96809f (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
104
105
106
107
108
109
<?php

/**
 * SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2014-2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace Test\App;

use OC;
use OC\App\InfoParser;
use OCP\Cache\CappedMemoryCache;
use Test\TestCase;

class InfoParserTest extends TestCase {
	/** @var CappedMemoryCache */
	private static $cache;

	public static function setUpBeforeClass(): void {
		self::$cache = new CappedMemoryCache();
	}

	public function parserTest($expectedJson, $xmlFile, $cache = null) {
		$parser = new InfoParser($cache);

		$expectedData = null;
		if (!is_null($expectedJson)) {
			$expectedData = json_decode(file_get_contents(OC::$SERVERROOT . "/tests/data/app/$expectedJson"), true);
		}
		$data = $parser->parse(OC::$SERVERROOT . "/tests/data/app/$xmlFile");

		$this->assertEquals($expectedData, $data);
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('providesInfoXml')]
	public function testParsingValidXmlWithoutCache($expectedJson, $xmlFile): void {
		$this->parserTest($expectedJson, $xmlFile);
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('providesInfoXml')]
	public function testParsingValidXmlWithCache($expectedJson, $xmlFile): void {
		$this->parserTest($expectedJson, $xmlFile, self::$cache);
	}

	public static function providesInfoXml(): array {
		return [
			['expected-info.json', 'valid-info.xml'],
			[null, 'invalid-info.xml'],
			['expected-info.json', 'valid-info.xml'],
			[null, 'invalid-info.xml'],
			['navigation-one-item.json', 'navigation-one-item.xml'],
			['navigation-two-items.json', 'navigation-two-items.xml'],
			['various-single-item.json', 'various-single-item.xml'],
		];
	}

	/**
	 * Providers for the app data values
	 */
	public static function appDataProvider(): array {
		return [
			[
				['description' => " \t  This is a multiline \n test with \n \t \n \n some new lines   "],
				['description' => "This is a multiline \n test with \n \t \n \n some new lines"],
			],
			[
				['description' => " \t  This is a multiline \n test with \n \t   some new lines   "],
				['description' => "This is a multiline \n test with \n \t   some new lines"],
			],
			[
				['description' => hex2bin('5065726d657420646520732761757468656e7469666965722064616e732070697769676f20646972656374656d656e74206176656320736573206964656e74696669616e7473206f776e636c6f75642073616e73206c65732072657461706572206574206d657420c3a0206a6f757273206365757820636920656e20636173206465206368616e67656d656e74206465206d6f742064652070617373652e0d0a0d')],
				['description' => "Permet de s'authentifier dans piwigo directement avec ses identifiants owncloud sans les retaper et met à jours ceux ci en cas de changement de mot de passe."],
			],
			[
				['not-a-description' => " \t  This is a multiline \n test with \n \t   some new lines   "],
				[
					'not-a-description' => " \t  This is a multiline \n test with \n \t   some new lines   ",
					'description' => '',
				],
			],
			[
				['description' => [100, 'bla']],
				['description' => ''],
			],
		];
	}

	/**
	 * Test app info parser
	 */
	#[\PHPUnit\Framework\Attributes\DataProvider('appDataProvider')]
	public function testApplyL10NNoLanguage(array $data, array $expected): void {
		$parser = new InfoParser();
		$this->assertSame($expected, $parser->applyL10N($data));
	}

	public function testApplyL10N(): void {
		$parser = new InfoParser();
		$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-multi-lang.xml');
		$this->assertEquals('English', $parser->applyL10N($data, 'en')['description']);
		$this->assertEquals('German', $parser->applyL10N($data, 'de')['description']);
	}

	public function testApplyL10NSingleLanguage(): void {
		$parser = new InfoParser();
		$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-single-lang.xml');
		$this->assertEquals('English', $parser->applyL10N($data, 'en')['description']);
	}
}