From 17b9f7be8a717a60c503b30b4ca64d8f899001fb Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 7 Dec 2014 01:36:15 -0800 Subject: [PATCH] CRM-15578 - StaticProvider, etal - Prep for unit-tests of Attachment API --- CRM/Core/Permission/UnitTests.php | 7 ++ Civi/API/Provider/AdhocProvider.php | 6 +- Civi/API/Provider/StaticProvider.php | 122 +++++++++++++++++++++++++++ xml/templates/listAll.tpl | 7 +- 4 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 Civi/API/Provider/StaticProvider.php diff --git a/CRM/Core/Permission/UnitTests.php b/CRM/Core/Permission/UnitTests.php index c083e73ec6..d4d2cb70ef 100644 --- a/CRM/Core/Permission/UnitTests.php +++ b/CRM/Core/Permission/UnitTests.php @@ -50,6 +50,13 @@ class CRM_Core_Permission_UnitTests extends CRM_Core_Permission_Base { * @access public */ function check($str) { + if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) { + return FALSE; + } + if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) { + return TRUE; + } + // return the stubbed permission (defaulting to true if the array is missing) return is_array($this->permissions) ? in_array($str, $this->permissions) : TRUE; } diff --git a/Civi/API/Provider/AdhocProvider.php b/Civi/API/Provider/AdhocProvider.php index 35cca808b7..cb4ff72d58 100644 --- a/Civi/API/Provider/AdhocProvider.php +++ b/Civi/API/Provider/AdhocProvider.php @@ -54,17 +54,17 @@ class AdhocProvider implements EventSubscriberInterface, ProviderInterface { /** * @var array (string $name => array('perm' => string, 'callback' => callable)) */ - private $actions = array(); + protected $actions = array(); /** * @var string */ - private $entity; + protected $entity; /** * @var int */ - private $version; + protected $version; /** * @param int $version diff --git a/Civi/API/Provider/StaticProvider.php b/Civi/API/Provider/StaticProvider.php new file mode 100644 index 0000000000..130255ab83 --- /dev/null +++ b/Civi/API/Provider/StaticProvider.php @@ -0,0 +1,122 @@ + array( + array('onApiResolve', Events::W_MIDDLE), + ), + Events::AUTHORIZE => array( + array('onApiAuthorize', Events::W_MIDDLE), + ), + ); + } + + public function __construct($version, $entity, $fields, $perms = array(), $records = array()) { + parent::__construct($version, $entity); + + $perms = array_merge(array( + 'create' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + 'get' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + 'delete' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + ), $perms); + + $this->records = \CRM_Utils_Array::index(array('id'), $records); + $this->fields = $fields; + + $this->addAction('create', $perms['create'], array($this, '_create')); + $this->addAction('get', $perms['get'], array($this, '_get')); + $this->addAction('delete', $perms['delete'], array($this, '_delete')); + } + + /** + * @return array + */ + public function getRecords() { + return $this->records; + } + + /** + * @param array $records + */ + public function setRecords($records) { + $this->records = $records; + } + + public function _create($apiRequest) { + if (isset($apiRequest['params']['id'])) { + $id = $apiRequest['params']['id']; + } + else { + $id = max(array_keys($this->records)) + 1; + $this->records[$id] = array(); + } + + if (!isset($this->records[$id])) { + throw new \API_Exception("Invalid ID: $id"); + } + + foreach ($this->fields as $field) { + if (isset($apiRequest['params'][$field])) { + $this->records[$id][$field] = $apiRequest['params'][$field]; + } + } + + return civicrm_api3_create_success($this->records[$id]); + } + + public function _get($apiRequest) { + $id = @$apiRequest['params']['id']; + if ($id && isset($this->records[$id])) { + return civicrm_api3_create_success(array($id => $this->records[$id])); + } + else { + return civicrm_api3_create_success(array()); + } + } + + public function _delete($apiRequest) { + $id = @$apiRequest['params']['id']; + if ($id && isset($this->records[$id])) { + unset($this->records[$id]); + } + return civicrm_api3_create_success(array()); + } +} diff --git a/xml/templates/listAll.tpl b/xml/templates/listAll.tpl index f302ac1163..6bf1a941b7 100644 --- a/xml/templates/listAll.tpl +++ b/xml/templates/listAll.tpl @@ -39,7 +39,7 @@ class CRM_Core_DAO_AllCoreTables {ldelim} static private $tables = null; static private $daoToClass = null; - static private function init($fresh = FALSE) {ldelim} + static public function init($fresh = FALSE) {ldelim} static $init = FALSE; if ($init && !$fresh) return; @@ -64,7 +64,10 @@ class CRM_Core_DAO_AllCoreTables {ldelim} $init = TRUE; {rdelim} - static private function registerEntityType($daoName, $className, $tableName) {ldelim} + /** + * (Quasi-Private) Do not call externally (except for unit-testing) + */ + static public function registerEntityType($daoName, $className, $tableName) {ldelim} self::$daoToClass[$daoName] = $className; self::$tables[$tableName] = $className; {rdelim} -- 2.25.1