1
0

FactoryTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. }
  39. /**
  40. * @param string[] $methods
  41. * @param bool $mockRequestGetHeaderMethod
  42. *
  43. * @return Factory|MockObject
  44. */
  45. protected function getFactory(array $methods = [], $mockRequestGetHeaderMethod = false) {
  46. if ($mockRequestGetHeaderMethod) {
  47. $this->request->expects(self::any())
  48. ->method('getHeader')
  49. ->willReturn('');
  50. }
  51. if (!empty($methods)) {
  52. return $this->getMockBuilder(Factory::class)
  53. ->setConstructorArgs([
  54. $this->config,
  55. $this->request,
  56. $this->userSession,
  57. $this->cacheFactory,
  58. $this->serverRoot,
  59. ])
  60. ->setMethods($methods)
  61. ->getMock();
  62. }
  63. return new Factory($this->config, $this->request, $this->userSession, $this->cacheFactory, $this->serverRoot);
  64. }
  65. public function dataFindAvailableLanguages(): array {
  66. return [
  67. [null],
  68. ['files'],
  69. ];
  70. }
  71. public function testFindLanguageWithExistingRequestLanguageAndNoApp(): void {
  72. $factory = $this->getFactory(['languageExists']);
  73. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  74. $factory->expects(self::once())
  75. ->method('languageExists')
  76. ->with(null, 'de')
  77. ->willReturn(true);
  78. self::assertSame('de', $factory->findLanguage());
  79. }
  80. public function testFindLanguageWithExistingRequestLanguageAndApp(): void {
  81. $factory = $this->getFactory(['languageExists']);
  82. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  83. $factory->expects(self::once())
  84. ->method('languageExists')
  85. ->with('MyApp', 'de')
  86. ->willReturn(true);
  87. self::assertSame('de', $factory->findLanguage('MyApp'));
  88. }
  89. public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredUserLanguage(): void {
  90. $factory = $this->getFactory(['languageExists']);
  91. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  92. $factory->expects($this->exactly(2))
  93. ->method('languageExists')
  94. ->withConsecutive(
  95. ['MyApp', 'de'],
  96. ['MyApp', 'jp'],
  97. )
  98. ->willReturnOnConsecutiveCalls(
  99. false,
  100. true,
  101. );
  102. $this->config
  103. ->expects($this->exactly(2))
  104. ->method('getSystemValue')
  105. ->withConsecutive(
  106. ['force_language', false],
  107. ['installed', false],
  108. )->willReturnOnConsecutiveCalls(
  109. false,
  110. true,
  111. );
  112. $user = $this->getMockBuilder(IUser::class)
  113. ->getMock();
  114. $user->expects(self::once())
  115. ->method('getUID')
  116. ->willReturn('MyUserUid');
  117. $this->userSession
  118. ->expects(self::exactly(2))
  119. ->method('getUser')
  120. ->willReturn($user);
  121. $this->config
  122. ->expects(self::once())
  123. ->method('getUserValue')
  124. ->with('MyUserUid', 'core', 'lang', null)
  125. ->willReturn('jp');
  126. self::assertSame('jp', $factory->findLanguage('MyApp'));
  127. }
  128. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguage(): void {
  129. $factory = $this->getFactory(['languageExists'], true);
  130. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  131. $factory->expects($this->exactly(3))
  132. ->method('languageExists')
  133. ->willReturnMap([
  134. ['MyApp', 'de', false],
  135. ['MyApp', 'jp', false],
  136. ['MyApp', 'es', true],
  137. ]);
  138. $this->config
  139. ->expects($this->exactly(3))
  140. ->method('getSystemValue')
  141. ->willReturnMap([
  142. ['force_language', false, false],
  143. ['installed', false, true],
  144. ['default_language', false, 'es']
  145. ]);
  146. $user = $this->getMockBuilder(IUser::class)
  147. ->getMock();
  148. $user->expects(self::once())
  149. ->method('getUID')
  150. ->willReturn('MyUserUid');
  151. $this->userSession
  152. ->expects(self::exactly(2))
  153. ->method('getUser')
  154. ->willReturn($user);
  155. $this->config
  156. ->expects(self::once())
  157. ->method('getUserValue')
  158. ->with('MyUserUid', 'core', 'lang', null)
  159. ->willReturn('jp');
  160. self::assertSame('es', $factory->findLanguage('MyApp'));
  161. }
  162. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefault(): void {
  163. $factory = $this->getFactory(['languageExists'], true);
  164. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  165. $factory->expects($this->exactly(3))
  166. ->method('languageExists')
  167. ->willReturnMap([
  168. ['MyApp', 'de', false],
  169. ['MyApp', 'jp', false],
  170. ['MyApp', 'es', false],
  171. ]);
  172. $this->config
  173. ->expects($this->exactly(3))
  174. ->method('getSystemValue')
  175. ->willReturnMap([
  176. ['force_language', false, false],
  177. ['installed', false, true],
  178. ['default_language', false, 'es']
  179. ]);
  180. $user = $this->getMockBuilder(IUser::class)
  181. ->getMock();
  182. $user->expects(self::once())
  183. ->method('getUID')
  184. ->willReturn('MyUserUid');
  185. $this->userSession
  186. ->expects(self::exactly(2))
  187. ->method('getUser')
  188. ->willReturn($user);
  189. $this->config
  190. ->expects(self::once())
  191. ->method('getUserValue')
  192. ->with('MyUserUid', 'core', 'lang', null)
  193. ->willReturn('jp');
  194. $this->config
  195. ->expects(self::never())
  196. ->method('setUserValue');
  197. self::assertSame('en', $factory->findLanguage('MyApp'));
  198. }
  199. public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope(): void {
  200. $factory = $this->getFactory(['languageExists'], true);
  201. $this->invokePrivate($factory, 'requestLanguage', ['de']);
  202. $factory->expects($this->exactly(3))
  203. ->method('languageExists')
  204. ->willReturnMap([
  205. ['MyApp', 'de', false],
  206. ['MyApp', 'jp', false],
  207. ['MyApp', 'es', false],
  208. ]);
  209. $this->config
  210. ->expects($this->exactly(3))
  211. ->method('getSystemValue')
  212. ->willReturnMap([
  213. ['force_language', false, false],
  214. ['installed', false, true],
  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('getSystemValue')
  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 === 'installed') {
  424. return true;
  425. } elseif ($var === 'default_language') {
  426. return $defaultLang;
  427. } else {
  428. return $default;
  429. }
  430. });
  431. if ($loggedIn) {
  432. $user = $this->getMockBuilder(IUser::class)
  433. ->getMock();
  434. $user->expects(self::any())
  435. ->method('getUID')
  436. ->willReturn('MyUserUid');
  437. $this->userSession
  438. ->expects(self::any())
  439. ->method('getUser')
  440. ->willReturn($user);
  441. $this->config->expects(self::any())
  442. ->method('getUserValue')
  443. ->with('MyUserUid', 'core', 'lang', null)
  444. ->willReturn($userLang);
  445. } else {
  446. $this->userSession
  447. ->expects(self::any())
  448. ->method('getUser')
  449. ->willReturn(null);
  450. }
  451. $this->request->expects(self::any())
  452. ->method('getHeader')
  453. ->with($this->equalTo('ACCEPT_LANGUAGE'))
  454. ->willReturn($browserLang);
  455. $factory = $this->getFactory(['languageExists', 'findAvailableLanguages', 'respectDefaultLanguage']);
  456. $factory->expects(self::any())
  457. ->method('languageExists')
  458. ->willReturnCallback(function ($app, $lang) use ($availableLang) {
  459. return in_array($lang, $availableLang);
  460. });
  461. $factory->expects(self::any())
  462. ->method('findAvailableLanguages')
  463. ->willReturnCallback(function ($app) use ($availableLang) {
  464. return $availableLang;
  465. });
  466. $factory->expects(self::any())
  467. ->method('respectDefaultLanguage')->willReturnCallback(function ($app, $lang) {
  468. return $lang;
  469. });
  470. $lang = $factory->findLanguage();
  471. self::assertSame($expected, $lang);
  472. }
  473. public function testFindGenericLanguageByEnforcedLanguage(): void {
  474. $factory = $this->getFactory();
  475. $this->config->expects(self::once())
  476. ->method('getSystemValue')
  477. ->with('force_language', false)
  478. ->willReturn('cz');
  479. $lang = $factory->findGenericLanguage();
  480. self::assertSame('cz', $lang);
  481. }
  482. public function testFindGenericLanguageByDefaultLanguage(): void {
  483. $factory = $this->getFactory(['languageExists']);
  484. $this->config->expects(self::exactly(2))
  485. ->method('getSystemValue')
  486. ->willReturnMap([
  487. ['force_language', false, false,],
  488. ['default_language', false, 'cz',],
  489. ]);
  490. $factory->expects(self::once())
  491. ->method('languageExists')
  492. ->with(null, 'cz')
  493. ->willReturn(true);
  494. $lang = $factory->findGenericLanguage();
  495. self::assertSame('cz', $lang);
  496. }
  497. public function testFindGenericLanguageByUserLanguage(): void {
  498. $factory = $this->getFactory();
  499. $this->config->expects(self::exactly(3))
  500. ->method('getSystemValue')
  501. ->willReturnMap([
  502. ['force_language', false, false,],
  503. ['default_language', false, false,],
  504. ['installed', false, true],
  505. ]);
  506. $user = $this->createMock(IUser::class);
  507. $this->userSession->expects(self::once())
  508. ->method('getUser')
  509. ->willReturn($user);
  510. $user->method('getUID')->willReturn('user123');
  511. $this->config->expects(self::once())
  512. ->method('getUserValue')
  513. ->with('user123', 'core', 'lang', null)
  514. ->willReturn('cz');
  515. $lang = $factory->findGenericLanguage();
  516. self::assertSame('cz', $lang);
  517. }
  518. public function testFindGenericLanguageByRequestLanguage(): void {
  519. $factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
  520. $this->config->method('getSystemValue')
  521. ->willReturnMap([
  522. ['force_language', false, false,],
  523. ['default_language', false, false,],
  524. ['installed', false, true],
  525. ]);
  526. $user = $this->createMock(IUser::class);
  527. $this->userSession->expects(self::once())
  528. ->method('getUser')
  529. ->willReturn($user);
  530. $user->method('getUID')->willReturn('user123');
  531. $this->config->expects(self::once())
  532. ->method('getUserValue')
  533. ->with('user123', 'core', 'lang', null)
  534. ->willReturn(null);
  535. $this->request->expects(self::once())
  536. ->method('getHeader')
  537. ->with('ACCEPT_LANGUAGE')
  538. ->willReturn('cz');
  539. $factory->expects(self::once())
  540. ->method('findAvailableLanguages')
  541. ->with(null)
  542. ->willReturn(['cz']);
  543. $lang = $factory->findGenericLanguage();
  544. self::assertSame('cz', $lang);
  545. }
  546. public function testFindGenericLanguageFallback(): void {
  547. $factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
  548. $this->config->method('getSystemValue')
  549. ->willReturnMap([
  550. ['force_language', false, false,],
  551. ['default_language', false, false,],
  552. ['installed', false, true],
  553. ]);
  554. $user = $this->createMock(IUser::class);
  555. $this->userSession->expects(self::once())
  556. ->method('getUser')
  557. ->willReturn($user);
  558. $user->method('getUID')->willReturn('user123');
  559. $this->config->expects(self::once())
  560. ->method('getUserValue')
  561. ->with('user123', 'core', 'lang', null)
  562. ->willReturn(null);
  563. $this->request->expects(self::once())
  564. ->method('getHeader')
  565. ->with('ACCEPT_LANGUAGE')
  566. ->willReturn('');
  567. $factory->expects(self::never())
  568. ->method('findAvailableLanguages');
  569. $factory->expects(self::never())
  570. ->method('languageExists');
  571. $lang = $factory->findGenericLanguage();
  572. self::assertSame('en', $lang);
  573. }
  574. public function dataTestRespectDefaultLanguage(): array {
  575. return [
  576. ['de', 'de_DE', true, 'de_DE'],
  577. ['de', 'de', true, 'de'],
  578. ['de', false, true, 'de'],
  579. ['fr', 'de_DE', true, 'fr'],
  580. ];
  581. }
  582. /**
  583. * test if we respect default language if possible
  584. *
  585. * @dataProvider dataTestRespectDefaultLanguage
  586. *
  587. * @param string $lang
  588. * @param string $defaultLanguage
  589. * @param bool $langExists
  590. * @param string $expected
  591. */
  592. public function testRespectDefaultLanguage($lang, $defaultLanguage, $langExists, $expected): void {
  593. $factory = $this->getFactory(['languageExists']);
  594. $factory->expects(self::any())
  595. ->method('languageExists')->willReturn($langExists);
  596. $this->config->expects(self::any())
  597. ->method('getSystemValue')->with('default_language', false)->willReturn($defaultLanguage);
  598. $result = $this->invokePrivate($factory, 'respectDefaultLanguage', ['app', $lang]);
  599. self::assertSame($expected, $result);
  600. }
  601. public function languageIteratorRequestProvider():array {
  602. return [
  603. [ true, $this->createMock(IUser::class)],
  604. [ false, $this->createMock(IUser::class)],
  605. [ false, null]
  606. ];
  607. }
  608. /**
  609. * @dataProvider languageIteratorRequestProvider
  610. */
  611. public function testGetLanguageIterator(bool $hasSession, IUser $iUserMock = null): void {
  612. $factory = $this->getFactory();
  613. if ($iUserMock === null) {
  614. $matcher = $this->userSession->expects(self::once())
  615. ->method('getUser');
  616. if ($hasSession) {
  617. $matcher->willReturn($this->createMock(IUser::class));
  618. } else {
  619. $this->expectException(\RuntimeException::class);
  620. }
  621. }
  622. $iterator = $factory->getLanguageIterator($iUserMock);
  623. self::assertInstanceOf(ILanguageIterator::class, $iterator);
  624. }
  625. }