|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Integration\Configuration\Controller; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Configuration\Model\Config; |
| 8 | +use PhpList\RestBundle\Configuration\Controller\ConfigController; |
| 9 | +use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController; |
| 10 | +use PhpList\RestBundle\Tests\Integration\Identity\Fixtures\AdministratorFixture; |
| 11 | +use PhpList\RestBundle\Tests\Integration\Identity\Fixtures\AdministratorTokenFixture; |
| 12 | + |
| 13 | +class ConfigControllerTest extends AbstractTestController |
| 14 | +{ |
| 15 | + public function testControllerIsAvailableViaContainer(): void |
| 16 | + { |
| 17 | + self::assertInstanceOf(ConfigController::class, self::getContainer()->get(ConfigController::class)); |
| 18 | + } |
| 19 | + |
| 20 | + public function testListWithoutSessionKeyReturnsForbidden(): void |
| 21 | + { |
| 22 | + self::getClient()->request('GET', '/api/v2/configs'); |
| 23 | + $this->assertHttpForbidden(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testListWithExpiredSessionKeyReturnsForbidden(): void |
| 27 | + { |
| 28 | + $this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class]); |
| 29 | + |
| 30 | + self::getClient()->request( |
| 31 | + 'GET', |
| 32 | + '/api/v2/configs', |
| 33 | + [], |
| 34 | + [], |
| 35 | + ['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'expiredtoken'] |
| 36 | + ); |
| 37 | + |
| 38 | + $this->assertHttpForbidden(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testListWithValidSessionKeyReturnsOkayWithPaginationStructure(): void |
| 42 | + { |
| 43 | + $this->authenticatedJsonRequest('GET', '/api/v2/configs'); |
| 44 | + $this->assertHttpOkay(); |
| 45 | + |
| 46 | + $response = $this->getDecodedJsonResponseContent(); |
| 47 | + self::assertIsArray($response); |
| 48 | + self::assertArrayHasKey('items', $response); |
| 49 | + self::assertArrayHasKey('pagination', $response); |
| 50 | + } |
| 51 | + |
| 52 | + public function testListReturnsCreatedConfigData(): void |
| 53 | + { |
| 54 | + $config = new Config(); |
| 55 | + $config->setKey('organisation_name'); |
| 56 | + $config->setValue('Example Organisation'); |
| 57 | + $config->setEditable(true); |
| 58 | + $config->setType('text'); |
| 59 | + |
| 60 | + $this->entityManager->persist($config); |
| 61 | + $this->entityManager->flush(); |
| 62 | + |
| 63 | + $this->authenticatedJsonRequest('GET', '/api/v2/configs'); |
| 64 | + $this->assertHttpOkay(); |
| 65 | + |
| 66 | + $response = $this->getDecodedJsonResponseContent(); |
| 67 | + self::assertNotEmpty($response['items']); |
| 68 | + |
| 69 | + $found = false; |
| 70 | + foreach ($response['items'] as $item) { |
| 71 | + if ($item['key'] === 'organisation_name') { |
| 72 | + $found = true; |
| 73 | + self::assertSame('Example Organisation', $item['value']); |
| 74 | + self::assertTrue($item['editable']); |
| 75 | + self::assertSame('text', $item['type']); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + self::assertTrue($found, 'Created config item not found in list response.'); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGetOneWithoutSessionKeyReturnsForbidden(): void |
| 83 | + { |
| 84 | + self::getClient()->request('GET', '/api/v2/configs/organisation_name'); |
| 85 | + $this->assertHttpForbidden(); |
| 86 | + } |
| 87 | + |
| 88 | + public function testGetOneWithValidSessionReturnsOkayAndData(): void |
| 89 | + { |
| 90 | + $config = new Config(); |
| 91 | + $config->setKey('site_title'); |
| 92 | + $config->setValue('My Site'); |
| 93 | + $config->setEditable(true); |
| 94 | + |
| 95 | + $this->entityManager->persist($config); |
| 96 | + $this->entityManager->flush(); |
| 97 | + |
| 98 | + $this->authenticatedJsonRequest('GET', '/api/v2/configs/site_title'); |
| 99 | + $this->assertHttpOkay(); |
| 100 | + |
| 101 | + $response = $this->getDecodedJsonResponseContent(); |
| 102 | + self::assertSame('site_title', $response['key']); |
| 103 | + self::assertSame('My Site', $response['value']); |
| 104 | + self::assertTrue($response['editable']); |
| 105 | + } |
| 106 | + |
| 107 | + public function testGetOneWithInvalidKeyReturnsNotFound(): void |
| 108 | + { |
| 109 | + $this->authenticatedJsonRequest('GET', '/api/v2/configs/nonexistent_key'); |
| 110 | + $this->assertHttpNotFound(); |
| 111 | + } |
| 112 | + |
| 113 | + public function testCreateWithoutSessionKeyReturnsForbidden(): void |
| 114 | + { |
| 115 | + $json = json_encode(['key' => 'new_key', 'value' => 'val']); |
| 116 | + $this->jsonRequest('POST', '/api/v2/configs', [], [], [], $json); |
| 117 | + $this->assertHttpForbidden(); |
| 118 | + } |
| 119 | + |
| 120 | + public function testCreateWithValidSessionCreatesConfig(): void |
| 121 | + { |
| 122 | + $json = json_encode(['key' => 'new_key', 'value' => 'val', 'editable' => true, 'type' => 'text']); |
| 123 | + $this->authenticatedJsonRequest('POST', '/api/v2/configs', [], [], [], $json); |
| 124 | + |
| 125 | + $this->assertHttpCreated(); |
| 126 | + $response = $this->getDecodedJsonResponseContent(); |
| 127 | + self::assertSame('new_key', $response['key']); |
| 128 | + self::assertSame('val', $response['value']); |
| 129 | + self::assertTrue($response['editable']); |
| 130 | + self::assertSame('text', $response['type']); |
| 131 | + } |
| 132 | + |
| 133 | + public function testCreateWithDuplicateKeyReturnsConflict(): void |
| 134 | + { |
| 135 | + $config = new Config(); |
| 136 | + $config->setKey('dup_key'); |
| 137 | + $config->setValue('first'); |
| 138 | + $this->entityManager->persist($config); |
| 139 | + $this->entityManager->flush(); |
| 140 | + |
| 141 | + $json = json_encode(['key' => 'dup_key', 'value' => 'second']); |
| 142 | + $this->authenticatedJsonRequest('POST', '/api/v2/configs', [], [], [], $json); |
| 143 | + |
| 144 | + $this->assertHttpConflict(); |
| 145 | + } |
| 146 | + |
| 147 | + public function testUpdateWithoutSessionKeyReturnsForbidden(): void |
| 148 | + { |
| 149 | + $this->jsonRequest('PUT', '/api/v2/configs/some_key', [], [], [], json_encode(['value' => 'x'])); |
| 150 | + $this->assertHttpForbidden(); |
| 151 | + } |
| 152 | + |
| 153 | + public function testUpdateWithValidSessionUpdatesValue(): void |
| 154 | + { |
| 155 | + $config = new Config(); |
| 156 | + $config->setKey('up_key'); |
| 157 | + $config->setValue('old'); |
| 158 | + $config->setEditable(true); |
| 159 | + $this->entityManager->persist($config); |
| 160 | + $this->entityManager->flush(); |
| 161 | + |
| 162 | + $this->authenticatedJsonRequest('PUT', '/api/v2/configs/up_key', [], [], [], json_encode(['value' => 'new'])); |
| 163 | + |
| 164 | + $this->assertHttpOkay(); |
| 165 | + $response = $this->getDecodedJsonResponseContent(); |
| 166 | + self::assertSame('new', $response['value']); |
| 167 | + } |
| 168 | + |
| 169 | + public function testUpdateForNonexistentKeyReturnsNotFound(): void |
| 170 | + { |
| 171 | + $this->authenticatedJsonRequest( |
| 172 | + 'PUT', |
| 173 | + '/api/v2/configs/does_not_exist', |
| 174 | + [], |
| 175 | + [], |
| 176 | + [], |
| 177 | + json_encode(['value' => 'x']) |
| 178 | + ); |
| 179 | + $this->assertHttpNotFound(); |
| 180 | + } |
| 181 | + |
| 182 | + public function testUpdateNonEditableReturnsForbidden(): void |
| 183 | + { |
| 184 | + $config = new Config(); |
| 185 | + $config->setKey('locked_key'); |
| 186 | + $config->setValue('orig'); |
| 187 | + $config->setEditable(false); |
| 188 | + $this->entityManager->persist($config); |
| 189 | + $this->entityManager->flush(); |
| 190 | + |
| 191 | + $this->authenticatedJsonRequest( |
| 192 | + 'PUT', |
| 193 | + '/api/v2/configs/locked_key', |
| 194 | + [], |
| 195 | + [], |
| 196 | + [], |
| 197 | + json_encode(['value' => 'changed']) |
| 198 | + ); |
| 199 | + |
| 200 | + $this->assertHttpForbidden(); |
| 201 | + } |
| 202 | + |
| 203 | + public function testDeleteWithoutSessionKeyReturnsForbidden(): void |
| 204 | + { |
| 205 | + self::getClient()->request('DELETE', '/api/v2/configs/some_key'); |
| 206 | + $this->assertHttpForbidden(); |
| 207 | + } |
| 208 | + |
| 209 | + public function testDeleteWithValidSessionDeletesConfig(): void |
| 210 | + { |
| 211 | + $config = new Config(); |
| 212 | + $config->setKey('del_key'); |
| 213 | + $config->setValue('to delete'); |
| 214 | + $this->entityManager->persist($config); |
| 215 | + $this->entityManager->flush(); |
| 216 | + |
| 217 | + $this->authenticatedJsonRequest('DELETE', '/api/v2/configs/del_key'); |
| 218 | + $this->assertHttpNoContent(); |
| 219 | + |
| 220 | + $this->entityManager->clear(); |
| 221 | + self::assertNull($this->entityManager->getRepository(Config::class)->find('del_key')); |
| 222 | + } |
| 223 | + |
| 224 | + public function testDeleteNonexistentReturnsNotFound(): void |
| 225 | + { |
| 226 | + $this->authenticatedJsonRequest('DELETE', '/api/v2/configs/unknown_key'); |
| 227 | + $this->assertHttpNotFound(); |
| 228 | + } |
| 229 | +} |
0 commit comments