Skip to content

Commit

Permalink
Tests for get_group()
Browse files Browse the repository at this point in the history
  • Loading branch information
mstilkerich committed Feb 24, 2021
1 parent 558e3b2 commit 95cb20e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/Addressbook.php
Expand Up @@ -776,10 +776,10 @@ function (array $g1, array $g2): int {
*
* @param string $group_id Group identifier
*
* @return array Group properties as hash array
* @return ?GroupSaveData Group properties as hash array, null in case of error.
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName -- method name defined by rcube_addressbook class
public function get_group($group_id): array
public function get_group($group_id): ?array
{
$infra = Config::inst();
$logger = $infra->logger();
Expand All @@ -791,14 +791,16 @@ public function get_group($group_id): array
// As of 1.4.6, roundcube is interested in name and email properties of a group,
// i. e. if the group as a distribution list had an email address of its own. Otherwise, it will fall back
// to getting the individual members' addresses
/** @var array{id: numeric-string, name: string} $result */
$result = $db->lookup(["id" => $group_id, "abook_id" => $this->id], 'id,name', 'groups');
$result['ID'] = $result['id'];
return $result;
} catch (\Exception $e) {
$logger->error("get_group($group_id): " . $e->getMessage());
$this->set_error(rcube_addressbook::ERROR_SEARCH, $e->getMessage());
return [];
$logger->error("get_group($group_id): Could not get group: " . $e->getMessage());
$this->set_error(rcube_addressbook::ERROR_SEARCH, "Could not get group $group_id");
}

return $result;
return null;
}

/**
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/AddressbookTest.php
Expand Up @@ -591,7 +591,6 @@ public function getRecordProvider(): array
* Tests that get_record() returns expected record.
*
* @dataProvider getRecordProvider
* @param list<string> $expRecords
*/
public function testGetRecordProvidesExpectedRecord(string $id, bool $assoc, bool $expError): void
{
Expand Down Expand Up @@ -638,6 +637,7 @@ public function testGetRecordProvidesExpectedRecord(string $id, bool $assoc, boo
}
}
}

/**
* @return list<array{?string, int, list<string>}>
*/
Expand Down Expand Up @@ -678,6 +678,45 @@ public function testListGroupsProvidesExpectedGroups(?string $filter, int $searc
}
}

/** @return array<string, array{string,bool}> */
public function getGroupProvider(): array
{
return [
'Valid ID' => [ '500', false ],
'Invalid ID' => [ '50', true ],
];
}

/**
* Tests that get_group() returns expected record.
*
* @dataProvider getGroupProvider
*/
public function testGetGroupProvidesExpectedRecord(string $id, bool $expError): void
{
$abook = $this->createAbook();

$saveDataRc = $abook->get_group($id);

if ($expError) {
$this->assertNull($saveDataRc);

$logger = TestInfrastructure::logger();
$logger->expectMessage('error', "Could not get group");

$abookErr = $abook->get_error();
$this->assertIsArray($abookErr);
$this->assertSame(rcube_addressbook::ERROR_SEARCH, $abookErr['type']);
$this->assertStringContainsString("Could not get group $id", (string) $abookErr['message']);
} else {
$this->assertIsArray($saveDataRc);

$fn = "tests/unit/data/addressbookTest/g{$id}.json";
$saveDataExp = Utils::readSaveDataFromJson($fn);
Utils::compareSaveData($saveDataExp, $saveDataRc, "Unexpected record data $id");
}
}

/**
* Asserts that a warning message concerning failure to download the photo has been issued for VCards where an
* invalid Photo URI is used.
Expand Down

0 comments on commit 95cb20e

Please sign in to comment.