APIv4 - Treat navigation permissions as array, add pseudoconstant for operator
authorColeman Watts <coleman@civicrm.org>
Mon, 29 Nov 2021 19:26:35 +0000 (14:26 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 29 Nov 2021 21:29:26 +0000 (16:29 -0500)
This gives consistency in how the fields are handled in the Navigation and Dashboard entities

CRM/Core/DAO/Dashboard.php
CRM/Core/DAO/Navigation.php
CRM/Core/SelectValues.php
ext/search_kit/CRM/Search/Upgrader.php
tests/phpunit/api/v4/Entity/NavigationTest.php [new file with mode: 0644]
xml/schema/Core/Dashboard.xml
xml/schema/Core/Navigation.xml

index 127a74dd9cc97a8a0da9b3db9226d2eeffe6c98d..4283041796acb12f0f0928c24d9a29f41a21d5e0 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/Dashboard.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:0404bd5bf3e04c20da16ec1950db8ac0)
+ * (GenCodeChecksum:9603ee2a3a27196ef1ec671bf758a783)
  */
 
 /**
@@ -260,6 +260,9 @@ class CRM_Core_DAO_Dashboard extends CRM_Core_DAO {
           'entity' => 'Dashboard',
           'bao' => 'CRM_Core_BAO_Dashboard',
           'localizable' => 0,
+          'pseudoconstant' => [
+            'callback' => 'CRM_Core_SelectValues::andOr',
+          ],
           'add' => '3.1',
         ],
         'fullscreen_url' => [
index 17a36917d8344bf0ecbef7cacbf44d453f66c552..f57b5fe09823e372b39b21298310271ac81a8df5 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/Navigation.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:2b9fb139d42e95f23ebc1fe7b055c546)
+ * (GenCodeChecksum:6400fef9c925231f0d6245030ec46e68)
  */
 
 /**
@@ -78,14 +78,14 @@ class CRM_Core_DAO_Navigation extends CRM_Core_DAO {
   public $icon;
 
   /**
-   * Permission for menu item
+   * Permission(s) needed to access menu item
    *
    * @var string
    */
   public $permission;
 
   /**
-   * Permission Operator
+   * Operator to use if item has more than one permission
    *
    * @var string
    */
@@ -261,7 +261,7 @@ class CRM_Core_DAO_Navigation extends CRM_Core_DAO {
           'name' => 'permission',
           'type' => CRM_Utils_Type::T_STRING,
           'title' => ts('Required Permission'),
-          'description' => ts('Permission for menu item'),
+          'description' => ts('Permission(s) needed to access menu item'),
           'maxlength' => 255,
           'size' => CRM_Utils_Type::HUGE,
           'where' => 'civicrm_navigation.permission',
@@ -269,13 +269,14 @@ class CRM_Core_DAO_Navigation extends CRM_Core_DAO {
           'entity' => 'Navigation',
           'bao' => 'CRM_Core_BAO_Navigation',
           'localizable' => 0,
+          'serialize' => self::SERIALIZE_COMMA,
           'add' => '3.0',
         ],
         'permission_operator' => [
           'name' => 'permission_operator',
           'type' => CRM_Utils_Type::T_STRING,
           'title' => ts('Permission Operator'),
-          'description' => ts('Permission Operator'),
+          'description' => ts('Operator to use if item has more than one permission'),
           'maxlength' => 3,
           'size' => CRM_Utils_Type::FOUR,
           'where' => 'civicrm_navigation.permission_operator',
@@ -283,6 +284,9 @@ class CRM_Core_DAO_Navigation extends CRM_Core_DAO {
           'entity' => 'Navigation',
           'bao' => 'CRM_Core_BAO_Navigation',
           'localizable' => 0,
+          'pseudoconstant' => [
+            'callback' => 'CRM_Core_SelectValues::andOr',
+          ],
           'add' => '3.0',
         ],
         'parent_id' => [
index 32b06fa2e4e4fa53acaae6250864eb1b701af4eb..4dbdc153c974e181b5fc868635819fe11c65f3dc 100644 (file)
@@ -1101,4 +1101,14 @@ class CRM_Core_SelectValues {
     ];
   }
 
+  /**
+   * @return array
+   */
+  public static function andOr() {
+    return [
+      'AND' => ts('And'),
+      'OR' => ts('Or'),
+    ];
+  }
+
 }
index a0dbb0d1fababe7b05b650c1c76be76ebd3353dc..321ed4cc8274e74e9289dc45c6b588eb9417eb34 100644 (file)
@@ -18,7 +18,7 @@ class CRM_Search_Upgrader extends CRM_Search_Upgrader_Base {
       ->addValue('icon', 'crm-i fa-search-plus')
       ->addValue('has_separator', 2)
       ->addValue('weight', 99)
-      ->addValue('permission', 'administer CiviCRM data')
+      ->addValue('permission', ['administer CiviCRM data'])
       ->execute();
   }
 
diff --git a/tests/phpunit/api/v4/Entity/NavigationTest.php b/tests/phpunit/api/v4/Entity/NavigationTest.php
new file mode 100644 (file)
index 0000000..a3a502f
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+namespace api\v4\Entity;
+
+use api\v4\UnitTestCase;
+use Civi\Api4\Navigation;
+use Civi\Test\TransactionalInterface;
+
+/**
+ * @group headless
+ */
+class NavigationTest extends UnitTestCase implements TransactionalInterface {
+
+  public function testCreate() {
+    $created = Navigation::create(FALSE)
+      ->addValue('permission', ['administer CiviCRM', 'access CiviCRM'])
+      ->addValue('name', 'Test menu item')
+      ->execute()->single();
+
+    $fetched = Navigation::get(FALSE)
+      ->addWhere('id', '=', $created['id'])
+      ->execute()->single();
+
+    $this->assertEquals(['administer CiviCRM', 'access CiviCRM'], $created['permission']);
+    $this->assertEquals(\CRM_Core_Config::domainID(), $fetched['domain_id']);
+    $this->assertGreaterThan(0, $fetched['weight']);
+  }
+
+}
index 63fc5b1599b4bc3837f0aac4a958a625e59748cd..b68fe09b897e5d69838738203eed516dad167036 100644 (file)
@@ -82,6 +82,9 @@
     <type>varchar</type>
     <title>Dashlet Permission Operator</title>
     <length>3</length>
+    <pseudoconstant>
+      <callback>CRM_Core_SelectValues::andOr</callback>
+    </pseudoconstant>
     <comment>Permission Operator</comment>
     <add>3.1</add>
   </field>
index 3b7d80f4d1bdff32b4103fab4bcd8ecb38d0100b..dd489e66e00cfe70c68f54bb28e9361af4a46532 100644 (file)
@@ -82,7 +82,8 @@
     <title>Required Permission</title>
     <type>varchar</type>
     <length>255</length>
-    <comment>Permission for menu item</comment>
+    <comment>Permission(s) needed to access menu item</comment>
+    <serialize>COMMA</serialize>
     <add>3.0</add>
   </field>
   <field>
     <title>Permission Operator</title>
     <type>varchar</type>
     <length>3</length>
-    <comment>Permission Operator</comment>
+    <pseudoconstant>
+      <callback>CRM_Core_SelectValues::andOr</callback>
+    </pseudoconstant>
+    <comment>Operator to use if item has more than one permission</comment>
     <add>3.0</add>
   </field>
   <field>