UserRightsTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('PUBLICDIR')) {
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. }
  23. if (!defined('GNUSOCIAL')) {
  24. define('GNUSOCIAL', true);
  25. }
  26. if (!defined('STATUSNET')) { // Compatibility
  27. define('STATUSNET', true);
  28. }
  29. use Exception;
  30. use PHPUnit\Framework\TestCase;
  31. use User;
  32. require_once INSTALLDIR . '/lib/util/common.php';
  33. final class UserRightsTest extends TestCase
  34. {
  35. protected $user;
  36. protected function setUp(): void
  37. {
  38. $user = User::getKV('nickname', 'userrightstestuser');
  39. if ($user) {
  40. // Leftover from a broken test run?
  41. $profile = $user->getProfile();
  42. $user->delete();
  43. $profile->delete();
  44. }
  45. $this->user = User::register(['nickname' => 'userrightstestuser']);
  46. if (!$this->user) {
  47. throw new Exception("Couldn't register userrightstestuser");
  48. }
  49. }
  50. protected function tearDown(): void
  51. {
  52. if ($this->user) {
  53. $profile = $this->user->getProfile();
  54. $this->user->delete();
  55. $profile->delete();
  56. }
  57. }
  58. public function testInvalidRole()
  59. {
  60. static::assertFalse($this->user->hasRole('invalidrole'));
  61. }
  62. public function standardRoles()
  63. {
  64. return [['admin'],
  65. ['moderator'],];
  66. }
  67. /**
  68. * @dataProvider standardRoles
  69. *
  70. * @param $role
  71. */
  72. public function testUngrantedRole($role)
  73. {
  74. static::assertFalse($this->user->hasRole($role));
  75. }
  76. /**
  77. * @dataProvider standardRoles
  78. *
  79. * @param $role
  80. */
  81. public function testGrantedRole($role)
  82. {
  83. $this->user->grantRole($role);
  84. static::assertTrue($this->user->hasRole($role));
  85. }
  86. }