Skip to content

Commit

Permalink
Test for AdminSettings::initPresets() (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstilkerich committed Dec 30, 2022
1 parent da9b481 commit b1c0790
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tests/Config.php
Expand Up @@ -46,7 +46,7 @@ class Config extends \MStilkerich\RCMCardDAV\Config
* the resource URI to the object to be returned for it. If the set object is an instance of Exception, this
* exception will be thrown by makeWebDavResource() instead.
*/
public $webDavResource;
public $webDavResources;

public function __construct(AbstractDatabase $db, TestLogger $logger, AdminSettings $admPrefs)
{
Expand Down Expand Up @@ -87,12 +87,12 @@ public function makeDiscoveryService(): Discovery

public function makeWebDavResource(string $uri, Account $account): WebDavResource
{
if (isset($this->webDavResource[$uri])) {
if ($this->webDavResource[$uri] instanceof Exception) {
throw $this->webDavResource[$uri];
if (isset($this->webDavResources[$uri])) {
if ($this->webDavResources[$uri] instanceof Exception) {
throw $this->webDavResources[$uri];
}

$res = $this->webDavResource[$uri];
$res = $this->webDavResources[$uri];
} else {
$res = parent::makeWebDavResource($uri, $account);
}
Expand Down
115 changes: 115 additions & 0 deletions tests/Unit/AdminSettingsWithDataTest.php
Expand Up @@ -26,6 +26,8 @@

namespace MStilkerich\Tests\RCMCardDAV\Unit;

use Exception;
use MStilkerich\CardDavClient\{AddressbookCollection,WebDavResource};
use MStilkerich\RCMCardDAV\Frontend\AddressbookManager;
use MStilkerich\Tests\RCMCardDAV\TestInfrastructure;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -181,6 +183,119 @@ public function testTemplateAddressbookProvidedCorrectly(): void
// template is not part of preset
$this->assertArrayNotHasKey('template', $tmpl);
}

/**
* @return array<string, list{string, list{?string,?string}}>
*/
public function initPresetDataProvider(): array
{
$base = 'tests/Unit/data/adminSettingsWithDataTest';

return [
'Two matchers, one match' => [ "$base/matchBoth.php", ['43', '43'] ],
'No matches (invalid preset, AND condition eval)' => [
"$base/noMatch.php",
[
'Setting for collected_recipients must include a valid preset attribute',
'Cannot set special addressbook collected_senders, there are 0 candidates (need: 1)'
]
],
'Multiple matches' => [
"$base/matchMultiple.php",
[
'Cannot set special addressbook collected_recipients, there are 2 candidates (need: 1)',
'Cannot set special addressbook collected_senders, there are 2 candidates (need: 1)'
]
],
'Preset not yet in DB' => [ "$base/presetNotInDbYet.php", [null, null] ],
];
}

/**
* Tests that presets are initialized and updated properly by AdminSettings::initPresets().
*
* The following is tested:
* - A new preset is added
* - Account is added to the DB with the proper settings
* - Extra addressbooks of the account are added to the DB, but no sync is attempted
* - A preset account existing in the DB is removed - RemovedPreset
* - The account and all its addressbooks are purged from the database.
* - A preset account existing in the DB is updated - UpdatedPreset
* - A new extra addressbook is added [NewXBook]
* - An extra addressbook existing in the DB is removed [RemovedBook]
* - Fixed settings for existing addressbooks are updated [username, refresh_time, require_always_email]
* - For a fixed name w/ server-side vars, the updated value is fetched from the server [Discovered Addressbook]
* - For an extra addressbook, its specific settings are taken if available [UpdatedXBook, require_always_email]
* - For an extra addressbook, main settings are taken if no specific available [UpdatedXBook, refresh_time]
* - An extra addressbook with invalid URL exists in the admin config, error is logged and remainder of the
* preset is properly processed [InvalidXBook]
* - A discovered addressbook does not exist on server anymore and server-side query is required to update name;
* it is not updated in the DB except for the name, and the rest of the preset is properly processed [book55]
* - Non-fixed settings with values different from the preset are retained [rediscover_time, use_categories]
*
* Note that the discovery of addressbooks belonging to a preset is out of scope of the AdminSettings::initPresets()
* function and therefore this test.
*/
public function testPresetsAreInitializedProperly(): void
{
$db = new JsonDatabase(['tests/Unit/data/adminSettingsWithDataTest/initPresetsDb.json']);
$this->db = $db;
$dbAfter = new JsonDatabase(['tests/Unit/data/adminSettingsWithDataTest/initPresetsDbAfter.json']);
$logger = TestInfrastructure::logger();
TestInfrastructure::init($this->db, 'tests/Unit/data/adminSettingsWithDataTest/initPresets.php');

$infra = TestInfrastructure::$infra;
$invalidXBookUrl = 'https://carddav.example.com/global/InvalidXBook/';
$infra->webDavResources = [
'https://carddav.example.com/books/johndoe/book42/' => $this->makeAbookCollStub(
'Book 42',
'https://carddav.example.com/books/johndoe/book42/',
"Hitchhiker's Guide"
),
'https://carddav.example.com/global/UpdatedXBook/' => $this->makeAbookCollStub(
'Updated XAbook',
'https://carddav.example.com/global/UpdatedXBook/',
'Public directory'
),
'https://carddav.example.com/global/NewXBook/' => $this->makeAbookCollStub(
'Added XAbook',
'https://carddav.example.com/global/NewXBook/',
'Newly added extra addressbook'
),
'https://carddav.example.com/global/RemovedBook' => new Exception('RemovedBook not on server anymore'),
$invalidXBookUrl => new Exception('InvalidXBook'),
"https://carddav.example.com/books/johndoe/book55/" => $this->createStub(WebDavResource::class),
];
$admPrefs = $infra->admPrefs();
$abMgr = new AddressbookManager();

$admPrefs->initPresets($abMgr, $infra);

$dbAfter->compareTables('accounts', $db);
$dbAfter->compareTables('addressbooks', $db);

$logger->expectMessage(
"error",
"Failed to add extra addressbook $invalidXBookUrl for preset UpdatedPreset: InvalidXBook"
);
$logger->expectMessage("error", "Cannot update name of addressbook 55: no addressbook collection at given URL");
}

/**
* Creates an AddressbookCollection stub that implements getUri() and getName().
*/
private function makeAbookCollStub(?string $name, string $url, ?string $desc): AddressbookCollection
{
$davobj = $this->createStub(AddressbookCollection::class);
$urlComp = explode('/', rtrim($url, '/'));
$baseName = $urlComp[count($urlComp) - 1];
$davobj->method('getName')->will($this->returnValue($name ?? $baseName));
$davobj->method('getBasename')->will($this->returnValue($baseName));
$davobj->method('getDisplayname')->will($this->returnValue($name));
$davobj->method('getDescription')->will($this->returnValue($desc));
$davobj->method('getUri')->will($this->returnValue($url));
return $davobj;
}
}

// vim: ts=4:sw=4:expandtab:fenc=utf8:ff=unix:tw=120
41 changes: 41 additions & 0 deletions tests/Unit/data/adminSettingsWithDataTest/initPresets.php
@@ -0,0 +1,41 @@
<?php

$prefs['_GLOBAL']['loglevel'] = \Psr\Log\LogLevel::DEBUG;
$prefs['_GLOBAL']['loglevel_http'] = \Psr\Log\LogLevel::INFO;

$prefs['UpdatedPreset'] = [
'accountname' => 'Updated Account',
'discovery_url' => 'https://carddav.example.com/',

'fixed' => [ 'username', 'refresh_time', 'require_always_email', 'name' ],

'username' => 'foodoo',
'password' => 'bar',
'rediscover_time' => '1:00',

'refresh_time' => '0:30',
'require_always_email' => true,
'name' => '%N (%D)',
'use_categories' => false,

'extra_addressbooks' => [
[
'url' => 'https://carddav.example.com/global/UpdatedXBook/',
'name' => 'Public readonly contacts',
'readonly' => true,
'require_always_email' => false,
],
[
'url' => 'https://carddav.example.com/global/NewXBook/',
'name' => '%D',
'readonly' => true,
'require_always_email' => false,
'active' => false
],
[
'url' => 'https://carddav.example.com/global/InvalidXBook/',
],
],
];

// vim: ts=4:sw=4:expandtab:fenc=utf8:ff=unix:tw=120:ft=php
111 changes: 111 additions & 0 deletions tests/Unit/data/adminSettingsWithDataTest/initPresetsDb.json
@@ -0,0 +1,111 @@
{
"accounts": [
{
"id": 42,
"accountname": "Removed Account",
"username": "%u",
"password": "%p",
"discovery_url": "https://rm.example.com/",
"user_id": "105",
"last_discovered": 12345,
"rediscover_time": 86400,
"presetname": "RemovedPreset"
},
{
"id": 43,
"accountname": "Updated Account",
"username": "foo",
"password": "bar",
"discovery_url": "https://carddav.example.com/",
"user_id": "105",
"last_discovered": 12345,
"rediscover_time": 86400,
"presetname": "UpdatedPreset"
}
],
"addressbooks": [
{
"id": 42,
"name": "Discovered",
"url": "https://rm.example.com/books/johndoe/book42/",
"flags": 7,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 42
},
{
"id": 43,
"name": "Extra addressbook",
"url": "https://rm.example.com/books/johndoe/book43/",
"flags": 11,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 42
},
{
"id": 44,
"name": "%N",
"url": "",
"flags": 35,
"last_updated": 0,
"refresh_time": 3600,
"sync_token": "",
"account_id": 42
},



{
"id": 51,
"name": "Discovered addressbook",
"url": "https://carddav.example.com/books/johndoe/book42/",
"flags": 7,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 52,
"name": "Removed extra addressbook",
"url": "https://carddav.example.com/global/RemovedBook/",
"flags": 3,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 53,
"name": "%N",
"url": "",
"flags": 35,
"last_updated": 0,
"refresh_time": 3600,
"sync_token": "",
"account_id": 43
},
{
"id": 54,
"name": "Updated extra addressbook",
"url": "https://carddav.example.com/global/UpdatedXBook/",
"flags": 3,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 55,
"name": "Discovered addressbook not existent on server anymore",
"url": "https://carddav.example.com/books/johndoe/book55/",
"flags": 7,
"last_updated": 12345,
"refresh_time": 3600,
"sync_token": "sync@3600",
"account_id": 43
}
]
}
67 changes: 67 additions & 0 deletions tests/Unit/data/adminSettingsWithDataTest/initPresetsDbAfter.json
@@ -0,0 +1,67 @@
{
"accounts": [
{
"id": 43,
"accountname": "Updated Account",
"username": "foodoo",
"password": "bar",
"discovery_url": "https://carddav.example.com/",
"user_id": "105",
"last_discovered": 12345,
"rediscover_time": 86400,
"presetname": "UpdatedPreset"
}
],
"addressbooks": [
{
"id": 51,
"name": "Book 42 (Hitchhiker's Guide)",
"url": "https://carddav.example.com/books/johndoe/book42/",
"flags": 23,
"last_updated": 12345,
"refresh_time": 1800,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 53,
"name": "%N (%D)",
"url": "",
"flags": 51,
"last_updated": 0,
"refresh_time": 1800,
"sync_token": "",
"account_id": 43
},
{
"id": 54,
"name": "Public readonly contacts",
"url": "https://carddav.example.com/global/UpdatedXBook/",
"flags": 11,
"last_updated": 12345,
"refresh_time": 1800,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 55,
"name": "Discovered addressbook not existent on server anymore",
"url": "https://carddav.example.com/books/johndoe/book55/",
"flags": 23,
"last_updated": 12345,
"refresh_time": 1800,
"sync_token": "sync@3600",
"account_id": 43
},
{
"id": 56,
"name": "Newly added extra addressbook",
"url": "https://carddav.example.com/global/NewXBook/",
"flags": 8,
"last_updated": 0,
"refresh_time": 1800,
"sync_token": "",
"account_id": 43
}
]
}

0 comments on commit b1c0790

Please sign in to comment.