Skip to content

Commit 1f5e232

Browse files
committed
chore: Move info parsing related method from OC_App to InfoParser
Also fix the tests Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent fb4e580 commit 1f5e232

6 files changed

Lines changed: 130 additions & 158 deletions

File tree

lib/private/App/AppManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ public function getAppInfoByPath(string $path, ?string $lang = null): ?array {
771771
$data = $parser->parse($path);
772772

773773
if (is_array($data)) {
774-
$data = \OC_App::parseAppInfo($data, $lang);
774+
$data = $parser->applyL10N($data, $lang);
775775
}
776776

777777
return $data;

lib/private/App/InfoParser.php

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
* @param string $file the xml file to be loaded
2424
* @return null|array where null is an indicator for an error
2525
*/
26-
public function parse($file) {
26+
public function parse(string $file): ?array {
2727
if (!file_exists($file)) {
2828
return null;
2929
}
@@ -219,7 +219,7 @@ private function isNavigationItem(array $data): bool {
219219

220220
public function xmlToArray(\SimpleXMLElement $xml): array|string {
221221
$children = $xml->children();
222-
if ($children === null) {
222+
if ($children === null || count($children) === 0) {
223223
return (string)$xml;
224224
}
225225

@@ -271,4 +271,78 @@ public function xmlToArray(\SimpleXMLElement $xml): array|string {
271271

272272
return $array;
273273
}
274+
275+
/**
276+
* Select the appropriate l10n version for fields name, summary and description
277+
*/
278+
public function applyL10N(array $data, ?string $lang = null): array {
279+
if ($lang !== '' && $lang !== null) {
280+
if (isset($data['name']) && is_array($data['name'])) {
281+
$data['name'] = $this->findBestL10NOption($data['name'], $lang);
282+
}
283+
if (isset($data['summary']) && is_array($data['summary'])) {
284+
$data['summary'] = $this->findBestL10NOption($data['summary'], $lang);
285+
}
286+
if (isset($data['description']) && is_array($data['description'])) {
287+
$data['description'] = trim($this->findBestL10NOption($data['description'], $lang));
288+
}
289+
} elseif (isset($data['description']) && is_string($data['description'])) {
290+
$data['description'] = trim($data['description']);
291+
} else {
292+
$data['description'] = '';
293+
}
294+
295+
return $data;
296+
}
297+
298+
protected function findBestL10NOption(array $options, string $lang): string {
299+
// only a single option
300+
if (isset($options['@value'])) {
301+
return $options['@value'];
302+
}
303+
304+
$fallback = $similarLangFallback = $englishFallback = false;
305+
306+
$lang = strtolower($lang);
307+
$similarLang = $lang;
308+
$pos = strpos($similarLang, '_');
309+
if ($pos !== false && $pos > 0) {
310+
// For "de_DE" we want to find "de" and the other way around
311+
$similarLang = substr($lang, 0, $pos);
312+
}
313+
314+
foreach ($options as $option) {
315+
if (is_array($option)) {
316+
if ($fallback === false) {
317+
$fallback = $option['@value'];
318+
}
319+
320+
if (!isset($option['@attributes']['lang'])) {
321+
continue;
322+
}
323+
324+
$attributeLang = strtolower($option['@attributes']['lang']);
325+
if ($attributeLang === $lang) {
326+
return $option['@value'];
327+
}
328+
329+
if ($attributeLang === $similarLang) {
330+
$similarLangFallback = $option['@value'];
331+
} elseif (str_starts_with($attributeLang, $similarLang . '_')) {
332+
if ($similarLangFallback === false) {
333+
$similarLangFallback = $option['@value'];
334+
}
335+
}
336+
} else {
337+
$englishFallback = $option;
338+
}
339+
}
340+
341+
if ($similarLangFallback !== false) {
342+
return $similarLangFallback;
343+
} elseif ($englishFallback !== false) {
344+
return $englishFallback;
345+
}
346+
return (string)$fallback;
347+
}
274348
}

lib/private/legacy/OC_App.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -775,81 +775,6 @@ private static function setupLiveMigrations(string $appId, array $steps) {
775775
}
776776
}
777777

778-
protected static function findBestL10NOption(array $options, string $lang): string {
779-
// only a single option
780-
if (isset($options['@value'])) {
781-
return $options['@value'];
782-
}
783-
784-
$fallback = $similarLangFallback = $englishFallback = false;
785-
786-
$lang = strtolower($lang);
787-
$similarLang = $lang;
788-
if (strpos($similarLang, '_')) {
789-
// For "de_DE" we want to find "de" and the other way around
790-
$similarLang = substr($lang, 0, strpos($lang, '_'));
791-
}
792-
793-
foreach ($options as $option) {
794-
if (is_array($option)) {
795-
if ($fallback === false) {
796-
$fallback = $option['@value'];
797-
}
798-
799-
if (!isset($option['@attributes']['lang'])) {
800-
continue;
801-
}
802-
803-
$attributeLang = strtolower($option['@attributes']['lang']);
804-
if ($attributeLang === $lang) {
805-
return $option['@value'];
806-
}
807-
808-
if ($attributeLang === $similarLang) {
809-
$similarLangFallback = $option['@value'];
810-
} elseif (str_starts_with($attributeLang, $similarLang . '_')) {
811-
if ($similarLangFallback === false) {
812-
$similarLangFallback = $option['@value'];
813-
}
814-
}
815-
} else {
816-
$englishFallback = $option;
817-
}
818-
}
819-
820-
if ($similarLangFallback !== false) {
821-
return $similarLangFallback;
822-
} elseif ($englishFallback !== false) {
823-
return $englishFallback;
824-
}
825-
return (string)$fallback;
826-
}
827-
828-
/**
829-
* parses the app data array and enhanced the 'description' value
830-
*
831-
* @param array $data the app data
832-
* @param string $lang
833-
* @return array improved app data
834-
*/
835-
public static function parseAppInfo(array $data, $lang = null): array {
836-
if ($lang && isset($data['name']) && is_array($data['name'])) {
837-
$data['name'] = self::findBestL10NOption($data['name'], $lang);
838-
}
839-
if ($lang && isset($data['summary']) && is_array($data['summary'])) {
840-
$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
841-
}
842-
if ($lang && isset($data['description']) && is_array($data['description'])) {
843-
$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
844-
} elseif (isset($data['description']) && is_string($data['description'])) {
845-
$data['description'] = trim($data['description']);
846-
} else {
847-
$data['description'] = '';
848-
}
849-
850-
return $data;
851-
}
852-
853778
/**
854779
* @param \OCP\IConfig $config
855780
* @param \OCP\IL10N $l

tests/lib/App/DependencyAnalyzerTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,6 @@ public static function providesOC(): array {
277277
],
278278
],
279279
],
280-
[
281-
[
282-
'Server version 10 or higher is required.',
283-
],
284-
[
285-
'nextcloud' => [
286-
'@attributes' => [
287-
'min-version' => '9.1',
288-
],
289-
],
290-
],
291-
],
292280
[
293281
[
294282
'Server version 9.2 or higher is required.',
@@ -382,18 +370,6 @@ public static function providesOC(): array {
382370
],
383371
],
384372
],
385-
[
386-
[
387-
'Server version 10 or higher is required.',
388-
],
389-
[
390-
'owncloud' => [
391-
'@attributes' => [
392-
'min-version' => '9.1',
393-
],
394-
],
395-
],
396-
],
397373
[
398374
[
399375
'Server version 9.2 or higher is required.',

tests/lib/App/InfoParserTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,57 @@ public static function providesInfoXml(): array {
5353
['various-single-item.json', 'various-single-item.xml'],
5454
];
5555
}
56+
57+
/**
58+
* Providers for the app data values
59+
*/
60+
public static function appDataProvider(): array {
61+
return [
62+
[
63+
['description' => " \t This is a multiline \n test with \n \t \n \n some new lines "],
64+
['description' => "This is a multiline \n test with \n \t \n \n some new lines"],
65+
],
66+
[
67+
['description' => " \t This is a multiline \n test with \n \t some new lines "],
68+
['description' => "This is a multiline \n test with \n \t some new lines"],
69+
],
70+
[
71+
['description' => hex2bin('5065726d657420646520732761757468656e7469666965722064616e732070697769676f20646972656374656d656e74206176656320736573206964656e74696669616e7473206f776e636c6f75642073616e73206c65732072657461706572206574206d657420c3a0206a6f757273206365757820636920656e20636173206465206368616e67656d656e74206465206d6f742064652070617373652e0d0a0d')],
72+
['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."],
73+
],
74+
[
75+
['not-a-description' => " \t This is a multiline \n test with \n \t some new lines "],
76+
[
77+
'not-a-description' => " \t This is a multiline \n test with \n \t some new lines ",
78+
'description' => '',
79+
],
80+
],
81+
[
82+
['description' => [100, 'bla']],
83+
['description' => ''],
84+
],
85+
];
86+
}
87+
88+
/**
89+
* Test app info parser
90+
*/
91+
#[\PHPUnit\Framework\Attributes\DataProvider('appDataProvider')]
92+
public function testApplyL10NNoLanguage(array $data, array $expected): void {
93+
$parser = new InfoParser();
94+
$this->assertSame($expected, $parser->applyL10N($data));
95+
}
96+
97+
public function testApplyL10N(): void {
98+
$parser = new InfoParser();
99+
$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-multi-lang.xml');
100+
$this->assertEquals('English', $parser->applyL10N($data, 'en')['description']);
101+
$this->assertEquals('German', $parser->applyL10N($data, 'de')['description']);
102+
}
103+
104+
public function testApplyL10NSingleLanguage(): void {
105+
$parser = new InfoParser();
106+
$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-single-lang.xml');
107+
$this->assertEquals('English', $parser->applyL10N($data, 'en')['description']);
108+
}
56109
}

tests/lib/AppTest.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Test;
1010

1111
use OC\App\AppManager;
12-
use OC\App\InfoParser;
1312
use OC\AppConfig;
1413
use OC\Config\ConfigManager;
1514
use OCP\EventDispatcher\IEventDispatcher;
@@ -586,59 +585,4 @@ private function restoreAppConfig() {
586585
// Remove the cache of the mocked apps list with a forceRefresh
587586
\OC_App::getEnabledApps();
588587
}
589-
590-
/**
591-
* Providers for the app data values
592-
*/
593-
public static function appDataProvider(): array {
594-
return [
595-
[
596-
['description' => " \t This is a multiline \n test with \n \t \n \n some new lines "],
597-
['description' => "This is a multiline \n test with \n \t \n \n some new lines"],
598-
],
599-
[
600-
['description' => " \t This is a multiline \n test with \n \t some new lines "],
601-
['description' => "This is a multiline \n test with \n \t some new lines"],
602-
],
603-
[
604-
['description' => hex2bin('5065726d657420646520732761757468656e7469666965722064616e732070697769676f20646972656374656d656e74206176656320736573206964656e74696669616e7473206f776e636c6f75642073616e73206c65732072657461706572206574206d657420c3a0206a6f757273206365757820636920656e20636173206465206368616e67656d656e74206465206d6f742064652070617373652e0d0a0d')],
605-
['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."],
606-
],
607-
[
608-
['not-a-description' => " \t This is a multiline \n test with \n \t some new lines "],
609-
[
610-
'not-a-description' => " \t This is a multiline \n test with \n \t some new lines ",
611-
'description' => '',
612-
],
613-
],
614-
[
615-
['description' => [100, 'bla']],
616-
['description' => ''],
617-
],
618-
];
619-
}
620-
621-
/**
622-
* Test app info parser
623-
*
624-
* @param array $data
625-
* @param array $expected
626-
*/
627-
#[\PHPUnit\Framework\Attributes\DataProvider('appDataProvider')]
628-
public function testParseAppInfo(array $data, array $expected): void {
629-
$this->assertSame($expected, \OC_App::parseAppInfo($data));
630-
}
631-
632-
public function testParseAppInfoL10N(): void {
633-
$parser = new InfoParser();
634-
$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-multi-lang.xml');
635-
$this->assertEquals('English', \OC_App::parseAppInfo($data, 'en')['description']);
636-
$this->assertEquals('German', \OC_App::parseAppInfo($data, 'de')['description']);
637-
}
638-
639-
public function testParseAppInfoL10NSingleLanguage(): void {
640-
$parser = new InfoParser();
641-
$data = $parser->parse(\OC::$SERVERROOT . '/tests/data/app/description-single-lang.xml');
642-
$this->assertEquals('English', \OC_App::parseAppInfo($data, 'en')['description']);
643-
}
644588
}

0 commit comments

Comments
 (0)