PersonalControllerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Settings\Controller;
  24. use OC\Settings\Controller\PersonalController;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\IRequest;
  30. use OCP\L10N\IFactory;
  31. class PersonalControllerTest extends \Test\TestCase {
  32. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  33. private $l10nFactory;
  34. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  35. private $config;
  36. /** @var PersonalController */
  37. private $controller;
  38. /** @var IL10N */
  39. private $l;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->l10nFactory = $this->createMock(IFactory::class);
  43. $this->config = $this->createMock(IConfig::class);
  44. $this->l = $this->createMock(IL10N::class);
  45. $this->l->method('t')
  46. ->will($this->returnCallback(function ($text, $parameters = []) {
  47. return vsprintf($text, $parameters);
  48. }));
  49. $this->controller = new PersonalController(
  50. 'settings',
  51. $this->createMock(IRequest::class),
  52. $this->l10nFactory,
  53. 'user',
  54. $this->config,
  55. $this->l
  56. );
  57. }
  58. public function testSetLanguage() {
  59. $this->l10nFactory->method('findAvailableLanguages')
  60. ->willReturn(['aa', 'bb', 'cc']);
  61. $this->config->expects($this->once())
  62. ->method('setUserValue')
  63. ->with(
  64. $this->equalTo('user'),
  65. $this->equalTo('core'),
  66. $this->equalTo('lang'),
  67. $this->equalTo('bb')
  68. );
  69. $resp = $this->controller->setLanguage('bb');
  70. $expected = new JSONResponse([]);
  71. $this->assertEquals($expected, $resp);
  72. }
  73. public function testSetLanguageEn() {
  74. $this->l10nFactory->method('findAvailableLanguages')
  75. ->willReturn(['aa', 'bb', 'cc']);
  76. $this->config->expects($this->once())
  77. ->method('setUserValue')
  78. ->with(
  79. $this->equalTo('user'),
  80. $this->equalTo('core'),
  81. $this->equalTo('lang'),
  82. $this->equalTo('en')
  83. );
  84. $resp = $this->controller->setLanguage('en');
  85. $expected = new JSONResponse([]);
  86. $this->assertEquals($expected, $resp);
  87. }
  88. public function testSetLanguageFails() {
  89. $this->l10nFactory->method('findAvailableLanguages')
  90. ->willReturn(['aa', 'bb', 'cc']);
  91. $this->config->expects($this->never())
  92. ->method('setUserValue');
  93. $resp = $this->controller->setLanguage('dd');
  94. $expected = new JSONResponse(['message' => 'Invalid request'], Http::STATUS_BAD_REQUEST);
  95. $this->assertEquals($expected, $resp);
  96. }
  97. public function testSetLanguageEmpty() {
  98. $this->l10nFactory->method('findAvailableLanguages')
  99. ->willReturn(['aa', 'bb', 'cc']);
  100. $this->config->expects($this->never())
  101. ->method('setUserValue');
  102. $resp = $this->controller->setLanguage('');
  103. $expected = new JSONResponse(['message' => 'Invalid request'], Http::STATUS_BAD_REQUEST);
  104. $this->assertEquals($expected, $resp);
  105. }
  106. }