dev/core#2418 - info.xml - Parse `<authors>` tag
authorTim Otten <totten@civicrm.org>
Fri, 26 Feb 2021 21:50:06 +0000 (13:50 -0800)
committerTim Otten <totten@civicrm.org>
Sun, 28 Feb 2021 03:08:54 +0000 (19:08 -0800)
CRM/Extension/Info.php
tests/phpunit/CRM/Extension/InfoTest.php

index 43e185d3264fb0eb5daa8d8a78d5e147be745304..7a0683be43bf6c5539851dbc51269ca741b4e378 100644 (file)
@@ -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);
       }
index 12cfc4c16bfe90be75fb2eb89f099561018bcffa..77b46076aca5b5a13dcb8c87e0239b481476e283 100644 (file)
@@ -68,6 +68,38 @@ class CRM_Extension_InfoTest extends CiviUnitTestCase {
     $this->assertEquals(['org.civicrm.a', 'org.civicrm.b'], $info->requires);
   }
 
+  public function getExampleAuthors() {
+    $authorAliceXml = '<author><name>Alice</name><email>alice@example.org</email><role>Maintainer</role></author>';
+    $authorAliceArr = ['name' => 'Alice', 'email' => 'alice@example.org', 'role' => 'Maintainer'];
+    $authorBobXml = ' <author><name>Bob</name><homepage>https://example.com/bob</homepage><role>Developer</role></author>';
+    $authorBobArr = ['name' => 'Bob', 'homepage' => 'https://example.com/bob', 'role' => 'Developer'];
+
+    $maintAliceXml = '<maintainer><author>Alice</author><email>alice@example.org</email></maintainer>';
+    $maintAliceArr = ['author' => 'Alice', 'email' => 'alice@example.org'];
+
+    $hdr = "<extension key='test.author' type='module'><file>testauthor</file>";
+    $ftr = "</extension>";
+
+    // Maintainers can be inputted via either <maintainer> or <authors> (with role).
+    // Maintainers are outputted via both `$info->maintainer` and `$info->authors` (with role)
+
+    $cases = [];
+    $cases[] = ["{$hdr}{$maintAliceXml}{$ftr}", [$authorAliceArr], $maintAliceArr];
+    $cases[] = ["{$hdr}<authors>{$authorAliceXml}</authors>{$ftr}", [$authorAliceArr], $maintAliceArr];
+    $cases[] = ["{$hdr}<authors>{$authorAliceXml}{$authorBobXml}</authors>{$ftr}", [$authorAliceArr, $authorBobArr], $maintAliceArr];
+    $cases[] = ["{$hdr}<authors>{$authorBobXml}</authors>{$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() {
     // <file> vs file>
     $data = "<extension key='test.foo' type='module'>file>foo</file></extension>";