FactoryTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright (c) 2016 Joas Schilling <nickvergessen@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace Test\L10N;
  10. use OC\L10N\Factory;
  11. use OC\L10N\LanguageNotFoundException;
  12. use OCP\App\AppPathNotFoundException;
  13. use OCP\App\IAppManager;
  14. use OCP\ICacheFactory;
  15. use OCP\IConfig;
  16. use OCP\IRequest;
  17. use OCP\IUser;
  18. use OCP\IUserSession;
  19. use OCP\L10N\ILanguageIterator;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Test\TestCase;
  22. class FactoryTest extends TestCase {
  23. /** @var IConfig|MockObject */
  24. protected $config;
  25. /** @var IRequest|MockObject */
  26. protected $request;
  27. /** @var IUserSession|MockObject */
  28. protected $userSession;
  29. /** @var ICacheFactory|MockObject */
  30. protected $cacheFactory;
  31. /** @var string */
  32. protected $serverRoot;
  33. /** @var IAppManager|MockObject */
  34. protected IAppManager $appManager;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->config = $this->createMock(IConfig::class);
  38. $this->request = $this->createMock(IRequest::class);
  39. $this->userSession = $this->createMock(IUserSession::class);
  40. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  41. $this->appManager = $this->createMock(IAppManager::class);
  42. $this->serverRoot = \OC::$SERVERROOT;
  43. $this->config
  44. ->method('getSystemValueBool')
  45. ->willReturnMap([
  46. ['installed', false, true],
  47. ]);
  48. }
  49. /**
  50. * @param string[] $methods
  51. * @param bool $mockRequestGetHeaderMethod
  52. *
  53. * @return Factory|MockObject
  54. */
  55. protected function getFactory(array $methods = [], $mockRequestGetHeaderMethod = false) {
  56. if ($mockRequestGetHeaderMethod) {
  57. $this->request->expects(self::any())
  58. ->method('getHeader')
  59. ->willReturn('');
  60. }
  61. if (!empty($methods)) {
  62. return $this->getMockBuilder(Factory::class)
  63. ->setConstructorArgs([
  64. $this->config,
  65. $this->request,
  66. $this->userSession,
  67. $this->cacheFactory,
  68. $this->serverRoot,
  69. $this->appManager,
  70. ])
  71. ->setMethods($methods)
  72. ->getMock();
  73. }
  74. return new Factory($this->config, $this->request, $this->userSession, $this->cacheFactory, $this->serverRoot, $this->appManager);
  75. }
  76. public function dataFindAvailableLanguages(): array {
  77. return [
  78. [null],
  79. ['files'],
  80. ];
  81. }
  82. public function testFindLanguageWithExistingRequestLanguageAndNoApp(): void {
  83. $factory = $this->getFactory(['languageExists']);
  84. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  85. $factory->expects(self::once())
  86. ->method('languageExists')
  87. ->with(null, 'de')
  88. ->willReturn(true);
  89. self::assertSame('de', $factory->findLanguage());
  90. }
  91. public function testFindLanguageWithExistingRequestLanguageAndApp(): void {
  92. $factory = $this->getFactory(['languageExists']);
  93. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  94. $factory->expects(self::once())
  95. ->method('languageExists')
  96. ->with('MyApp', 'de')
  97. ->willReturn(true);
  98. self::assertSame('de', $factory->findLanguage('MyApp'));
  99. }
  100. public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredUserLanguage(): void {
  101. $factory = $this->getFactory(['languageExists']);
  102. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  103. $factory->expects($this->exactly(2))
  104. ->method('languageExists')
  105. ->withConsecutive(
  106. ['MyApp', 'de'],
  107. ['MyApp', 'jp'],
  108. )
  109. ->willReturnOnConsecutiveCalls(
  110. false,
  111. true,
  112. );
  113. $this->config
  114. ->expects($this->exactly(1))
  115. ->method('getSystemValue')
  116. ->withConsecutive(
  117. ['force_language', false],
  118. )->willReturnOnConsecutiveCalls(
  119. false,
  120. );
  121. $user = $this->getMockBuilder(IUser::class)
  122. ->getMock();
  123. $user->expects(self::once())
  124. ->method('getUID')
  125. ->willReturn('MyUserUid');
  126. $this->userSession
  127. ->expects(self::exactly(2))
  128. ->method('getUser')
  129. ->willReturn($user);
  130. $this->config
  131. ->expects(self::once())
  132. ->method('getUserValue')
  133. ->with('MyUserUid', 'core', 'lang', null)
  134. ->willReturn('jp');
  135. self::assertSame('jp', $factory->findLanguage('MyApp'));
  136. }
  137. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguage(): void {
  138. $factory = $this->getFactory(['languageExists'], true);
  139. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  140. $factory->expects($this->exactly(3))
  141. ->method('languageExists')
  142. ->willReturnMap([
  143. ['MyApp', 'de', false],
  144. ['MyApp', 'jp', false],
  145. ['MyApp', 'es', true],
  146. ]);
  147. $this->config
  148. ->expects($this->exactly(2))
  149. ->method('getSystemValue')
  150. ->willReturnMap([
  151. ['force_language', false, false],
  152. ['default_language', false, 'es']
  153. ]);
  154. $user = $this->getMockBuilder(IUser::class)
  155. ->getMock();
  156. $user->expects(self::once())
  157. ->method('getUID')
  158. ->willReturn('MyUserUid');
  159. $this->userSession
  160. ->expects(self::exactly(2))
  161. ->method('getUser')
  162. ->willReturn($user);
  163. $this->config
  164. ->expects(self::once())
  165. ->method('getUserValue')
  166. ->with('MyUserUid', 'core', 'lang', null)
  167. ->willReturn('jp');
  168. self::assertSame('es', $factory->findLanguage('MyApp'));
  169. }
  170. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefault(): void {
  171. $factory = $this->getFactory(['languageExists'], true);
  172. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  173. $factory->expects($this->exactly(3))
  174. ->method('languageExists')
  175. ->willReturnMap([
  176. ['MyApp', 'de', false],
  177. ['MyApp', 'jp', false],
  178. ['MyApp', 'es', false],
  179. ]);
  180. $this->config
  181. ->expects($this->exactly(2))
  182. ->method('getSystemValue')
  183. ->willReturnMap([
  184. ['force_language', false, false],
  185. ['default_language', false, 'es']
  186. ]);
  187. $user = $this->getMockBuilder(IUser::class)
  188. ->getMock();
  189. $user->expects(self::once())
  190. ->method('getUID')
  191. ->willReturn('MyUserUid');
  192. $this->userSession
  193. ->expects(self::exactly(2))
  194. ->method('getUser')
  195. ->willReturn($user);
  196. $this->config
  197. ->expects(self::once())
  198. ->method('getUserValue')
  199. ->with('MyUserUid', 'core', 'lang', null)
  200. ->willReturn('jp');
  201. $this->config
  202. ->expects(self::never())
  203. ->method('setUserValue');
  204. self::assertSame('en', $factory->findLanguage('MyApp'));
  205. }
  206. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope(): void {
  207. $factory = $this->getFactory(['languageExists'], true);
  208. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  209. $factory->expects($this->exactly(3))
  210. ->method('languageExists')
  211. ->willReturnMap([
  212. ['MyApp', 'de', false],
  213. ['MyApp', 'jp', false],
  214. ['MyApp', 'es', false],
  215. ]);
  216. $this->config
  217. ->expects($this->exactly(2))
  218. ->method('getSystemValue')
  219. ->willReturnMap([
  220. ['force_language', false, false],
  221. ['default_language', false, 'es']
  222. ]);
  223. $user = $this->getMockBuilder(IUser::class)
  224. ->getMock();
  225. $user->expects(self::once())
  226. ->method('getUID')
  227. ->willReturn('MyUserUid');
  228. $this->userSession
  229. ->expects(self::exactly(2))
  230. ->method('getUser')
  231. ->willReturn($user);
  232. $this->config
  233. ->expects(self::once())
  234. ->method('getUserValue')
  235. ->with('MyUserUid', 'core', 'lang', null)
  236. ->willReturn('jp');
  237. $this->config
  238. ->expects(self::never())
  239. ->method('setUserValue')
  240. ->with('MyUserUid', 'core', 'lang', 'en');
  241. self::assertSame('en', $factory->findLanguage('MyApp'));
  242. }
  243. public function testFindLanguageWithForcedLanguage(): void {
  244. $factory = $this->getFactory(['languageExists']);
  245. $this->config
  246. ->expects($this->once())
  247. ->method('getSystemValue')
  248. ->with('force_language', false)
  249. ->willReturn('de');
  250. $factory->expects($this->once())
  251. ->method('languageExists')
  252. ->with('MyApp', 'de')
  253. ->willReturn(true);
  254. self::assertSame('de', $factory->findLanguage('MyApp'));
  255. }
  256. /**
  257. * @dataProvider dataFindAvailableLanguages
  258. *
  259. * @param string|null $app
  260. */
  261. public function testFindAvailableLanguages($app): void {
  262. $factory = $this->getFactory(['findL10nDir']);
  263. $factory->expects(self::once())
  264. ->method('findL10nDir')
  265. ->with($app)
  266. ->willReturn(\OC::$SERVERROOT . '/tests/data/l10n/');
  267. self::assertEqualsCanonicalizing(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app));
  268. }
  269. public function dataLanguageExists(): array {
  270. return [
  271. [null, 'en', [], true],
  272. [null, 'de', [], false],
  273. [null, 'de', ['ru'], false],
  274. [null, 'de', ['ru', 'de'], true],
  275. ['files', 'en', [], true],
  276. ['files', 'de', [], false],
  277. ['files', 'de', ['ru'], false],
  278. ['files', 'de', ['de', 'ru'], true],
  279. ];
  280. }
  281. public function testFindAvailableLanguagesWithThemes(): void {
  282. $this->serverRoot .= '/tests/data';
  283. $app = 'files';
  284. $factory = $this->getFactory(['findL10nDir']);
  285. $factory->expects(self::once())
  286. ->method('findL10nDir')
  287. ->with($app)
  288. ->willReturn($this->serverRoot . '/apps/files/l10n/');
  289. $this->config
  290. ->expects(self::once())
  291. ->method('getSystemValueString')
  292. ->with('theme')
  293. ->willReturn('abc');
  294. self::assertEqualsCanonicalizing(['en', 'zz'], $factory->findAvailableLanguages($app));
  295. }
  296. /**
  297. * @dataProvider dataLanguageExists
  298. *
  299. * @param string|null $app
  300. * @param string $lang
  301. * @param string[] $availableLanguages
  302. * @param string $expected
  303. */
  304. public function testLanguageExists($app, $lang, array $availableLanguages, $expected): void {
  305. $factory = $this->getFactory(['findAvailableLanguages']);
  306. $factory->expects(($lang === 'en') ? self::never() : self::once())
  307. ->method('findAvailableLanguages')
  308. ->with($app)
  309. ->willReturn($availableLanguages);
  310. self::assertSame($expected, $factory->languageExists($app, $lang));
  311. }
  312. public function dataSetLanguageFromRequest(): array {
  313. return [
  314. // Language is available
  315. [null, 'de', ['de'], 'de'],
  316. [null, 'de,en', ['de'], 'de'],
  317. [null, 'de-DE,en-US;q=0.8,en;q=0.6', ['de'], 'de'],
  318. // Language is not available
  319. [null, 'de', ['ru'], new LanguageNotFoundException()],
  320. [null, 'de,en', ['ru', 'en'], 'en'],
  321. [null, 'de-DE,en-US;q=0.8,en;q=0.6', ['ru', 'en'], 'en'],
  322. // Language for app
  323. ['files_pdfviewer', 'de', ['de'], 'de'],
  324. ['files_pdfviewer', 'de,en', ['de'], 'de'],
  325. ['files_pdfviewer', 'de-DE,en-US;q=0.8,en;q=0.6', ['de'], 'de'],
  326. // Language for app is not available
  327. ['files_pdfviewer', 'de', ['ru'], new LanguageNotFoundException()],
  328. ['files_pdfviewer', 'de,en', ['ru', 'en'], 'en'],
  329. ['files_pdfviewer', 'de-DE,en-US;q=0.8,en;q=0.6', ['ru', 'en'], 'en'],
  330. ];
  331. }
  332. /**
  333. * @dataProvider dataSetLanguageFromRequest
  334. *
  335. * @param string|null $app
  336. * @param string $header
  337. * @param string[] $availableLanguages
  338. * @param string $expected
  339. */
  340. public function testGetLanguageFromRequest($app, $header, array $availableLanguages, $expected): void {
  341. $factory = $this->getFactory(['findAvailableLanguages', 'respectDefaultLanguage']);
  342. $factory->expects(self::once())
  343. ->method('findAvailableLanguages')
  344. ->with($app)
  345. ->willReturn($availableLanguages);
  346. $factory->expects(self::any())
  347. ->method('respectDefaultLanguage')->willReturnCallback(function ($app, $lang) {
  348. return $lang;
  349. });
  350. $this->request->expects(self::once())
  351. ->method('getHeader')
  352. ->with('ACCEPT_LANGUAGE')
  353. ->willReturn($header);
  354. if ($expected instanceof LanguageNotFoundException) {
  355. $this->expectException(LanguageNotFoundException::class);
  356. self::invokePrivate($factory, 'getLanguageFromRequest', [$app]);
  357. } else {
  358. self::assertSame($expected, self::invokePrivate($factory, 'getLanguageFromRequest', [$app]), 'Asserting returned language');
  359. }
  360. }
  361. public function dataGetL10nFilesForApp(): array {
  362. return [
  363. ['', 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']],
  364. ['core', 'ru', [\OC::$SERVERROOT . '/core/l10n/ru.json']],
  365. ['lib', 'ru', [\OC::$SERVERROOT . '/lib/l10n/ru.json']],
  366. ['settings', 'de', [\OC::$SERVERROOT . '/apps/settings/l10n/de.json']],
  367. ['files', 'de', [\OC::$SERVERROOT . '/apps/files/l10n/de.json']],
  368. ['files', '_lang_never_exists_', []],
  369. ['_app_never_exists_', 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']],
  370. ];
  371. }
  372. /**
  373. * @dataProvider dataGetL10nFilesForApp
  374. *
  375. * @param string $app
  376. * @param string $expected
  377. */
  378. public function testGetL10nFilesForApp($app, $lang, $expected): void {
  379. $factory = $this->getFactory();
  380. if (in_array($app, ['settings','files'])) {
  381. $this->appManager
  382. ->method('getAppPath')
  383. ->with($app)
  384. ->willReturn(\OC::$SERVERROOT . '/apps/' . $app);
  385. } else {
  386. $this->appManager
  387. ->method('getAppPath')
  388. ->with($app)
  389. ->willThrowException(new AppPathNotFoundException());
  390. }
  391. self::assertSame($expected, $this->invokePrivate($factory, 'getL10nFilesForApp', [$app, $lang]));
  392. }
  393. public function dataFindL10NDir(): array {
  394. return [
  395. ['', \OC::$SERVERROOT . '/core/l10n/'],
  396. ['core', \OC::$SERVERROOT . '/core/l10n/'],
  397. ['lib', \OC::$SERVERROOT . '/lib/l10n/'],
  398. ['settings', \OC::$SERVERROOT . '/apps/settings/l10n/'],
  399. ['files', \OC::$SERVERROOT . '/apps/files/l10n/'],
  400. ['_app_never_exists_', \OC::$SERVERROOT . '/core/l10n/'],
  401. ];
  402. }
  403. /**
  404. * @dataProvider dataFindL10NDir
  405. *
  406. * @param string $app
  407. * @param string $expected
  408. */
  409. public function testFindL10NDir($app, $expected): void {
  410. $factory = $this->getFactory();
  411. if (in_array($app, ['settings','files'])) {
  412. $this->appManager
  413. ->method('getAppPath')
  414. ->with($app)
  415. ->willReturn(\OC::$SERVERROOT . '/apps/' . $app);
  416. } else {
  417. $this->appManager
  418. ->method('getAppPath')
  419. ->with($app)
  420. ->willThrowException(new AppPathNotFoundException());
  421. }
  422. self::assertSame($expected, $this->invokePrivate($factory, 'findL10nDir', [$app]));
  423. }
  424. public function dataFindLanguage(): array {
  425. return [
  426. // Not logged in
  427. [false, [], 'en'],
  428. [false, ['fr'], 'fr'],
  429. [false, ['de', 'fr'], 'de'],
  430. [false, ['nl', 'de', 'fr'], 'de'],
  431. [true, [], 'en'],
  432. [true, ['fr'], 'fr'],
  433. [true, ['de', 'fr'], 'de'],
  434. [true, ['nl', 'de', 'fr'], 'nl'],
  435. ];
  436. }
  437. /**
  438. * @dataProvider dataFindLanguage
  439. *
  440. * @param bool $loggedIn
  441. * @param array $availableLang
  442. * @param string $expected
  443. */
  444. public function testFindLanguage($loggedIn, $availableLang, $expected): void {
  445. $userLang = 'nl';
  446. $browserLang = 'de';
  447. $defaultLang = 'fr';
  448. $this->config->expects(self::any())
  449. ->method('getSystemValue')
  450. ->willReturnCallback(function ($var, $default) use ($defaultLang) {
  451. if ($var === 'default_language') {
  452. return $defaultLang;
  453. } else {
  454. return $default;
  455. }
  456. });
  457. if ($loggedIn) {
  458. $user = $this->getMockBuilder(IUser::class)
  459. ->getMock();
  460. $user->expects(self::any())
  461. ->method('getUID')
  462. ->willReturn('MyUserUid');
  463. $this->userSession
  464. ->expects(self::any())
  465. ->method('getUser')
  466. ->willReturn($user);
  467. $this->config->expects(self::any())
  468. ->method('getUserValue')
  469. ->with('MyUserUid', 'core', 'lang', null)
  470. ->willReturn($userLang);
  471. } else {
  472. $this->userSession
  473. ->expects(self::any())
  474. ->method('getUser')
  475. ->willReturn(null);
  476. }
  477. $this->request->expects(self::any())
  478. ->method('getHeader')
  479. ->with($this->equalTo('ACCEPT_LANGUAGE'))
  480. ->willReturn($browserLang);
  481. $factory = $this->getFactory(['languageExists', 'findAvailableLanguages', 'respectDefaultLanguage']);
  482. $factory->expects(self::any())
  483. ->method('languageExists')
  484. ->willReturnCallback(function ($app, $lang) use ($availableLang) {
  485. return in_array($lang, $availableLang);
  486. });
  487. $factory->expects(self::any())
  488. ->method('findAvailableLanguages')
  489. ->willReturnCallback(function ($app) use ($availableLang) {
  490. return $availableLang;
  491. });
  492. $factory->expects(self::any())
  493. ->method('respectDefaultLanguage')->willReturnCallback(function ($app, $lang) {
  494. return $lang;
  495. });
  496. $lang = $factory->findLanguage();
  497. self::assertSame($expected, $lang);
  498. }
  499. public function testFindGenericLanguageByEnforcedLanguage(): void {
  500. $factory = $this->getFactory();
  501. $this->config->expects(self::once())
  502. ->method('getSystemValue')
  503. ->with('force_language', false)
  504. ->willReturn('cz');
  505. $lang = $factory->findGenericLanguage();
  506. self::assertSame('cz', $lang);
  507. }
  508. public function testFindGenericLanguageByDefaultLanguage(): void {
  509. $factory = $this->getFactory(['languageExists']);
  510. $this->config->expects(self::exactly(2))
  511. ->method('getSystemValue')
  512. ->willReturnMap([
  513. ['force_language', false, false,],
  514. ['default_language', false, 'cz',],
  515. ]);
  516. $factory->expects(self::once())
  517. ->method('languageExists')
  518. ->with(null, 'cz')
  519. ->willReturn(true);
  520. $lang = $factory->findGenericLanguage();
  521. self::assertSame('cz', $lang);
  522. }
  523. public function testFindGenericLanguageByUserLanguage(): void {
  524. $factory = $this->getFactory();
  525. $this->config->expects(self::exactly(2))
  526. ->method('getSystemValue')
  527. ->willReturnMap([
  528. ['force_language', false, false,],
  529. ['default_language', false, false,],
  530. ]);
  531. $user = $this->createMock(IUser::class);
  532. $this->userSession->expects(self::once())
  533. ->method('getUser')
  534. ->willReturn($user);
  535. $user->method('getUID')->willReturn('user123');
  536. $this->config->expects(self::once())
  537. ->method('getUserValue')
  538. ->with('user123', 'core', 'lang', null)
  539. ->willReturn('cz');
  540. $lang = $factory->findGenericLanguage();
  541. self::assertSame('cz', $lang);
  542. }
  543. public function testFindGenericLanguageByRequestLanguage(): void {
  544. $factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
  545. $this->config->method('getSystemValue')
  546. ->willReturnMap([
  547. ['force_language', false, false,],
  548. ['default_language', false, false,],
  549. ]);
  550. $user = $this->createMock(IUser::class);
  551. $this->userSession->expects(self::once())
  552. ->method('getUser')
  553. ->willReturn($user);
  554. $user->method('getUID')->willReturn('user123');
  555. $this->config->expects(self::once())
  556. ->method('getUserValue')
  557. ->with('user123', 'core', 'lang', null)
  558. ->willReturn(null);
  559. $this->request->expects(self::once())
  560. ->method('getHeader')
  561. ->with('ACCEPT_LANGUAGE')
  562. ->willReturn('cz');
  563. $factory->expects(self::once())
  564. ->method('findAvailableLanguages')
  565. ->with(null)
  566. ->willReturn(['cz']);
  567. $lang = $factory->findGenericLanguage();
  568. self::assertSame('cz', $lang);
  569. }
  570. public function testFindGenericLanguageFallback(): void {
  571. $factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
  572. $this->config->method('getSystemValue')
  573. ->willReturnMap([
  574. ['force_language', false, false,],
  575. ['default_language', false, false,],
  576. ]);
  577. $user = $this->createMock(IUser::class);
  578. $this->userSession->expects(self::once())
  579. ->method('getUser')
  580. ->willReturn($user);
  581. $user->method('getUID')->willReturn('user123');
  582. $this->config->expects(self::once())
  583. ->method('getUserValue')
  584. ->with('user123', 'core', 'lang', null)
  585. ->willReturn(null);
  586. $this->request->expects(self::once())
  587. ->method('getHeader')
  588. ->with('ACCEPT_LANGUAGE')
  589. ->willReturn('');
  590. $factory->expects(self::never())
  591. ->method('findAvailableLanguages');
  592. $factory->expects(self::never())
  593. ->method('languageExists');
  594. $lang = $factory->findGenericLanguage();
  595. self::assertSame('en', $lang);
  596. }
  597. public function dataTestRespectDefaultLanguage(): array {
  598. return [
  599. ['de', 'de_DE', true, 'de_DE'],
  600. ['de', 'de', true, 'de'],
  601. ['de', false, true, 'de'],
  602. ['fr', 'de_DE', true, 'fr'],
  603. ];
  604. }
  605. /**
  606. * test if we respect default language if possible
  607. *
  608. * @dataProvider dataTestRespectDefaultLanguage
  609. *
  610. * @param string $lang
  611. * @param string $defaultLanguage
  612. * @param bool $langExists
  613. * @param string $expected
  614. */
  615. public function testRespectDefaultLanguage($lang, $defaultLanguage, $langExists, $expected): void {
  616. $factory = $this->getFactory(['languageExists']);
  617. $factory->expects(self::any())
  618. ->method('languageExists')->willReturn($langExists);
  619. $this->config->expects(self::any())
  620. ->method('getSystemValue')->with('default_language', false)->willReturn($defaultLanguage);
  621. $result = $this->invokePrivate($factory, 'respectDefaultLanguage', ['app', $lang]);
  622. self::assertSame($expected, $result);
  623. }
  624. public function languageIteratorRequestProvider():array {
  625. return [
  626. [ true, $this->createMock(IUser::class)],
  627. [ false, $this->createMock(IUser::class)],
  628. [ false, null]
  629. ];
  630. }
  631. /**
  632. * @dataProvider languageIteratorRequestProvider
  633. */
  634. public function testGetLanguageIterator(bool $hasSession, ?IUser $iUserMock = null): void {
  635. $factory = $this->getFactory();
  636. if ($iUserMock === null) {
  637. $matcher = $this->userSession->expects(self::once())
  638. ->method('getUser');
  639. if ($hasSession) {
  640. $matcher->willReturn($this->createMock(IUser::class));
  641. } else {
  642. $this->expectException(\RuntimeException::class);
  643. }
  644. }
  645. $iterator = $factory->getLanguageIterator($iUserMock);
  646. self::assertInstanceOf(ILanguageIterator::class, $iterator);
  647. }
  648. }