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/common.php';
  33. final class UserRightsTest extends TestCase
  34. {
  35. protected $user = null;
  36. 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(array('nickname' => 'userrightstestuser'));
  46. if (!$this->user) {
  47. throw new Exception("Couldn't register userrightstestuser");
  48. }
  49. }
  50. function tearDown(): void
  51. {
  52. if ($this->user) {
  53. $profile = $this->user->getProfile();
  54. $this->user->delete();
  55. $profile->delete();
  56. }
  57. }
  58. function testInvalidRole()
  59. {
  60. $this->assertFalse($this->user->hasRole('invalidrole'));
  61. }
  62. function standardRoles()
  63. {
  64. return array(array('admin'),
  65. array('moderator'));
  66. }
  67. /**
  68. * @dataProvider standardRoles
  69. * @param $role
  70. */
  71. function testUngrantedRole($role)
  72. {
  73. $this->assertFalse($this->user->hasRole($role));
  74. }
  75. /**
  76. * @dataProvider standardRoles
  77. * @param $role
  78. */
  79. function testGrantedRole($role)
  80. {
  81. $this->user->grantRole($role);
  82. $this->assertTrue($this->user->hasRole($role));
  83. }
  84. }