diff --git a/src/app/Console/Commands/Data/Reports/VatCommand.php b/src/app/Console/Commands/Data/Reports/VatCommand.php index 6468100a..79c927ee 100644 --- a/src/app/Console/Commands/Data/Reports/VatCommand.php +++ b/src/app/Console/Commands/Data/Reports/VatCommand.php @@ -1,88 +1,90 @@ argument('email'); $result = DB::select( -"SELECT - DATE_FORMAT(p.created_at, '%Y-%m-%d %H:%I') AS timestamp, - v.country AS country, - p.id AS payment_id, - ROUND((amount / 100), 2) AS income_gross, - ROUND(((amount - (amount / (100 + v.rate) * v.rate)) / 100), 2) AS income_net, - ROUND(((amount / (100 + v.rate) * v.rate) / 100), 2) AS income_vat -FROM - payments p -INNER JOIN vat_rates v - ON p.vat_rate_id = v.id -INNER JOIN wallets w - ON p.wallet_id = w.id -INNER JOIN user_settings us - ON w.user_id = us.user_id -WHERE - p.status = 'paid' - AND us.`key` = 'country' -ORDER BY timestamp, country" + <<sendMail($recipient, $csv); } /** * Sends an email message with csv file attached */ protected function sendMail($recipient, $csv) { $plainBody = 'See the attached report!'; $attachment = Attachment::fromData(fn () => $csv, 'Report.csv')->withMime('text/csv'); $mail = new \App\Mail\Mailable(); $mail->subject('VAT Report') // This hack allows as to use plain text body instead of a Laravel view ->text(new \Illuminate\Support\HtmlString($plainBody)) ->to($recipient) ->attach($attachment); \App\Mail\Helper::sendMail($mail); } } diff --git a/src/app/Console/Commands/Status/Health.php b/src/app/Console/Commands/Status/Health.php index 545c7af5..c62ce3be 100644 --- a/src/app/Console/Commands/Status/Health.php +++ b/src/app/Console/Commands/Status/Health.php @@ -1,219 +1,219 @@ line($exception); return false; } } private function checkOpenExchangeRates() { try { OpenExchangeRates::healthcheck(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkMollie() { try { return Mollie::healthcheck(); } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkDAV() { try { DAV::healthcheck(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkLDAP() { try { LDAP::healthcheck(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkIMAP() { try { IMAP::healthcheck(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkRoundcube() { try { //TODO maybe run a select? Roundcube::dbh(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkRedis() { try { Redis::connection(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkStorage() { try { Storage::healthcheck(); return true; } catch (\Exception $exception) { $this->line($exception); return false; } } private function checkMeet() { $urls = \config('meet.api_urls'); $success = true; foreach ($urls as $url) { $this->line("Checking $url"); try { $client = new \GuzzleHttp\Client( [ 'http_errors' => false, // No exceptions from Guzzle 'base_uri' => $url, 'verify' => \config('meet.api_verify_tls'), 'headers' => [ 'X-Auth-Token' => \config('meet.api_token'), ], 'connect_timeout' => 10, 'timeout' => 10, 'on_stats' => function (\GuzzleHttp\TransferStats $stats) { $threshold = \config('logging.slow_log'); if ($threshold && ($sec = $stats->getTransferTime()) > $threshold) { $url = $stats->getEffectiveUri(); $method = $stats->getRequest()->getMethod(); \Log::warning(sprintf("[STATS] %s %s: %.4f sec.", $method, $url, $sec)); } }, ] ); $response = $client->request('GET', "ping"); if ($response->getStatusCode() != 200) { $code = $response->getStatusCode(); $reason = $response->getReasonPhrase(); $success = false; $this->line("Backend {$url} not available. Status: {$code} Reason: {$reason}"); } } catch (\Exception $exception) { $success = false; $this->line("Backend {$url} not available. Error: {$exception}"); } } return $success; } /** * Execute the console command. * * @return mixed */ public function handle() { $result = 0; $steps = $this->option('check'); if (empty($steps)) { $steps = [ 'DB', 'Redis', 'IMAP', 'Roundcube', 'Meet', 'DAV', 'Mollie', 'OpenExchangeRates' ]; if (\config('app.with_ldap')) { array_unshift($steps, 'LDAP'); } if (\config('app.with_imap')) { array_unshift($steps, 'IMAP'); } if (\config('app.with_files')) { array_unshift($steps, 'Storage'); } } foreach ($steps as $step) { $func = "check{$step}"; $this->line("Checking {$step}..."); if ($this->{$func}()) { $this->info("OK"); } else { $this->error("Not found"); $result = 1; } } return $result; } } diff --git a/src/app/Console/Commands/Wallet/BalancesCommand.php b/src/app/Console/Commands/Wallet/BalancesCommand.php index e332be90..e8f7bd81 100644 --- a/src/app/Console/Commands/Wallet/BalancesCommand.php +++ b/src/app/Console/Commands/Wallet/BalancesCommand.php @@ -1,82 +1,82 @@ option('skip-zeros'); $negative = $this->option('negative'); $invalid = $this->option('invalid'); $wallets = Wallet::select('wallets.*', 'users.email') ->join('users', 'users.id', '=', 'wallets.user_id') ->withEnvTenantContext('users') ->whereNull('users.deleted_at') ->orderBy('balance'); if ($invalid) { $balances = Transaction::select(DB::raw('sum(amount) as summary, object_id as wallet_id')) ->where('object_type', Wallet::class) ->groupBy('wallet_id'); - + $wallets->addSelect('balances.summary') ->leftJoinSub($balances, 'balances', function ($join) { $join->on('wallets.id', '=', 'balances.wallet_id'); }) ->whereRaw('(balances.summary != wallets.balance or (balances.summary is null and wallets.balance != 0))'); if ($negative) { $wallets->where('balances.summary', '<', 0); } elseif ($skip_zeros) { $wallets->whereRaw('balances.summary != 0 and balances.summary is not null'); } } else { if ($negative) { $wallets->where('wallets.balance', '<', 0); } elseif ($skip_zeros) { $wallets->whereNot('wallets.balance', 0); } } $wallets->cursor()->each( function (Wallet $wallet) use ($invalid) { $balance = $wallet->balance; $summary = $wallet->summary ?? 0; $email = $wallet->email; // @phpstan-ignore-line if ($invalid) { $this->info(sprintf("%s: %8s %8s (%s)", $wallet->id, $balance, $summary, $email)); return; } $this->info(sprintf("%s: %8s (%s)", $wallet->id, $balance, $email)); } ); } } diff --git a/src/app/EventLog.php b/src/app/EventLog.php index 92ed887e..7a57fc1d 100644 --- a/src/app/EventLog.php +++ b/src/app/EventLog.php @@ -1,134 +1,134 @@ The attributes that are mass assignable */ protected $fillable = [ 'comment', // extra event info (json) 'data', 'type', // user, domain, etc. 'object_id', 'object_type', // actor, if any 'user_email', ]; /** @var array Casts properties as type */ protected $casts = [ 'created_at' => 'datetime:Y-m-d H:i:s', 'data' => 'array', 'type' => 'integer', ]; /** @var array The attributes that can be not set */ protected $nullable = ['comment', 'data', 'user_email']; /** @var string Database table name */ protected $table = 'eventlog'; /** @var bool Indicates if the model should be timestamped. */ public $timestamps = false; /** * Create an eventlog object for a specified object. * * @param object $object Object (User, Domain, etc.) * @param int $type Event type (use one of EventLog::TYPE_* consts) * @param ?string $comment Event description * @param ?array $data Extra information * * @return EventLog */ public static function createFor($object, int $type, string $comment = null, array $data = null): EventLog { $event = self::create([ 'object_id' => $object->id, 'object_type' => get_class($object), 'type' => $type, 'comment' => $comment, 'data' => $data, ]); return $event; } /** * Principally an object such as Domain, User, Group. * Note that it may be trashed (soft-deleted). * * @return mixed */ public function object() { return $this->morphTo()->withTrashed(); } /** * Get an event type name. * * @return ?string Event type name */ public function eventName(): ?string { switch ($this->type) { - case self::TYPE_SUSPENDED: - return \trans('app.event-suspended'); - case self::TYPE_UNSUSPENDED: - return \trans('app.event-unsuspended'); - case self::TYPE_COMMENT: - return \trans('app.event-comment'); - case self::TYPE_MAILSENT: - return \trans('app.event-mailsent'); - default: - return null; + case self::TYPE_SUSPENDED: + return \trans('app.event-suspended'); + case self::TYPE_UNSUSPENDED: + return \trans('app.event-unsuspended'); + case self::TYPE_COMMENT: + return \trans('app.event-comment'); + case self::TYPE_MAILSENT: + return \trans('app.event-mailsent'); + default: + return null; } } /** * Event type mutator * * @throws \Exception */ public function setTypeAttribute($type) { if (!is_numeric($type)) { throw new \Exception("Expecting an event type to be numeric"); } $type = (int) $type; if ($type < 0 || $type > 255) { throw new \Exception("Expecting an event type between 0 and 255"); } $this->attributes['type'] = $type; } } diff --git a/src/app/Http/Controllers/API/V4/Admin/EventLogController.php b/src/app/Http/Controllers/API/V4/Admin/EventLogController.php index d3083f4f..3bdb462d 100644 --- a/src/app/Http/Controllers/API/V4/Admin/EventLogController.php +++ b/src/app/Http/Controllers/API/V4/Admin/EventLogController.php @@ -1,67 +1,67 @@ errorResponse(404); } - $object = (new $object_type)->find($object_id); + $object = (new $object_type())->find($object_id); if (!$this->checkTenant($object)) { return $this->errorResponse(404); } $page = intval($request->input('page')) ?: 1; $pageSize = 20; $hasMore = false; $result = EventLog::where('object_id', $object_id) ->where('object_type', $object_type) ->orderBy('created_at', 'desc') ->limit($pageSize + 1) ->offset($pageSize * ($page - 1)) ->get(); if (count($result) > $pageSize) { $result->pop(); $hasMore = true; } $result = $result->map(function ($event) { return [ 'id' => $event->id, 'comment' => $event->comment, 'createdAt' => $event->created_at->toDateTimeString(), 'event' => $event->eventName(), 'data' => $event->data, 'user' => $event->user_email, ]; }); return response()->json([ 'list' => $result, 'count' => count($result), 'hasMore' => $hasMore, ]); } } diff --git a/src/app/Listeners/SqlDebug.php b/src/app/Listeners/SqlDebug.php index 48b133f8..d952d18c 100644 --- a/src/app/Listeners/SqlDebug.php +++ b/src/app/Listeners/SqlDebug.php @@ -1,81 +1,81 @@ */ public function subscribe(Dispatcher $events): array { if (!\config('app.debug')) { return []; } return [ QueryExecuted::class => 'handle', TransactionBeginning::class => 'handle', TransactionCommitted::class => 'handle', TransactionRolledBack::class => 'handle' ]; } /** * Handle the event. * * @param object $event An event object */ public function handle(object $event): void { - switch(get_class($event)) { - case TransactionBeginning::class: - $query = 'begin'; - break; - case TransactionCommitted::class: - $query = 'commit'; - break; - case TransactionRolledBack::class: - $query = 'rollback'; - break; - default: - $query = sprintf( - '%s [%s]: %.4f sec.', - $event->sql, - self::serializeSQLBindings($event->bindings, $event->sql), - $event->time / 1000 - ); + switch (get_class($event)) { + case TransactionBeginning::class: + $query = 'begin'; + break; + case TransactionCommitted::class: + $query = 'commit'; + break; + case TransactionRolledBack::class: + $query = 'rollback'; + break; + default: + $query = sprintf( + '%s [%s]: %.4f sec.', + $event->sql, + self::serializeSQLBindings($event->bindings, $event->sql), + $event->time / 1000 + ); } \Log::debug("[SQL] {$query}"); } /** * Serialize a bindings array to a string. */ private static function serializeSQLBindings(array $array, string $sql): string { $ipv = preg_match('/ip([46])nets/', $sql, $m) ? $m[1] : null; $serialized = array_map(function ($entry) use ($ipv) { if ($entry instanceof \DateTime) { return $entry->format('Y-m-d h:i:s'); } elseif ($ipv && is_string($entry) && strlen($entry) == ($ipv == 6 ? 16 : 4)) { // binary IP address? use HEX representation return '0x' . bin2hex($entry); } return $entry; }, $array); return implode(', ', $serialized); } } diff --git a/src/tests/Infrastructure/ActivesyncTest.php b/src/tests/Infrastructure/ActivesyncTest.php index 8e1af571..688bc0dd 100644 --- a/src/tests/Infrastructure/ActivesyncTest.php +++ b/src/tests/Infrastructure/ActivesyncTest.php @@ -1,1667 +1,1665 @@ loadXML($xml); $encoder->encode($dom); rewind($outputStream); return stream_get_contents($outputStream); } private static function fromWbxml($binary) { $stream = fopen('php://memory', 'r+'); fwrite($stream, $binary); rewind($stream); $decoder = new \Syncroton_Wbxml_Decoder($stream); return $decoder->decode(); } private function request($request, $cmd, $deviceId = null) { $user = self::$user; if (!$deviceId) { $deviceId = self::$deviceId; } $body = self::toWbxml($request); return self::$client->request( 'POST', "?Cmd={$cmd}&User={$user->email}&DeviceId={$deviceId}&DeviceType=WindowsOutlook15", [ 'headers' => [ "Content-Type" => "application/vnd.ms-sync.wbxml", 'MS-ASProtocolVersion' => "14.0" ], 'body' => $body ] ); } private function xpath($dom) { $xpath = new \DOMXpath($dom); $xpath->registerNamespace("ns", $dom->documentElement->namespaceURI); $xpath->registerNamespace("Tasks", "uri:Tasks"); $xpath->registerNamespace("Calendar", "uri:Calendar"); $xpath->registerNamespace("Email", "uri:Email"); $xpath->registerNamespace("Email2", "uri:Email2"); return $xpath; } /** * {@inheritDoc} */ public function setUp(): void { parent::setUp(); if (!self::$deviceId) { // By always creating a new device we force syncroton to initialize. // Otherwise we work against uninitialized metadata (subscription states), // because the account has been removed, but syncroton doesn't reinitalize the metadata for known devices. self::$deviceId = (string) Str::uuid(); self::$deviceId2 = (string) Str::uuid(); } $deviceId = self::$deviceId; \config(['imap.default_folders' => [ 'Drafts' => [ 'metadata' => [ '/private/vendor/kolab/folder-type' => 'mail.drafts', '/private/vendor/kolab/activesync' => "{\"FOLDER\":{\"{$deviceId}\":{\"S\":1}}}" ], ], 'Calendar' => [ 'metadata' => [ '/private/vendor/kolab/folder-type' => 'event.default', '/private/vendor/kolab/activesync' => "{\"FOLDER\":{\"{$deviceId}\":{\"S\":1}}}" ], ], 'Tasks' => [ 'metadata' => [ '/private/vendor/kolab/folder-type' => 'task.default', '/private/vendor/kolab/activesync' => "{\"FOLDER\":{\"{$deviceId}\":{\"S\":1}}}" ], ], 'Contacts' => [ 'metadata' => [ '/private/vendor/kolab/folder-type' => 'contact.default', '/private/vendor/kolab/activesync' => "{\"FOLDER\":{\"{$deviceId}\":{\"S\":1}}}" ], ], ]]); if (!self::$user) { self::$user = $this->getTestUser('activesynctest@kolab.org', ['password' => 'simple123'], true); //FIXME this shouldn't be required, but it seems to be. Roundcube::dbh()->table('kolab_cache_task')->truncate(); Roundcube::dbh()->table('syncroton_folder')->truncate(); // Roundcube::dbh()->table('syncroton_content')->truncate(); // Roundcube::dbh()->table('syncroton_device')->truncate(); } if (!self::$client) { self::$client = new \GuzzleHttp\Client([ 'http_errors' => false, // No exceptions 'base_uri' => \config("services.activesync.uri"), 'verify' => false, 'auth' => [self::$user->email, 'simple123'], 'connect_timeout' => 10, 'timeout' => 10, 'headers' => [ "Content-Type" => "application/xml; charset=utf-8", "Depth" => "1", ] ]); } } public function testOptions() { $response = self::$client->request('OPTIONS', ''); $this->assertEquals(200, $response->getStatusCode()); $this->assertStringContainsString('14', $response->getHeader('MS-Server-ActiveSync')[0]); $this->assertStringContainsString('14.1', $response->getHeader('MS-ASProtocolVersions')[0]); $this->assertStringContainsString('FolderSync', $response->getHeader('MS-ASProtocolCommands')[0]); } public function testPartialCommand() { $request = << 0 EOF; $body = self::toWbxml($request); $deviceId = self::$deviceId; $user = self::$user; $response = self::$client->request( 'POST', "?Cmd=FolderSync&User={$user->email}&DeviceId={$deviceId}&DeviceType=WindowsOutlook15", [ 'headers' => [ "Content-Type" => "application/vnd.ms-sync.wbxml", 'MS-ASProtocolVersion' => "14.0" ], //Truncated body 'body' => substr($body, 0, strlen($body) / 2) ] ); $this->assertEquals(500, $response->getStatusCode()); } public function testList() { $request = << 0 EOF; $response = $this->request($request, 'FolderSync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $xml = $dom->saveXML(); $this->assertStringContainsString('INBOX', $xml); // The hash is based on the name, so it's always the same $inboxId = '38b950ebd62cd9a66929c89615d0fc04'; $this->assertStringContainsString($inboxId, $xml); $this->assertStringContainsString('Drafts', $xml); $this->assertStringContainsString('Calendar', $xml); $this->assertStringContainsString('Tasks', $xml); $this->assertStringContainsString('Contacts', $xml); // Find the inbox for the next step // $collectionIds = $dom->getElementsByTagName('ServerId'); // $inboxId = $collectionIds[0]->nodeValue; return $inboxId; } /** * @depends testList */ public function testInitialSync($inboxId) { $request = << 0 {$inboxId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Email", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("1", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); return $inboxId; } /** * @depends testInitialSync */ public function testAdd($inboxId) { $request = << 1 {$inboxId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); // We expect an empty response without a change $this->assertEquals(0, $response->getBody()->getSize()); } /** * @depends testList */ public function testSyncTasks() { $tasksId = "90335880f65deff6e521acea2b71a773"; $request = << 0 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); return $tasksId; } /** * @depends testSyncTasks */ public function testAddTask($tasksId) { $request = << 1 {$tasksId} clientId1 task1 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Tasks", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("2", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); $this->assertEquals("clientId1", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals(0, $xpath->query("//ns:Commands")->length); return [ 'collectionId' => $tasksId, 'serverId1' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue ]; } /** * Re-issuing the same command should not result in the sync key being invalidated. * * @depends testAddTask */ public function testReAddTask($result) { $tasksId = $result['collectionId']; $request = << 1 {$tasksId} clientId1 task1 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Tasks", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("2", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); $this->assertEquals("clientId1", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals(0, $xpath->query("//ns:Commands")->length); return [ 'collectionId' => $tasksId, 'serverId1' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue ]; } /** * Make sure we can continue with the sync after the previous hickup, also include a modification. * * @depends testAddTask */ public function testAddTaskContinued($result) { $tasksId = $result['collectionId']; $serverId = $result['serverId1']; $request = << 2 {$tasksId} clientId2 task2 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z clientId3 task3 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z {$serverId} task4 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Tasks", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("3", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(2, $add->length); $this->assertEquals("clientId2", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals("clientId3", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(1)->nodeValue); $this->assertEquals(0, $xpath->query("//ns:Commands")->length); // The server does not have to inform about a successful change $change = $xpath->query("//ns:Responses/ns:Change"); $this->assertEquals(0, $change->length); return [ 'collectionId' => $tasksId, 'serverId1' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue, 'serverId2' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(1)->nodeValue ]; } /** * Perform another duplicate request. * * @depends testAddTaskContinued */ public function testAddTaskContinuedAgain($result) { $tasksId = $result['collectionId']; $serverId = $result['serverId1']; $request = << 2 {$tasksId} clientId2 task2 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z clientId3 task3 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z {$serverId} task4 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Tasks", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("3", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); print($dom->saveXML()); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(2, $add->length); $this->assertEquals("clientId2", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals( $result['serverId1'], $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue ); $this->assertEquals("clientId3", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(1)->nodeValue); $this->assertEquals( $result['serverId2'], $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(1)->nodeValue ); // The server does not have to inform about a successful change $change = $xpath->query("//ns:Responses/ns:Change"); $this->assertEquals(0, $change->length); $this->assertEquals(0, $xpath->query("//ns:Commands")->length); return [ 'collectionId' => $tasksId, 'serverId2' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(1)->nodeValue ]; } /** * Test a sync key that shouldn't exist yet. * @depends testSyncTasks */ public function testInvalidSyncKey($tasksId) { $request = << 4 {$tasksId} clientId999 task1 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("3", $status[0]->nodeValue); //After this we have to start from scratch } /** * Test fetching changes with a second device * @depends testAddTaskContinuedAgain */ public function testFetchTasks($result) { $tasksId = $result['collectionId']; $serverId = $result['serverId2']; // Initialize the second device $request = << 0 EOF; $response = $this->request($request, 'FolderSync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $request = << 0 {$tasksId} 0 512 0 2 8 4 1 16 EOF; $response = $this->request($request, 'Sync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); // Fetch the content $request = << 1 {$tasksId} 16 EOF; $response = $this->request($request, 'Sync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Commands/ns:Add"); $this->assertEquals(3, $add->length); //Resend the same command $request = << 1 {$tasksId} 16 EOF; $response = $this->request($request, 'Sync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Commands/ns:Add"); $this->assertEquals(3, $add->length); // Add another entry, delete an entry, with the original device (we have to init first again) $request = << 0 {$tasksId} 0 512 0 2 8 4 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$tasksId} 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 2 {$tasksId} clientId4 task4 0 2020-11-04T00:00:00.000Z 2020-11-03T23:00:00.000Z {$serverId} 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); // Delete does not have to be confirmed according to spec $delete = $xpath->query("//ns:Responses/ns:Delete"); $this->assertEquals(0, $delete->length); // And fetch the changes $request = << 2 {$tasksId} 16 EOF; $response = $this->request($request, 'Sync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); // print("=====\n"); // print($dom->saveXML()); // print("=====\n"); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Commands/ns:Add"); $this->assertEquals(1, $add->length); $delete = $xpath->query("//ns:Commands/ns:Delete"); $this->assertEquals(1, $delete->length); // and finally refetch the same changes $request = << 2 {$tasksId} 16 EOF; $response = $this->request($request, 'Sync', self::$deviceId2); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); // print("=====\n"); // print($dom->saveXML()); // print("=====\n"); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Commands/ns:Add"); $this->assertEquals(1, $add->length); //FIXME we currently miss deletions. $delete = $xpath->query("//ns:Commands/ns:Delete"); $this->assertEquals(0, $delete->length); } /** * @depends testList */ public function testSyncCalendar() { $tasksId = "cca1b81c734abbcd669bea90d23e08ae"; $request = << 0 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); return $tasksId; } /** * @depends testSyncCalendar */ public function testAddEvent($tasksId) { $request = << 1 {$tasksId} clientId1 20230719T200032Z 2 20230719T194232Z 20230719T203032Z 046f2e01-e8d0-47c6-a607-ba360251761d activesynctest@kolab.org 0 0 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Calendar", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("2", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); $this->assertEquals("clientId1", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals("1", $xpath->query("//ns:Responses/ns:Add/ns:Status")->item(0)->nodeValue); // $this->assertEquals(0, $xpath->query("//ns:Commands")->length); return [ 'collectionId' => $tasksId, 'serverId1' => $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue ]; } /** * @depends testAddEvent */ public function testReaddEvent($result) { $tasksId = $result['collectionId']; $request = << 2 {$tasksId} clientId1 20230719T200032Z 2 20230719T194232Z 20230719T203032Z 046f2e01-e8d0-47c6-a607-ba360251761d activesynctest@kolab.org 0 0 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $collections = $dom->getElementsByTagName('Collection'); $this->assertEquals(1, $collections->length); $collection = $collections->item(0); $this->assertEquals("Class", $collection->childNodes->item(0)->nodeName); $this->assertEquals("Calendar", $collection->childNodes->item(0)->nodeValue); $this->assertEquals("SyncKey", $collection->childNodes->item(1)->nodeName); $this->assertEquals("3", $collection->childNodes->item(1)->nodeValue); $this->assertEquals("Status", $collection->childNodes->item(3)->nodeName); $this->assertEquals("1", $collection->childNodes->item(3)->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); $this->assertEquals("clientId1", $xpath->query("//ns:Responses/ns:Add/ns:ClientId")->item(0)->nodeValue); $this->assertEquals("5", $xpath->query("//ns:Responses/ns:Add/ns:Status")->item(0)->nodeValue); $this->assertEquals(0, $xpath->query("//ns:Commands")->length); return $result; } /** * @depends testReaddEvent */ public function testDeleteEvent($result) { $tasksId = $result['collectionId']; $serverId = $result['serverId1']; $request = << 3 {$tasksId} {$serverId} 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); return $tasksId; } /** * @depends testDeleteEvent */ public function testMeetingResponse($tasksId) { $inboxId = '38b950ebd62cd9a66929c89615d0fc04'; $request = << 0 {$inboxId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); //Add the invitation to inbox $request = << 1 {$inboxId} clientid1 2023-07-24T06:53:57.000Z "Doe, John" <doe@kolab1.mkpf.ch> 65001 You've been invited to "event1" admin@kolab1.mkpf.ch 1 4 MIME-Version: 1.0 From: "Doe, John" <doe@kolab1.mkpf.ch> Date: Mon, 24 Jul 2023 08:53:55 +0200 Message-ID: <9cd8885d1339b976c7cb15db086e7bbc@kolab1.mkpf.ch> To: admin@kolab1.mkpf.ch Subject: You've been invited to "event1" Content-Type: multipart/alternative; boundary="=_a32392e5fc9266e3eeba97347cbfc147" --=_a32392e5fc9266e3eeba97347cbfc147 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; format=flowed *event1* When: 2023-07-24 11:00 - 11:30 (Europe/Vaduz) Invitees: Doe, John <doe@kolab1.mkpf.ch>, admin@kolab1.mkpf.ch Please find attached an iCalendar file with all the event details which you= =20 can import to your calendar application. --=_a32392e5fc9266e3eeba97347cbfc147 Content-Transfer-Encoding: 8bit Content-Type: text/calendar; charset=UTF-8; method=REQUEST; name=event.ics BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Roundcube 1.5.3//Sabre VObject 4.5.3//EN CALSCALE:GREGORIAN METHOD:REQUEST BEGIN:VTIMEZONE TZID:Europe/Vaduz BEGIN:STANDARD DTSTART:20221030T010000 TZOFFSETFROM:+0200 TZOFFSETTO:+0100 TZNAME:CET END:STANDARD BEGIN:STANDARD DTSTART:20231029T010000 TZOFFSETFROM:+0200 TZOFFSETTO:+0100 TZNAME:CET END:STANDARD BEGIN:DAYLIGHT DTSTART:20230326T010000 TZOFFSETFROM:+0100 TZOFFSETTO:+0200 TZNAME:CEST END:DAYLIGHT END:VTIMEZONE BEGIN:VEVENT UID:CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2 DTSTAMP:20230724T065355Z CREATED:20230724T065354Z LAST-MODIFIED:20230724T065354Z DTSTART;TZID=Europe/Vaduz:20230724T110000 DTEND;TZID=Europe/Vaduz:20230724T113000 SUMMARY:event1 SEQUENCE:0 TRANSP:OPAQUE ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;CUTYPE=INDIVIDUAL;RSVP= TRUE:mailto:admin@kolab1.mkpf.ch ORGANIZER;CN="Doe, John":mailto:doe@kolab1.mkpf.ch END:VEVENT END:VCALENDAR --=_a32392e5fc9266e3eeba97347cbfc147-- 1 IPM.Note urn:content-classes:message EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $status = $dom->getElementsByTagName('Status'); $this->assertEquals("1", $status[0]->nodeValue); $xpath = $this->xpath($dom); $add = $xpath->query("//ns:Responses/ns:Add"); $this->assertEquals(1, $add->length); $this->assertEquals("1", $xpath->query("//ns:Responses/ns:Add/ns:Status")->item(0)->nodeValue); $serverId = $xpath->query("//ns:Responses/ns:Add/ns:ServerId")->item(0)->nodeValue; //List the MeetingRequest $request = << 0 {$inboxId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$inboxId} 0 1 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals('IPM.Schedule.Meeting.Request', $xpath->query("//ns:Add/ns:ApplicationData/Email:MessageClass")->item(0)->nodeValue); $this->assertEquals('urn:content-classes:calendarmessage', $xpath->query("//ns:Add/ns:ApplicationData/Email:ContentClass")->item(0)->nodeValue); $this->assertEquals('BAAAAIIA4AB0xbcQGoLgCAAAAAAAAAAAAAAAAAAAAAAAAAAAPgAAAHZDYWwtVWlkAQAAAENDNTQxOTFGNjU2REZCQjI5NEJFMEFDMThFNzA5MzE1LTUyOUNCQkRENDdBQ0REQzIA', $xpath->query("//ns:Add/ns:ApplicationData/Email:MeetingRequest/Email:GlobalObjId")->item(0)->nodeValue); $this->assertEquals('1', $xpath->query("//ns:Add/ns:ApplicationData/Email:MeetingRequest/Email2:MeetingMessageType")->item(0)->nodeValue); //Send a meeting response to accept $request = << {$inboxId} 1 $serverId EOF; $response = $this->request($request, 'MeetingResponse'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals("1", $xpath->query("//ns:MeetingResponse/ns:Result/ns:Status")->item(0)->nodeValue); $this->assertStringContainsString("CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2", $xpath->query("//ns:MeetingResponse/ns:Result/ns:CalendarId")->item(0)->nodeValue); //Fetch the event and validate $request = << 0 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$tasksId} 0 1 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals("CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2", $xpath->query("//ns:Add/ns:ApplicationData/Calendar:UID")->item(0)->nodeValue); $this->assertEquals('activesynctest@kolab.org', $xpath->query("//ns:Add/ns:ApplicationData/Calendar:Attendees/Calendar:Attendee/Calendar:Email")->item(0)->nodeValue); $this->assertEquals('3', $xpath->query("//ns:Add/ns:ApplicationData/Calendar:Attendees/Calendar:Attendee/Calendar:AttendeeStatus")->item(0)->nodeValue); $serverId = $xpath->query("//ns:Add/ns:ServerId")->item(0)->nodeValue; $add = $xpath->query("//ns:Add"); $this->assertEquals(1, $add->length); //Send a dummy event with an invalid attendeestatus (just like outlook does) $request = << 2 {$tasksId} 0 0 512 0 1 1 Calendar {B94F1272-ED5F-4613-90D6-731491596147} AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== 20230724T173929Z 20230724T090000Z event1 CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2 doe@kolab1.mkpf.ch doe@kolab1.mkpf.ch activesynctest@kolab.org activesynctest@kolab.org 0 1 20230724T093000Z 0 2 0 15 3 1 0 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals("5", $xpath->query("//ns:Add/ns:Status")->item(0)->nodeValue); //Fetch the event and validate again $request = << 0 {$tasksId} 0 0 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $request = << 1 {$tasksId} 0 1 512 0 1 1 16 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals("CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2", $xpath->query("//ns:Add/ns:ApplicationData/Calendar:UID")->item(0)->nodeValue); $this->assertEquals('activesynctest@kolab.org', $xpath->query("//ns:Add/ns:ApplicationData/Calendar:Attendees/Calendar:Attendee/Calendar:Email")->item(0)->nodeValue); $this->assertEquals('3', $xpath->query("//ns:Add/ns:ApplicationData/Calendar:Attendees/Calendar:Attendee/Calendar:AttendeeStatus")->item(0)->nodeValue); //Send a dummy event to change to tentative (just like outlook does $request = << 2 {$tasksId} 0 0 512 0 1 1 Calendar {$serverId} AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== 20230724T173929Z 20230724T090000Z event1 CC54191F656DFBB294BE0AC18E709315-529CBBDD47ACDDC2 doe@kolab1.mkpf.ch doe@kolab1.mkpf.ch activesynctest@kolab.org activesynctest@kolab.org 0 1 20230724T093000Z 0 1 0 15 3 1 0 EOF; $response = $this->request($request, 'Sync'); $this->assertEquals(200, $response->getStatusCode()); $dom = self::fromWbxml($response->getBody()); print($dom->saveXML()); $xpath = $this->xpath($dom); $this->assertEquals("1", $xpath->query("//ns:Collection/ns:Status")->item(0)->nodeValue); - - } /** * @doesNotPerformAssertions */ public function testCleanup(): void { $this->deleteTestUser(self::$user->email); } }