b62d9d474ecace410bd597e3f9104a2af46f2508
[civicrm-core.git] / tests / phpunit / api / v3 / ExtensionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_extension_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Core
17 */
18
19 /**
20 * Class api_v3_ExtensionTest.
21 * @group headless
22 */
23 class api_v3_ExtensionTest extends CiviUnitTestCase {
24
25 public function setUp(): void {
26 $url = 'file://' . dirname(dirname(dirname(dirname(__FILE__)))) . '/mock/extension_browser_results';
27 Civi::settings()->set('ext_repo_url', $url);
28 }
29
30 public function tearDown(): void {
31 Civi::settings()->revert('ext_repo_url');
32 }
33
34 /**
35 * Test getremote.
36 */
37 public function testGetremote() {
38 $result = $this->callAPISuccess('extension', 'getremote', []);
39 $this->assertEquals('org.civicrm.module.cividiscount', $result['values'][0]['key']);
40 $this->assertEquals('module', $result['values'][0]['type']);
41 $this->assertEquals('CiviDiscount', $result['values'][0]['name']);
42 }
43
44 /**
45 * Test getting a single extension
46 * @see https://issues.civicrm.org/jira/browse/CRM-20532
47 */
48 public function testExtensionGetSingleExtension() {
49 $result = $this->callAPISuccess('extension', 'get', ['key' => 'test.extension.manager.moduletest']);
50 $this->assertEquals('test.extension.manager.moduletest', $result['values'][$result['id']]['key']);
51 $this->assertEquals('module', $result['values'][$result['id']]['type']);
52 $this->assertEquals('test_extension_manager_moduletest', $result['values'][$result['id']]['name']);
53 }
54
55 /**
56 * Test single Extension get with specific fields in return
57 * @see https://issues.civicrm.org/jira/browse/CRM-20532
58 */
59 public function testSingleExtensionGetWithReturnFields() {
60 $result = $this->callAPISuccess('extension', 'get', ['key' => 'test.extension.manager.moduletest', 'return' => ['name', 'status', 'key']]);
61 $this->assertEquals('test.extension.manager.moduletest', $result['values'][$result['id']]['key']);
62 $this->assertFalse(isset($result['values'][$result['id']]['type']));
63 $this->assertEquals('test_extension_manager_moduletest', $result['values'][$result['id']]['name']);
64 $this->assertEquals('uninstalled', $result['values'][$result['id']]['status']);
65 }
66
67 /**
68 * Test Extension Get returns detailed information
69 * Note that this is likely to fail locally but will work on Jenkins due to the result count check
70 * @see https://issues.civicrm.org/jira/browse/CRM-20532
71 */
72 public function testExtensionGet() {
73 $result = $this->callAPISuccess('extension', 'get', ['options' => ['limit' => 0]]);
74 $testExtensionResult = $this->callAPISuccess('extension', 'get', ['key' => 'test.extension.manager.paymenttest']);
75 $ext = $result['values'][$testExtensionResult['id']];
76 $this->assertNotNull($ext['typeInfo']);
77 $this->assertEquals(['mock'], $ext['tags']);
78 $this->assertTrue($result['count'] >= 6);
79 }
80
81 /**
82 * Filtering by status=installed or status=uninstalled should produce different results.
83 */
84 public function testExtensionGetByStatus() {
85 $installed = $this->callAPISuccess('extension', 'get', ['status' => 'installed', 'options' => ['limit' => 0]]);
86 $uninstalled = $this->callAPISuccess('extension', 'get', ['status' => 'uninstalled', 'options' => ['limit' => 0]]);
87
88 // If the filter works, then results should be strictly independent.
89 $this->assertEquals(
90 [],
91 array_intersect(
92 CRM_Utils_Array::collect('key', $installed['values']),
93 CRM_Utils_Array::collect('key', $uninstalled['values'])
94 )
95 );
96
97 $all = $this->callAPISuccess('extension', 'get', ['options' => ['limit' => 0]]);
98 $this->assertEquals($all['count'], $installed['count'] + $uninstalled['count']);
99 }
100
101 public function testGetMultipleExtensions() {
102 $result = $this->callAPISuccess('extension', 'get', ['key' => ['test.extension.manager.paymenttest', 'test.extension.manager.moduletest']]);
103 $this->assertEquals(2, $result['count']);
104 }
105
106 /**
107 * Test that extension get works with api request with parameter full_name as build by api explorer.
108 */
109 public function testGetMultipleExtensionsApiExplorer() {
110 $result = $this->callAPISuccess('extension', 'get', ['full_name' => ['test.extension.manager.paymenttest', 'test.extension.manager.moduletest']]);
111 $this->assertEquals(2, $result['count']);
112 }
113
114 /**
115 * Test that extension get can be filtered by id.
116 */
117 public function testGetExtensionByID() {
118 $result = $this->callAPISuccess('extension', 'get', ['id' => 2, 'return' => ['label']]);
119 $this->assertEquals(1, $result['count']);
120 }
121
122 }