1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 * 21 */ 22 23declare(strict_types=1); 24 25namespace Ampache\Module\Application\Admin\Access\Lib; 26 27use Ampache\MockeryTestCase; 28use Ampache\Repository\Model\ModelFactoryInterface; 29use Ampache\Repository\Model\User; 30use Ampache\Module\Authorization\Access; 31use Mockery\MockInterface; 32 33class AccessListItemTest extends MockeryTestCase 34{ 35 /** @var MockInterface|Access|null */ 36 private MockInterface $access; 37 38 /** @var MockInterface|ModelFactoryInterface|null */ 39 private MockInterface $modelFactory; 40 41 private ?AccessListItem $subject; 42 43 public function setUp(): void 44 { 45 $this->access = $this->mock(Access::class); 46 $this->modelFactory = $this->mock(ModelFactoryInterface::class); 47 48 $this->subject = new AccessListItem( 49 $this->modelFactory, 50 $this->access 51 ); 52 } 53 54 /** 55 * @dataProvider levelNameDataProvider 56 */ 57 public function testGetLevelNameReturnsLabel( 58 int $level, 59 string $label 60 ): void { 61 $this->access->level = $level; 62 63 $this->assertSame( 64 $label, 65 $this->subject->getLevelName() 66 ); 67 } 68 69 public function levelNameDataProvider(): array 70 { 71 return [ 72 [99, 'All'], 73 [5, 'View'], 74 [25, 'Read'], 75 [50, 'Read/Write'], 76 [-666, ''], 77 ]; 78 } 79 80 public function testGetUserNameReturnsDefault(): void 81 { 82 $userId = -1; 83 84 $this->access->user = (string)$userId; 85 86 $this->assertSame( 87 'All', 88 $this->subject->getUserName() 89 ); 90 } 91 92 public function testGetUserNameReturnsName(): void 93 { 94 $userId = 666; 95 $userFullName = 'fullname'; 96 $userName = 'username'; 97 98 $user = $this->mock(User::class); 99 100 $user->fullname = $userFullName; 101 $user->username = $userName; 102 103 $this->access->user = (string)$userId; 104 105 $this->modelFactory->shouldReceive('createUser') 106 ->with($userId) 107 ->once() 108 ->andReturn($user); 109 110 111 $this->assertSame( 112 sprintf('%s (%s)', $userFullName, $userName), 113 $this->subject->getUserName() 114 ); 115 } 116 117 /** 118 * @dataProvider typeNameDataProvider 119 */ 120 public function testGetTypeNameReturnLabel( 121 string $typeId, 122 string $label 123 ): void { 124 $this->access->type = $typeId; 125 126 $this->assertSame( 127 $label, 128 $this->subject->getTypeName() 129 ); 130 } 131 132 public function typeNameDataProvider(): array 133 { 134 return [ 135 ['rpc', 'API/RPC'], 136 ['network', 'Local Network Definition'], 137 ['interface', 'Web Interface'], 138 ['stream', 'Stream Access'], 139 ['foobar', 'Stream Access'], 140 ]; 141 } 142 143 public function testGetStartIpReturnsEmptyStringIfConversionFails(): void 144 { 145 $this->access->start = 'foobar'; 146 147 $this->assertSame( 148 '', 149 $this->subject->getStartIp() 150 ); 151 } 152 153 public function testGetStartIpReturnsIpReadable(): void 154 { 155 $ip = '1.2.3.4'; 156 157 $this->access->start = inet_pton($ip); 158 159 $this->assertSame( 160 $ip, 161 $this->subject->getStartIp() 162 ); 163 } 164 165 public function testGetEndIpReturnsEmptyStringIfConversionFails(): void 166 { 167 $this->access->end = 'foobar'; 168 169 $this->assertSame( 170 '', 171 $this->subject->getEndIp() 172 ); 173 } 174 175 public function testGetEndIpReturnsIpReadable(): void 176 { 177 $ip = '1.2.3.4'; 178 179 $this->access->end = inet_pton($ip); 180 181 $this->assertSame( 182 $ip, 183 $this->subject->getEndIp() 184 ); 185 } 186 187 public function testGetNameReturnsValue(): void 188 { 189 $value = 'some-value'; 190 191 $this->access->name = $value; 192 193 $this->assertSame( 194 $value, 195 $this->subject->getName() 196 ); 197 } 198 199 public function testGetIdReturnsValue(): void 200 { 201 $value = 42; 202 203 $this->access->id = $value; 204 205 $this->assertSame( 206 $value, 207 $this->subject->getId() 208 ); 209 } 210 211 public function testGetLevelReturnsValue(): void 212 { 213 $value = 42; 214 215 $this->access->level = $value; 216 217 $this->assertSame( 218 $value, 219 $this->subject->getLevel() 220 ); 221 } 222 223 public function testGetTypeReturnsValue(): void 224 { 225 $value = 'some-value'; 226 227 $this->access->type = $value; 228 229 $this->assertSame( 230 $value, 231 $this->subject->getType() 232 ); 233 } 234 235 public function testGetUserIdReturnsValue(): void 236 { 237 $value = 42; 238 239 $this->access->user = $value; 240 241 $this->assertSame( 242 $value, 243 $this->subject->getUserId() 244 ); 245 } 246} 247