1
0

LanguageIteratorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\L10N;
  24. use OC\L10N\LanguageIterator;
  25. use OCP\IConfig;
  26. use OCP\IUser;
  27. use Test\TestCase;
  28. class LanguageIteratorTest extends TestCase {
  29. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $user;
  31. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $config;
  33. /** @var LanguageIterator */
  34. protected $iterator;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->user = $this->createMock(IUser::class);
  38. $this->config = $this->createMock(IConfig::class);
  39. $this->iterator = new LanguageIterator($this->user, $this->config);
  40. }
  41. public function languageSettingsProvider() {
  42. return [
  43. // all language settings set
  44. [ 'de_DE', 'es_CU', 'zh_TW', ['de_DE', 'de', 'es_CU', 'es', 'zh_TW', 'zh', 'en']],
  45. [ 'de', 'es', 'zh', ['de', 'es', 'zh', 'en']],
  46. [ 'en', 'en', 'en', ['en', 'en', 'en', 'en']],
  47. // one possible setting is missing each
  48. [ false, 'es_CU', 'zh_TW', ['es_CU', 'es', 'zh_TW', 'zh', 'en']],
  49. [ false, 'es', 'zh_TW', ['es', 'zh_TW', 'zh', 'en']],
  50. [ false, 'es_CU', 'zh', ['es_CU', 'es', 'zh', 'en']],
  51. [ 'de_DE', null, 'zh_TW', ['de_DE', 'de', 'zh_TW', 'zh', 'en']],
  52. [ 'de_DE', null, 'zh', ['de_DE', 'de', 'zh', 'en']],
  53. [ 'de', null, 'zh_TW', ['de', 'zh_TW', 'zh', 'en']],
  54. [ 'de_DE', 'es_CU', 'en', ['de_DE', 'de', 'es_CU', 'es', 'en', 'en']],
  55. [ 'de', 'es_CU', 'en', ['de', 'es_CU', 'es', 'en', 'en']],
  56. [ 'de_DE', 'es', 'en', ['de_DE', 'de', 'es', 'en', 'en']],
  57. // two possible settings are missing each
  58. [ false, null, 'zh_TW', ['zh_TW', 'zh', 'en']],
  59. [ false, null, 'zh', ['zh', 'en']],
  60. [ false, 'es_CU', 'en', ['es_CU', 'es', 'en', 'en']],
  61. [ false, 'es', 'en', ['es', 'en', 'en']],
  62. [ 'de_DE', null, 'en', ['de_DE', 'de', 'en', 'en']],
  63. [ 'de', null, 'en', ['de', 'en', 'en']],
  64. // nothing is set
  65. [ false, null, 'en', ['en', 'en']],
  66. ];
  67. }
  68. /**
  69. * @dataProvider languageSettingsProvider
  70. */
  71. public function testIterator($forcedLang, $userLang, $sysLang, $expectedValues) {
  72. $this->config->expects($this->any())
  73. ->method('getSystemValue')
  74. ->willReturnMap([
  75. ['force_language', false, $forcedLang],
  76. ]);
  77. $this->config->expects($this->any())
  78. ->method('getSystemValueString')
  79. ->willReturnMap([
  80. ['default_language', 'en', $sysLang],
  81. ]);
  82. $this->config->expects($this->any())
  83. ->method('getUserValue')
  84. ->willReturn($userLang);
  85. foreach ($expectedValues as $expected) {
  86. $this->assertTrue($this->iterator->valid());
  87. $this->assertSame($expected, $this->iterator->current());
  88. $this->iterator->next();
  89. }
  90. $this->assertFalse($this->iterator->valid());
  91. }
  92. }