4 * Class CRM_Extension_InfoTest
7 class CRM_Extension_InfoTest
extends CiviUnitTestCase
{
9 public function setUp(): void
{
14 public function tearDown(): void
{
21 public function testGood_file() {
22 $this->file
= tempnam(sys_get_temp_dir(), 'infoxml-');
23 file_put_contents($this->file
, "<extension key='test.foo' type='module'><file>foo</file><typeInfo><extra>zamboni</extra></typeInfo></extension>");
25 $info = CRM_Extension_Info
::loadFromFile($this->file
);
26 $this->assertEquals('test.foo', $info->key
);
27 $this->assertEquals('foo', $info->file
);
28 $this->assertEquals('zamboni', $info->typeInfo
['extra']);
31 public function testBad_file() {
33 $this->file
= tempnam(sys_get_temp_dir(), 'infoxml-');
34 file_put_contents($this->file
, "<extension key='test.foo' type='module'>file>foo</file></extension>");
38 $info = CRM_Extension_Info
::loadFromFile($this->file
);
40 catch (CRM_Extension_Exception
$e) {
43 $this->assertTrue(is_object($exc));
46 public function testGood_string() {
47 $data = "<extension key='test.foo' type='module'><file>foo</file><typeInfo><extra>zamboni</extra></typeInfo></extension>";
49 $info = CRM_Extension_Info
::loadFromString($data);
50 $this->assertEquals('test.foo', $info->key
);
51 $this->assertEquals('foo', $info->file
);
52 $this->assertEquals('zamboni', $info->typeInfo
['extra']);
53 $this->assertEquals(NULL, $info->upgrader
);
54 $this->assertEquals([], $info->requires
);
57 public function testGood_string_extras() {
58 $data = "<extension key='test.bar' type='module'><file>testbar</file>
60 <psr4 prefix=\"Civi\\\" path=\"Civi\"/>
61 <psr0 prefix=\"CRM_\" path=\"\"/>
63 <upgrader>CRM_Foo_Upgrader</upgrader>
64 <requires><ext>org.civicrm.a</ext><ext>org.civicrm.b</ext></requires>
68 $info = CRM_Extension_Info
::loadFromString($data);
69 $this->assertEquals('test.bar', $info->key
);
70 $this->assertEquals('testbar', $info->file
);
71 $this->assertEquals('Civi\\', $info->classloader
[0]['prefix']);
72 $this->assertEquals('Civi', $info->classloader
[0]['path']);
73 $this->assertEquals('psr4', $info->classloader
[0]['type']);
74 $this->assertEquals('CRM_', $info->classloader
[1]['prefix']);
75 $this->assertEquals('', $info->classloader
[1]['path']);
76 $this->assertEquals('psr0', $info->classloader
[1]['type']);
77 $this->assertEquals('CRM_Foo_Upgrader', $info->upgrader
);
78 $this->assertEquals(['org.civicrm.a', 'org.civicrm.b'], $info->requires
);
81 public function getExampleAuthors() {
82 $authorAliceXml = '<author><name>Alice</name><email>alice@example.org</email><role>Maintainer</role></author>';
83 $authorAliceArr = ['name' => 'Alice', 'email' => 'alice@example.org', 'role' => 'Maintainer'];
84 $authorBobXml = ' <author><name>Bob</name><homepage>https://example.com/bob</homepage><role>Developer</role></author>';
85 $authorBobArr = ['name' => 'Bob', 'homepage' => 'https://example.com/bob', 'role' => 'Developer'];
87 $maintAliceXml = '<maintainer><author>Alice</author><email>alice@example.org</email></maintainer>';
88 $maintAliceArr = ['author' => 'Alice', 'email' => 'alice@example.org'];
90 $hdr = "<extension key='test.author' type='module'><file>testauthor</file>";
91 $ftr = "</extension>";
93 // Maintainers can be inputted via either <maintainer> or <authors> (with role).
94 // Maintainers are outputted via both `$info->maintainer` and `$info->authors` (with role)
97 $cases[] = ["{$hdr}{$maintAliceXml}{$ftr}", [$authorAliceArr], $maintAliceArr];
98 $cases[] = ["{$hdr}<authors>{$authorAliceXml}</authors>{$ftr}", [$authorAliceArr], $maintAliceArr];
99 $cases[] = ["{$hdr}<authors>{$authorAliceXml}{$authorBobXml}</authors>{$ftr}", [$authorAliceArr, $authorBobArr], $maintAliceArr];
100 $cases[] = ["{$hdr}<authors>{$authorBobXml}</authors>{$ftr}", [$authorBobArr], NULL];
105 * @dataProvider getExampleAuthors
107 public function testAuthors($xmlString, $expectAuthors, $expectMaintainer) {
108 $info = CRM_Extension_Info
::loadFromString($xmlString);
109 $this->assertEquals($expectAuthors, $info->authors
);
110 $this->assertEquals($expectMaintainer, $info->maintainer
);
113 public function testBad_string() {
115 $data = "<extension key='test.foo' type='module'>file>foo</file></extension>";
119 $info = CRM_Extension_Info
::loadFromString($data);
121 catch (CRM_Extension_Exception
$e) {
124 $this->assertTrue(is_object($exc));
127 public function test_requirements() {
128 // Quicksearch requirement should get filtered out per extension-compatibility.json
129 $data = "<extension key='test.foo' type='module'><file>foo</file><requires><ext>example.test</ext><ext>com.ixiam.modules.quicksearch</ext></requires></extension>";
131 $info = CRM_Extension_Info
::loadFromString($data);
132 $this->assertEquals(['example.test'], $info->requires
);