UserRightsTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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('GNUSOCIAL')) {
  21. define('GNUSOCIAL', true);
  22. }
  23. if (!defined('STATUSNET')) { // Compatibility
  24. define('STATUSNET', true);
  25. }
  26. use Exception;
  27. use PHPUnit\Framework\TestCase;
  28. use User;
  29. require_once INSTALLDIR . '/lib/common.php';
  30. final class UserRightsTest extends TestCase
  31. {
  32. protected $user = null;
  33. function setUp()
  34. {
  35. $user = User::getKV('nickname', 'userrightstestuser');
  36. if ($user) {
  37. // Leftover from a broken test run?
  38. $profile = $user->getProfile();
  39. $user->delete();
  40. $profile->delete();
  41. }
  42. $this->user = User::register(array('nickname' => 'userrightstestuser'));
  43. if (!$this->user) {
  44. throw new Exception("Couldn't register userrightstestuser");
  45. }
  46. }
  47. function tearDown()
  48. {
  49. if ($this->user) {
  50. $profile = $this->user->getProfile();
  51. $this->user->delete();
  52. $profile->delete();
  53. }
  54. }
  55. function testInvalidRole()
  56. {
  57. $this->assertFalse($this->user->hasRole('invalidrole'));
  58. }
  59. function standardRoles()
  60. {
  61. return array(array('admin'),
  62. array('moderator'));
  63. }
  64. /**
  65. * @dataProvider standardRoles
  66. * @param $role
  67. */
  68. function testUngrantedRole($role)
  69. {
  70. $this->assertFalse($this->user->hasRole($role));
  71. }
  72. /**
  73. * @dataProvider standardRoles
  74. * @param $role
  75. */
  76. function testGrantedRole($role)
  77. {
  78. $this->user->grantRole($role);
  79. $this->assertTrue($this->user->hasRole($role));
  80. }
  81. }