|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace JmapClient\Tests\Integration\Mail; |
| 10 | + |
| 11 | +use JmapClient\Client; |
| 12 | +use JmapClient\Requests\Mail\MailIdentityGet as MailIdentityGetRequest; |
| 13 | +use JmapClient\Requests\Mail\MailIdentitySet as MailIdentitySetRequest; |
| 14 | +use JmapClient\Responses\Mail\MailIdentityGet as MailIdentityGetResponse; |
| 15 | +use JmapClient\Responses\Mail\MailIdentityParameters; |
| 16 | +use JmapClient\Responses\Mail\MailIdentitySet as MailIdentitySetResponse; |
| 17 | +use JmapClient\Session\Account; |
| 18 | +use JmapClient\Tests\Integration\ClientFactory; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class MailIdentityTest extends TestCase |
| 22 | +{ |
| 23 | + private Client $client; |
| 24 | + private Account $account; |
| 25 | + private string $identityEmail = ''; |
| 26 | + |
| 27 | + public function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + $this->client = ClientFactory::instance('service1'); |
| 31 | + $this->client->connect(); |
| 32 | + $this->account = $this->client->sessionAccountDefault('submission'); |
| 33 | + |
| 34 | + // derive the email address from the service config identifier |
| 35 | + $this->identityEmail = ClientFactory::identifier('service1'); |
| 36 | + |
| 37 | + // create a seed identity so get/set tests always have at least one identity to work with |
| 38 | + $seedId = uniqid(); |
| 39 | + $rSeed = new MailIdentitySetRequest($this->account->id()); |
| 40 | + $pSeed = $rSeed->create($seedId); |
| 41 | + $pSeed->name('Test Identity ' . time()); |
| 42 | + $pSeed->address($this->identityEmail); |
| 43 | + $response = $this->client->perform([$rSeed]); |
| 44 | + $result = $response->first()->createSuccess($seedId); |
| 45 | + $this->assertNotNull($result, 'Failed to create seed identity'); |
| 46 | + } |
| 47 | + |
| 48 | + public function tearDown(): void |
| 49 | + { |
| 50 | + parent::tearDown(); |
| 51 | + // construct list request |
| 52 | + $r0 = new MailIdentityGetRequest($this->account->id()); |
| 53 | + // transceive |
| 54 | + $bundle = $this->client->perform([$r0]); |
| 55 | + $response = $bundle->first(); |
| 56 | + if (!($response instanceof MailIdentityGetResponse)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + $commands = []; |
| 60 | + // construct deletion requests for test-created identities |
| 61 | + foreach ($response->objects() as $identity) { |
| 62 | + if ($identity->deletable() && |
| 63 | + (str_starts_with($identity->name(), 'Test Identity ') || |
| 64 | + str_starts_with($identity->name(), 'Modified Identity ') || |
| 65 | + str_starts_with($identity->name(), 'To Delete Identity '))) { |
| 66 | + $rq = new MailIdentitySetRequest($this->account->id()); |
| 67 | + $rq->delete($identity->id()); |
| 68 | + $commands[] = $rq; |
| 69 | + } |
| 70 | + } |
| 71 | + // transceive |
| 72 | + if (!empty($commands)) { |
| 73 | + //$this->client->perform($commands); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function testMailIdentityGetAll(): void |
| 78 | + { |
| 79 | + // construct fetch request |
| 80 | + $r0 = new MailIdentityGetRequest($this->account->id()); |
| 81 | + |
| 82 | + // transceive |
| 83 | + $bundle = $this->client->perform([$r0]); |
| 84 | + |
| 85 | + // verify fetch response — per RFC 8621 §6 servers MUST provide at least one Identity |
| 86 | + $this->assertEquals(1, $bundle->tally()); |
| 87 | + $this->assertInstanceOf(MailIdentityGetResponse::class, $bundle->first()); |
| 88 | + $results = $bundle->first()->objects(); |
| 89 | + $this->assertIsArray($results); |
| 90 | + $this->assertNotEmpty($results); |
| 91 | + foreach ($results as $result) { |
| 92 | + $this->assertInstanceOf(MailIdentityParameters::class, $result); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public function testMailIdentityGetSpecific(): void |
| 97 | + { |
| 98 | + // retrieve all identities to find a real id |
| 99 | + $r0 = new MailIdentityGetRequest($this->account->id()); |
| 100 | + $bundle = $this->client->perform([$r0]); |
| 101 | + $first = $bundle->first()->object(0); |
| 102 | + $this->assertInstanceOf(MailIdentityParameters::class, $first); |
| 103 | + $identityId = $first->id(); |
| 104 | + |
| 105 | + // construct fetch request for a specific identity |
| 106 | + $r1 = new MailIdentityGetRequest($this->account->id()); |
| 107 | + $r1->target($identityId); |
| 108 | + |
| 109 | + // transceive |
| 110 | + $bundle = $this->client->perform([$r1]); |
| 111 | + |
| 112 | + // verify fetch response |
| 113 | + $this->assertEquals(1, $bundle->tally()); |
| 114 | + $this->assertInstanceOf(MailIdentityGetResponse::class, $bundle->first()); |
| 115 | + $result = $bundle->first()->object(0); |
| 116 | + $this->assertInstanceOf(MailIdentityParameters::class, $result); |
| 117 | + $this->assertEquals($identityId, $result->id()); |
| 118 | + $this->assertNotEmpty($result->name()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testMailIdentityGetWithProperties(): void |
| 122 | + { |
| 123 | + // construct fetch request with limited properties |
| 124 | + $r0 = new MailIdentityGetRequest($this->account->id()); |
| 125 | + $r0->property('id', 'name'); |
| 126 | + |
| 127 | + // transceive |
| 128 | + $bundle = $this->client->perform([$r0]); |
| 129 | + |
| 130 | + // verify fetch response — unrequested properties should be absent/null |
| 131 | + $this->assertEquals(1, $bundle->tally()); |
| 132 | + $this->assertInstanceOf(MailIdentityGetResponse::class, $bundle->first()); |
| 133 | + $result = $bundle->first()->object(0); |
| 134 | + $this->assertInstanceOf(MailIdentityParameters::class, $result); |
| 135 | + $this->assertNotEmpty($result->id()); |
| 136 | + $this->assertNotNull($result->name()); |
| 137 | + $this->assertNull($result->address()); |
| 138 | + } |
| 139 | + |
| 140 | + public function testMailIdentitySetCreate(): void |
| 141 | + { |
| 142 | + // construct create request |
| 143 | + $createId = uniqid(); |
| 144 | + $r0 = new MailIdentitySetRequest($this->account->id()); |
| 145 | + $p0 = $r0->create($createId); |
| 146 | + $p0->name('Test Identity ' . time()); |
| 147 | + $p0->address($this->identityEmail); |
| 148 | + $p0->signaturePlain('-- Test signature'); |
| 149 | + |
| 150 | + // transceive |
| 151 | + $bundle = $this->client->perform([$r0]); |
| 152 | + |
| 153 | + // verify create response |
| 154 | + $this->assertEquals(1, $bundle->tally()); |
| 155 | + $this->assertInstanceOf(MailIdentitySetResponse::class, $bundle->first()); |
| 156 | + $result = $bundle->first()->createSuccess($createId); |
| 157 | + $this->assertNotNull($result); |
| 158 | + $this->assertArrayHasKey('id', $result); |
| 159 | + } |
| 160 | + |
| 161 | + public function testMailIdentitySetUpdate(): void |
| 162 | + { |
| 163 | + // construct create request |
| 164 | + $createId = uniqid(); |
| 165 | + $r0 = new MailIdentitySetRequest($this->account->id()); |
| 166 | + $p0 = $r0->create($createId); |
| 167 | + $p0->name('Test Identity ' . time()); |
| 168 | + $p0->address($this->identityEmail); |
| 169 | + |
| 170 | + // transceive |
| 171 | + $bundle = $this->client->perform([$r0]); |
| 172 | + |
| 173 | + // extract identity id |
| 174 | + $result = $bundle->first()->createSuccess($createId); |
| 175 | + $identityId = $result['id']; |
| 176 | + |
| 177 | + // construct modify request |
| 178 | + $r1 = new MailIdentitySetRequest($this->account->id()); |
| 179 | + $p1 = $r1->update($identityId); |
| 180 | + $p1->name('Modified Identity ' . time()); |
| 181 | + $p1->signaturePlain('-- Modified test signature'); |
| 182 | + |
| 183 | + // transceive |
| 184 | + $bundle = $this->client->perform([$r1]); |
| 185 | + |
| 186 | + // verify modify response |
| 187 | + $this->assertEquals(1, $bundle->tally()); |
| 188 | + $this->assertInstanceOf(MailIdentitySetResponse::class, $bundle->first()); |
| 189 | + $result = $bundle->first()->updateSuccess($identityId); |
| 190 | + $this->assertNotNull($result); |
| 191 | + $this->assertArrayHasKey('id', $result); |
| 192 | + } |
| 193 | + |
| 194 | + public function testMailIdentitySetDelete(): void |
| 195 | + { |
| 196 | + // construct create request |
| 197 | + $createId = uniqid(); |
| 198 | + $r0 = new MailIdentitySetRequest($this->account->id()); |
| 199 | + $p0 = $r0->create($createId); |
| 200 | + $p0->name('To Delete Identity ' . time()); |
| 201 | + $p0->address($this->identityEmail); |
| 202 | + |
| 203 | + // transceive |
| 204 | + $bundle = $this->client->perform([$r0]); |
| 205 | + |
| 206 | + // extract identity id |
| 207 | + $result = $bundle->first()->createSuccess($createId); |
| 208 | + $identityId = $result['id']; |
| 209 | + |
| 210 | + // verify the newly created identity is deletable |
| 211 | + $r1 = new MailIdentityGetRequest($this->account->id()); |
| 212 | + $r1->target($identityId); |
| 213 | + $bundle = $this->client->perform([$r1]); |
| 214 | + $identity = $bundle->first()->object(0); |
| 215 | + $this->assertInstanceOf(MailIdentityParameters::class, $identity); |
| 216 | + $this->assertTrue($identity->deletable(), 'Newly created identity should be deletable'); |
| 217 | + |
| 218 | + // construct delete request |
| 219 | + $r2 = new MailIdentitySetRequest($this->account->id()); |
| 220 | + $r2->delete($identityId); |
| 221 | + |
| 222 | + // transceive |
| 223 | + $bundle = $this->client->perform([$r2]); |
| 224 | + |
| 225 | + // verify delete response |
| 226 | + $this->assertEquals(1, $bundle->tally()); |
| 227 | + $this->assertInstanceOf(MailIdentitySetResponse::class, $bundle->first()); |
| 228 | + $result = $bundle->first()->deleteSuccess($identityId); |
| 229 | + $this->assertNotNull($result); |
| 230 | + $this->assertArrayHasKey('id', $result); |
| 231 | + } |
| 232 | +} |
0 commit comments