| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  | namespace Tests\Unit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!defined('INSTALLDIR')) { | 
					
						
							|  |  |  |     define('INSTALLDIR', dirname(dirname(__DIR__))); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-25 01:29:20 +01:00
										 |  |  | if (!defined('PUBLICDIR')) { | 
					
						
							|  |  |  |     define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  | if (!defined('GNUSOCIAL')) { | 
					
						
							|  |  |  |     define('GNUSOCIAL', true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | if (!defined('STATUSNET')) { // Compatibility
 | 
					
						
							|  |  |  |     define('STATUSNET', true); | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							|  |  |  | use User; | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 13:36:02 +01:00
										 |  |  | require_once INSTALLDIR . '/lib/util/common.php'; | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  | final class UserRightsTest extends TestCase | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     protected $user; | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     protected function setUp(): void | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $user = User::getKV('nickname', 'userrightstestuser'); | 
					
						
							| 
									
										
										
										
											2009-12-15 14:49:53 -08:00
										 |  |  |         if ($user) { | 
					
						
							|  |  |  |             // Leftover from a broken test run?
 | 
					
						
							|  |  |  |             $profile = $user->getProfile(); | 
					
						
							|  |  |  |             $user->delete(); | 
					
						
							|  |  |  |             $profile->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |         $this->user = User::register(['nickname' => 'userrightstestuser']); | 
					
						
							| 
									
										
										
										
											2009-12-15 14:49:53 -08:00
										 |  |  |         if (!$this->user) { | 
					
						
							|  |  |  |             throw new Exception("Couldn't register userrightstestuser"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     protected function tearDown(): void | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-15 14:49:53 -08:00
										 |  |  |         if ($this->user) { | 
					
						
							|  |  |  |             $profile = $this->user->getProfile(); | 
					
						
							|  |  |  |             $this->user->delete(); | 
					
						
							|  |  |  |             $profile->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     public function testInvalidRole() | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |         static::assertFalse($this->user->hasRole('invalidrole')); | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     public function standardRoles() | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |         return [['admin'], | 
					
						
							|  |  |  |             ['moderator'],]; | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider standardRoles | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  |      * @param $role | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     public function testUngrantedRole($role) | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |         static::assertFalse($this->user->hasRole($role)); | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider standardRoles | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-07-12 16:31:14 +01:00
										 |  |  |      * @param $role | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |     public function testGrantedRole($role) | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->user->grantRole($role); | 
					
						
							| 
									
										
										
										
											2020-06-24 13:08:11 +01:00
										 |  |  |         static::assertTrue($this->user->hasRole($role)); | 
					
						
							| 
									
										
										
										
											2009-09-15 15:28:11 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-09-28 15:20:10 +02:00
										 |  |  | } |