LanguageIteratorTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\L10N;
  7. use OC\L10N\LanguageIterator;
  8. use OCP\IConfig;
  9. use OCP\IUser;
  10. use Test\TestCase;
  11. class LanguageIteratorTest extends TestCase {
  12. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  13. protected $user;
  14. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  15. protected $config;
  16. /** @var LanguageIterator */
  17. protected $iterator;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->user = $this->createMock(IUser::class);
  21. $this->config = $this->createMock(IConfig::class);
  22. $this->iterator = new LanguageIterator($this->user, $this->config);
  23. }
  24. public function languageSettingsProvider() {
  25. return [
  26. // all language settings set
  27. [ 'de_DE', 'es_CU', 'zh_TW', ['de_DE', 'de', 'es_CU', 'es', 'zh_TW', 'zh', 'en']],
  28. [ 'de', 'es', 'zh', ['de', 'es', 'zh', 'en']],
  29. [ 'en', 'en', 'en', ['en', 'en', 'en', 'en']],
  30. // one possible setting is missing each
  31. [ false, 'es_CU', 'zh_TW', ['es_CU', 'es', 'zh_TW', 'zh', 'en']],
  32. [ false, 'es', 'zh_TW', ['es', 'zh_TW', 'zh', 'en']],
  33. [ false, 'es_CU', 'zh', ['es_CU', 'es', 'zh', 'en']],
  34. [ 'de_DE', null, 'zh_TW', ['de_DE', 'de', 'zh_TW', 'zh', 'en']],
  35. [ 'de_DE', null, 'zh', ['de_DE', 'de', 'zh', 'en']],
  36. [ 'de', null, 'zh_TW', ['de', 'zh_TW', 'zh', 'en']],
  37. [ 'de_DE', 'es_CU', 'en', ['de_DE', 'de', 'es_CU', 'es', 'en', 'en']],
  38. [ 'de', 'es_CU', 'en', ['de', 'es_CU', 'es', 'en', 'en']],
  39. [ 'de_DE', 'es', 'en', ['de_DE', 'de', 'es', 'en', 'en']],
  40. // two possible settings are missing each
  41. [ false, null, 'zh_TW', ['zh_TW', 'zh', 'en']],
  42. [ false, null, 'zh', ['zh', 'en']],
  43. [ false, 'es_CU', 'en', ['es_CU', 'es', 'en', 'en']],
  44. [ false, 'es', 'en', ['es', 'en', 'en']],
  45. [ 'de_DE', null, 'en', ['de_DE', 'de', 'en', 'en']],
  46. [ 'de', null, 'en', ['de', 'en', 'en']],
  47. // nothing is set
  48. [ false, null, 'en', ['en', 'en']],
  49. ];
  50. }
  51. /**
  52. * @dataProvider languageSettingsProvider
  53. */
  54. public function testIterator($forcedLang, $userLang, $sysLang, $expectedValues): void {
  55. $this->config->expects($this->any())
  56. ->method('getSystemValue')
  57. ->willReturnMap([
  58. ['force_language', false, $forcedLang],
  59. ]);
  60. $this->config->expects($this->any())
  61. ->method('getSystemValueString')
  62. ->willReturnMap([
  63. ['default_language', 'en', $sysLang],
  64. ]);
  65. $this->config->expects($this->any())
  66. ->method('getUserValue')
  67. ->willReturn($userLang);
  68. foreach ($expectedValues as $expected) {
  69. $this->assertTrue($this->iterator->valid());
  70. $this->assertSame($expected, $this->iterator->current());
  71. $this->iterator->next();
  72. }
  73. $this->assertFalse($this->iterator->valid());
  74. }
  75. }