UserPanelTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Controller;
  20. use App\Core\DB\DB;
  21. use App\Util\GNUsocialTestCase;
  22. use Functional as F;
  23. use Jchook\AssertThrows\AssertThrows;
  24. class UserPanelTest extends GNUsocialTestCase
  25. {
  26. use AssertThrows;
  27. /**
  28. * @covers \App\Controller\UserPanel::allSettings
  29. * @covers \App\Controller\UserPanel::personalInfo
  30. */
  31. public function testPersonalInfo()
  32. {
  33. $client = static::createClient();
  34. $user = DB::findOneBy('local_user', ['nickname' => 'form_personal_info_test_user']);
  35. $client->loginUser($user);
  36. $client->request('GET', '/settings');
  37. $this->assertResponseIsSuccessful();
  38. $crawler = $client->submitForm('Save personal info', [
  39. 'save_personal_info[nickname]' => 'form_test_user_new_nickname',
  40. 'save_personal_info[full_name]' => 'Form User',
  41. 'save_personal_info[homepage]' => 'https://gnu.org',
  42. 'save_personal_info[bio]' => 'I was born at a very young age',
  43. 'save_personal_info[location]' => 'right here',
  44. 'save_personal_info[self_tags]' => 'foo bar',
  45. ]);
  46. $changed_user = DB::findOneBy('local_user', ['id' => $user->getId()]);
  47. $actor = $changed_user->getActor();
  48. static::assertSame($changed_user->getNickname(), 'form_test_user_new_nickname');
  49. static::assertSame($actor->getNickname(), 'form_test_user_new_nickname');
  50. static::assertSame($actor->getFullName(), 'Form User');
  51. static::assertSame($actor->getHomepage(), 'https://gnu.org');
  52. static::assertSame($actor->getBio(), 'I was born at a very young age');
  53. static::assertSame($actor->getLocation(), 'right here');
  54. $tags = F\map($actor->getSelfTags(), fn ($tag) => $tag->getTag());
  55. sort($tags);
  56. static::assertSame($tags, ['bar', 'foo']);
  57. }
  58. /**
  59. * @covers \App\Controller\UserPanel::account
  60. * @covers \App\Controller\UserPanel::allSettings
  61. */
  62. public function testAccount()
  63. {
  64. $client = static::createClient();
  65. $user = DB::findOneBy('local_user', ['nickname' => 'form_account_test_user']);
  66. $client->loginUser($user);
  67. $client->request('GET', '/settings');
  68. $this->assertResponseIsSuccessful();
  69. $crawler = $client->submitForm('Save account info', [
  70. 'save_account_info[outgoing_email]' => 'outgoing@provider',
  71. 'save_account_info[incoming_email]' => 'incoming@provider',
  72. 'save_account_info[old_password]' => 'some password',
  73. 'save_account_info[password][first]' => 'this is some test password',
  74. 'save_account_info[password][second]' => 'this is some test password',
  75. 'save_account_info[phone_number]' => '+351908555842', // from fakenumber.net
  76. ]);
  77. $changed_user = DB::findOneBy('local_user', ['id' => $user->getId()]);
  78. static::assertSame($changed_user->getOutgoingEmail(), 'outgoing@provider');
  79. static::assertSame($changed_user->getIncomingEmail(), 'incoming@provider');
  80. static::assertTrue($changed_user->checkPassword('this is some test password'));
  81. static::assertSame($changed_user->getPhoneNumber()->getNationalNumber(), '908555842');
  82. }
  83. /**
  84. * @covers \App\Controller\UserPanel::account
  85. * @covers \App\Controller\UserPanel::allSettings
  86. */
  87. public function testAccountWrongPassword()
  88. {
  89. $client = static::createClient();
  90. $user = DB::findOneBy('local_user', ['nickname' => 'form_account_test_user']);
  91. $client->loginUser($user);
  92. $client->request('GET', '/settings');
  93. $this->assertResponseIsSuccessful();
  94. $crawler = $client->submitForm('Save account info', [
  95. 'save_account_info[old_password]' => 'some wrong password',
  96. 'save_account_info[password][first]' => 'this is some test password',
  97. 'save_account_info[password][second]' => 'this is some test password',
  98. ]);
  99. $this->assertResponseStatusCodeSame(500); // 401 in future
  100. $this->assertSelectorTextContains('.stacktrace', 'AuthenticationException');
  101. }
  102. /**
  103. * @covers \App\Controller\UserPanel::allSettings
  104. * @covers \App\Controller\UserPanel::notifications
  105. */
  106. public function testNotifications()
  107. {
  108. $client = static::createClient();
  109. $user = DB::findOneBy('local_user', ['nickname' => 'form_account_test_user']);
  110. $client->loginUser($user);
  111. $client->request('GET', '/settings');
  112. $this->assertResponseIsSuccessful();
  113. $crawler = $client->submitForm('Save notification settings for Email', [
  114. 'save_email[activity_by_subscribed]' => false,
  115. 'save_email[mention]' => true,
  116. 'save_email[reply]' => false,
  117. 'save_email[subscription]' => true,
  118. 'save_email[favorite]' => false,
  119. 'save_email[nudge]' => true,
  120. 'save_email[dm]' => false,
  121. 'save_email[enable_posting]' => true,
  122. ]);
  123. $settings = DB::findOneBy('user_notification_prefs', ['user_id' => $user->getId(), 'transport' => 'email']);
  124. static::assertSame($settings->getActivityBySubscribed(), false);
  125. static::assertSame($settings->getMention(), true);
  126. static::assertSame($settings->getReply(), false);
  127. static::assertSame($settings->getSubscription(), true);
  128. static::assertSame($settings->getFavorite(), false);
  129. static::assertSame($settings->getNudge(), true);
  130. static::assertSame($settings->getDm(), false);
  131. static::assertSame($settings->getEnablePosting(), true);
  132. }
  133. }