From 72657f71263c8023a1529db1a308ceefd14bcc87 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 26 Feb 2021 13:50:06 -0800 Subject: [PATCH] dev/core#2418 - info.xml - Parse `` tag --- CRM/Extension/Info.php | 31 +++++++++++++++++++++++ tests/phpunit/CRM/Extension/InfoTest.php | 32 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/CRM/Extension/Info.php b/CRM/Extension/Info.php index 43e185d326..7a0683be43 100644 --- a/CRM/Extension/Info.php +++ b/CRM/Extension/Info.php @@ -50,6 +50,21 @@ class CRM_Extension_Info { */ public $tags = []; + /** + * @var array + * List of authors. + * Ex: [0 => ['name' => 'Alice', 'email' => 'a@b', 'homepage' => 'https://example.com', 'role' => 'Person']] + */ + public $authors = []; + + /** + * @var array|null + * The current maintainer at time of publication. + * This is deprecated in favor of $authors. + * @deprecated + */ + public $maintainer = NULL; + /** * Load extension info an XML file. * @@ -170,6 +185,22 @@ class CRM_Extension_Info { elseif ($attr === 'requires') { $this->requires = $this->filterRequirements($val); } + elseif ($attr === 'maintainer') { + $this->maintainer = CRM_Utils_XML::xmlObjToArray($val); + $this->authors[] = [ + 'name' => (string) $val->author, + 'email' => (string) $val->email, + 'role' => 'Maintainer', + ]; + } + elseif ($attr === 'authors') { + foreach ($val->author as $author) { + $this->authors[] = $thisAuthor = CRM_Utils_XML::xmlObjToArray($author); + if ('maintainer' === strtolower($thisAuthor['role'] ?? '')) { + $this->maintainer = ['author' => $thisAuthor['name'], 'email' => $thisAuthor['email'] ?? NULL]; + } + } + } else { $this->$attr = CRM_Utils_XML::xmlObjToArray($val); } diff --git a/tests/phpunit/CRM/Extension/InfoTest.php b/tests/phpunit/CRM/Extension/InfoTest.php index 12cfc4c16b..77b46076ac 100644 --- a/tests/phpunit/CRM/Extension/InfoTest.php +++ b/tests/phpunit/CRM/Extension/InfoTest.php @@ -68,6 +68,38 @@ class CRM_Extension_InfoTest extends CiviUnitTestCase { $this->assertEquals(['org.civicrm.a', 'org.civicrm.b'], $info->requires); } + public function getExampleAuthors() { + $authorAliceXml = 'Alicealice@example.orgMaintainer'; + $authorAliceArr = ['name' => 'Alice', 'email' => 'alice@example.org', 'role' => 'Maintainer']; + $authorBobXml = ' Bobhttps://example.com/bobDeveloper'; + $authorBobArr = ['name' => 'Bob', 'homepage' => 'https://example.com/bob', 'role' => 'Developer']; + + $maintAliceXml = 'Alicealice@example.org'; + $maintAliceArr = ['author' => 'Alice', 'email' => 'alice@example.org']; + + $hdr = "testauthor"; + $ftr = ""; + + // Maintainers can be inputted via either or (with role). + // Maintainers are outputted via both `$info->maintainer` and `$info->authors` (with role) + + $cases = []; + $cases[] = ["{$hdr}{$maintAliceXml}{$ftr}", [$authorAliceArr], $maintAliceArr]; + $cases[] = ["{$hdr}{$authorAliceXml}{$ftr}", [$authorAliceArr], $maintAliceArr]; + $cases[] = ["{$hdr}{$authorAliceXml}{$authorBobXml}{$ftr}", [$authorAliceArr, $authorBobArr], $maintAliceArr]; + $cases[] = ["{$hdr}{$authorBobXml}{$ftr}", [$authorBobArr], NULL]; + return $cases; + } + + /** + * @dataProvider getExampleAuthors + */ + public function testAuthors($xmlString, $expectAuthors, $expectMaintainer) { + $info = CRM_Extension_Info::loadFromString($xmlString); + $this->assertEquals($expectAuthors, $info->authors); + $this->assertEquals($expectMaintainer, $info->maintainer); + } + public function testBad_string() { // vs file> $data = "file>foo"; -- 2.25.1