1
0

FactoryTest.php 20 KB

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