|
16 | 16 | use App\Entity\Package; |
17 | 17 | use App\Entity\User; |
18 | 18 | use App\Tests\IntegrationTestCase; |
| 19 | +use PHPUnit\Framework\Attributes\TestWith; |
19 | 20 |
|
20 | 21 | class PackageControllerTest extends IntegrationTestCase |
21 | 22 | { |
@@ -144,4 +145,82 @@ public function testRemoveMaintainer(): void |
144 | 145 |
|
145 | 146 | $this->assertNotNull($auditRecord); |
146 | 147 | } |
| 148 | + |
| 149 | + public function testTransferPackage(): void |
| 150 | + { |
| 151 | + $john = self:: createUser( 'john', '[email protected]'); |
| 152 | + $alice = self:: createUser( 'alice', '[email protected]'); |
| 153 | + $bob = self:: createUser( 'bob', '[email protected]'); |
| 154 | + $package = self::createPackage('test/pkg', 'https://example.com/test/pkg', maintainers: [$john, $alice]); |
| 155 | + |
| 156 | + $this->store($john, $alice, $bob, $package); |
| 157 | + |
| 158 | + $this->client->loginUser($john); |
| 159 | + |
| 160 | + $this->assertTrue($package->isMaintainer($john)); |
| 161 | + $this->assertTrue($package->isMaintainer($alice)); |
| 162 | + $this->assertFalse($package->isMaintainer($bob)); |
| 163 | + |
| 164 | + $crawler = $this->client->request('GET', '/packages/test/pkg'); |
| 165 | + |
| 166 | + $form = $crawler->filter('[name="transfer_package_form"]')->form(); |
| 167 | + $form->setValues([ |
| 168 | + 'transfer_package_form[maintainers][0]' => 'alice', |
| 169 | + 'transfer_package_form[maintainers][1]' => '[email protected]', |
| 170 | + ]); |
| 171 | + |
| 172 | + $this->client->submit($form); |
| 173 | + |
| 174 | + $this->assertResponseRedirects('/packages/test/pkg'); |
| 175 | + $this->client->followRedirect(); |
| 176 | + $this->assertResponseIsSuccessful(); |
| 177 | + |
| 178 | + $em = self::getEM(); |
| 179 | + $em->clear(); |
| 180 | + |
| 181 | + $package = $em->getRepository(Package::class)->find($package->getId()); |
| 182 | + $this->assertNotNull($package); |
| 183 | + |
| 184 | + $maintainerIds = array_map(fn (User $user) => $user->getId(), $package->getMaintainers()->toArray()); |
| 185 | + $this->assertContains($alice->getId(), $maintainerIds); |
| 186 | + $this->assertContains($bob->getId(), $maintainerIds); |
| 187 | + $this->assertNotContains($john->getId(), $maintainerIds); |
| 188 | + |
| 189 | + $auditRecord = $em->getRepository(\App\Entity\AuditRecord::class)->findOneBy([ |
| 190 | + 'type' => AuditRecordType::PackageTransferred->value, |
| 191 | + 'packageId' => $package->getId(), |
| 192 | + ]); |
| 193 | + |
| 194 | + $this->assertNotNull($auditRecord, 'Audit record not found'); |
| 195 | + } |
| 196 | + |
| 197 | + #[TestWith(['does_not_exist', 'value is not a valid username or email'])] |
| 198 | + #[TestWith([null, 'at least one maintainer must be specified'])] |
| 199 | + public function testTransferPackageReturnsValidationError(?string $value, string $message): void |
| 200 | + { |
| 201 | + $alice = self:: createUser( 'alice', '[email protected]'); |
| 202 | + $package = self::createPackage('test/pkg', 'https://example.com/test/pkg', maintainers: [$alice]); |
| 203 | + |
| 204 | + $this->store($alice, $package); |
| 205 | + |
| 206 | + $this->client->loginUser($alice); |
| 207 | + |
| 208 | + $crawler = $this->client->request('GET', '/packages/test/pkg'); |
| 209 | + |
| 210 | + $form = $crawler->filter('[name="transfer_package_form"]')->form(); |
| 211 | + $form->setValues([ |
| 212 | + 'transfer_package_form[maintainers][0]' => $value, |
| 213 | + ]); |
| 214 | + |
| 215 | + $this->client->submit($form); |
| 216 | + |
| 217 | + $this->assertResponseRedirects('/packages/test/pkg'); |
| 218 | + $crawler = $this->client->followRedirect(); |
| 219 | + $this->assertResponseIsSuccessful(); |
| 220 | + |
| 221 | + $elements = $crawler->filter('.flash-container .alert-error'); |
| 222 | + $this->assertCount(1, $elements); |
| 223 | + $text = $elements->text(); |
| 224 | + $this->assertStringContainsStringIgnoringCase($message, $text); |
| 225 | + } |
147 | 226 | } |
0 commit comments