From 3ee128fbbf85e1994e1fa3a30a4c890d84640590 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 27 Oct 2020 03:02:28 -0700 Subject: [PATCH] dev/core#2141 - APIv4 - Add OAuthProvider.get w/test. Split dist/local/test lists. --- ext/oauth-client/Civi/Api4/OAuthProvider.php | 78 +++++++++++++++++++ .../Civi/OAuth/CiviGenericProvider.php | 6 ++ .../data/oauth-providers.dist.json | 30 +++++++ .../data/oauth-providers.test.json | 21 +++++ ext/oauth-client/oauth_client.php | 21 +++++ .../phpunit/api/v4/OAuthProviderTest.php | 46 +++++++++++ ext/oauth-client/tests/phpunit/bootstrap.php | 1 + 7 files changed, 203 insertions(+) create mode 100644 ext/oauth-client/Civi/Api4/OAuthProvider.php create mode 100644 ext/oauth-client/Civi/OAuth/CiviGenericProvider.php create mode 100644 ext/oauth-client/data/oauth-providers.dist.json create mode 100644 ext/oauth-client/data/oauth-providers.test.json create mode 100644 ext/oauth-client/tests/phpunit/api/v4/OAuthProviderTest.php diff --git a/ext/oauth-client/Civi/Api4/OAuthProvider.php b/ext/oauth-client/Civi/Api4/OAuthProvider.php new file mode 100644 index 0000000000..47b33005e2 --- /dev/null +++ b/ext/oauth-client/Civi/Api4/OAuthProvider.php @@ -0,0 +1,78 @@ +has('OAuthProvider_list')) { + $providers = []; + $event = GenericHookEvent::create([ + 'providers' => &$providers, + ]); + \Civi::dispatcher()->dispatch('hook_civicrm_oauthProviders', $event); + + foreach ($providers as $name => &$provider) { + if ($provider['name'] !== $name) { + throw new \API_Exception(sprintf("Mismatched OAuth provider names: \"%s\" vs \"%s\"", + $provider['name'], $name)); + } + if (!isset($provider['class'])) { + $provider['class'] = CiviGenericProvider::class; + } + } + + $cache->set('OAuthProvider_list', $providers, self::TTL); + } + return $cache->get('OAuthProvider_list'); + }); + return $action->setCheckPermissions($checkPermissions); + } + + /** + * @param bool $checkPermissions + * @return Generic\BasicGetFieldsAction + */ + public static function getFields($checkPermissions = TRUE) { + $action = new Generic\BasicGetFieldsAction('OAuthProvider', __FUNCTION__, function () { + return [ + [ + 'name' => 'name', + ], + [ + 'name' => 'title', + ], + [ + 'name' => 'class', + ], + [ + 'name' => 'options', + ], + ]; + }); + return $action->setCheckPermissions($checkPermissions); + } + + /** + * @return array + */ + public static function permissions() { + return [ + "meta" => ["access CiviCRM"], + "get" => ["access CiviCRM"], + "default" => ["administer CiviCRM"], + ]; + } + +} diff --git a/ext/oauth-client/Civi/OAuth/CiviGenericProvider.php b/ext/oauth-client/Civi/OAuth/CiviGenericProvider.php new file mode 100644 index 0000000000..03969977be --- /dev/null +++ b/ext/oauth-client/Civi/OAuth/CiviGenericProvider.php @@ -0,0 +1,6 @@ +getPath('[civicrm.private]/oauth-providers.local.json'); + if (file_exists($localFile)) { + $ingest($localFile); + } +} diff --git a/ext/oauth-client/tests/phpunit/api/v4/OAuthProviderTest.php b/ext/oauth-client/tests/phpunit/api/v4/OAuthProviderTest.php new file mode 100644 index 0000000000..a7f54e880e --- /dev/null +++ b/ext/oauth-client/tests/phpunit/api/v4/OAuthProviderTest.php @@ -0,0 +1,46 @@ +install('oauth-client')->apply(); + } + + public function setUp() { + parent::setUp(); + } + + public function tearDown() { + parent::tearDown(); + } + + /** + * Create, read, and destroy token - with full access to secrets. + */ + public function testGet() { + \CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; + + $examples = Civi\Api4\OAuthProvider::get() + ->addWhere('name', 'LIKE', 'test_example%') + ->addOrderBy('name', 'DESC') + ->execute(); + $this->assertEquals(2, $examples->count()); + + $this->assertEquals('Civi\OAuth\CiviGenericProvider', $examples->last()['class']); + $this->assertEquals('My\Example2', $examples->first()['class']); + $this->assertEquals('https://example.com/one/auth', $examples->last()['options']['urlAuthorize']); + $this->assertEquals('https://example.com/two', $examples->first()['options']['urlAuthorize']); + } + +} diff --git a/ext/oauth-client/tests/phpunit/bootstrap.php b/ext/oauth-client/tests/phpunit/bootstrap.php index a5b49253c8..41c0800198 100644 --- a/ext/oauth-client/tests/phpunit/bootstrap.php +++ b/ext/oauth-client/tests/phpunit/bootstrap.php @@ -2,6 +2,7 @@ ini_set('memory_limit', '2G'); ini_set('safe_mode', 0); +define('CIVICRM_TEST', 1); // phpcs:disable eval(cv('php:boot --level=classloader', 'phpcode')); // phpcs:enable -- 2.25.1